/*
 * Facebook Graph for SK
 * Author: Pao
 */

function _gmailOpenAuth () {

	var window_width = 720;
	var window_height = 470;
    var center_width = (window.screen.width - window_width) / 2;
    var center_height = (window.screen.height - window_height) / 2;
	var fbpop = window.open( gmail_oauth_url, "FBOAuth", "toolbar=0,height=" + window_height + ",width=" + window_width + ",left=" + center_width + ",top=" + center_height);
	fbpop.focus();

}

function _fbOpenAuth () {

	var window_width = 720;
	var window_height = 470;
    var center_width = (window.screen.width - window_width) / 2;
    var center_height = (window.screen.height - window_height) / 2;

	var fbpop = window.open( fb_oauth_url, "FBOAuth", "toolbar=0,height=" + window_height + ",width=" + window_width + ",left=" + center_width + ",top=" + center_height);
	fbpop.focus();

}

function _fbCloseOAuth (url) {
	if(opener) {
		window.opener.location.reload();
		window.close();
	} else
	{
		window.location.href = url;
	}
}

zip_cache = {};
zip_cache[320] = {};
zip_cache[253] = {};
zip_cache[443] = {};

$.form = {

	request : function (request_url, request_data, request_callback) {
		$.ajax({
			type: 'POST',
			url : request_url,
			data : request_data,
			beforeSend : function () {
				$(".form form input[type=button]").attr("disabled", "disabled");
				$(".buttons img").show();
			},
			success : request_callback
		});
	},

	check : function (data) {
		this.request("form_validation.php?form=facebook&fcn=Ajax", data, this.response);
	},

	response : function (data) {
		$(".error").hide();
		$(".buttons img").hide();
		$(".form form input[type=button]").attr("disabled", "");
		if(data) {
			$(".error").html(data).fadeIn(900);
		} else {
			$(".buttons img").show();
			$(".form form input[type=button]").attr("disabled", "disabled");
			$(".form form").submit();
		}
	},

	get_city_list : function (country, zip) {

		if(zip != '')
	    {
	        if(zip_cache[country][zip.substr(0,1)] != undefined)
	        {
	            $(".form select[name=urcity] option").remove();
	            $(".form select[name=urcity]").append(zip_cache[country][zip.substr(0,1)]);
	        } else
	        {
	        	$.ajax({
	        		type: 'POST',
	        		url: 'ajax/getzip.php',
	        		data: 'wb_country=' + country + '&plz=' + zip.substr(0,1),
	        		beforeSend: function() {
	        			$("#urcity_text").attr("disabled", "disabled");
	        		},
	        		success: function(data) {
	        			$(".form select[name=urcity]").attr("disabled", "");
	        			$(".form select[name=urcity] option").remove();
	        			if(data!='') {
	        			    zip_cache[country][zip.substr(0,1)] = data;
	        				$(".form select[name=urcity]").append(data);
	        			}
	        		}
	        	});
	        }
	    } else
	    {
	        $("#urcity_text option").remove();
	    }
	}

}

$(document).ready(function () {

	if( top.frames.length == 0 )
	{
		var _top = top.location.href;

		if( _top.indexOf('?page=facebook') > 0 || _top.indexOf('?page=gmail') > 0 )
		{

			// set validation type
    		var type = $("#validation_type").val();
			var zip = $(".form input[name=zip]");
			var country = $(".form select[name=usercountry]");

                        $("#agb_text").hide();
                        $(".agb").hide();

			if (type == 1)
			{
				$.form.get_city_list(country.val(), zip.val());

				country.change(function() {
					if($(this).val() == 443)
					{
						if(zip.val().length > 4)
						{
							zip.val(zip.val().substr(0, 4));
						}
						zip.attr("maxlength", "4");
					} else
					{
						zip.attr("maxlength", "5");
					}
					$.form.get_city_list($(this).val(), zip.val());
				});

				zip.keyup(function() {
					$.form.get_city_list(country.val(), $(this).val());
				});

			}

			$(".form input[type=button]").click(function () {
				$(".error").hide();
				$.form.check($(".form form").serialize());
				return false;
			});

			$("#agb_link").click(function() {
				$(".agb").fadeIn(300)
				$("#agb_text").fadeIn(600);
			});

			$("#default-pic").hide();
			$("#usefbpic").attr("checked", "checked");
			$("#usefbpic").change(function(){
				if($(this).attr("checked")){
					$("#fb-pic").show();
					$("#default-pic").hide();
				} else {
					$("#fb-pic").hide();
					$("#default-pic").show();
				}
			});
		}
	}
});



















