/**
* Return the Y page's position
*/
function getY(){
	var posY = 0;
	// With Mozilla
	if (window.innerHeight){
		posY = window.pageYOffset;
	// With Internet Explorer (if there is 'DOCTYPE' header)
	}else if (document.documentElement.scrollTop){
		posY = document.documentElement.scrollTop;
	// With Internet Explorer (without 'DOCTYPE' header)
	}else if (document.body){
		posY = document.body.scrollTop;
	}
	return posY;
}
function getX(){
	var posX = 0;
	posX = ($(document).width())/2-(parseInt($('#noticeArea').width())/2);

	return posX;
}

function Site() {}

Site.prototype._getValue = function(elementId) {
	try{
		return $('#'+elementId).val();
	}catch(e){
		alert(elementId+' is not valid!');
	}
}


Site.prototype.chooseLocation = function(location){
	$.ajax({
		data:{
			action: "chooseLocation",
			country_code : location
		}
	});

	return true;
}

Site.prototype.displayOrHideRetaTable = function(id){
 $("#rate > table").each(function(i){
	 if ($(this).attr("id") == id){
	 	$(this).show();
	 }else{
	 	if ($(this).attr("id"))
	 		$(this).hide();
	 }
 });
}
Site.prototype.displayReview = function(){
 $(".idTabs > ul > li").each(function(i){
 	$('a',this).removeClass();
 });
 $('a[@href$="#review"]').addClass("selected");

 $(".idTabs > div").each(function(i){
 	$(this).hide();
 });
 $("#review").show();
}

Site.prototype.getClientHeight = function(){
	var winH = 0;
	if (navigator.appName.indexOf("Microsoft")!=-1)
		winH = document.body.offsetHeight;
	else
		winH = window.innerHeight;
	return winH;
}

Site.prototype.getClientWidth = function(){
	var winW=0;
	if (navigator.appName.indexOf("Microsoft")!=-1)
		winW = document.body.offsetWidth;
	else
		winW = window.innerWidth;

	return winW;
}


/**
* Set a content to the div 'noticeArea'
*/
Site.prototype.noticeAreaContent = function(content,timeOut){
	var noticeArea = $("#noticeArea");
	noticeArea.css({
	'position':'absolute',
	'top':getY()+100+'px',
	'left': '40%',
	'display':'block'
	});
	noticeArea.innerHTML = content + "<br/>" ;

    if(timeOut > 0){
	   self.setTimeout('$("#noticeArea").css({\'display\':\'none\'});$("noticeArea").innerHTML=""',timeOut);
    }
}



Site.prototype._stickOnTop = function(elementId){
	var clientHeight = this.getClientHeight();

	if (window.innerHeight)
		pos = window.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop)
		pos = document.documentElement.scrollTop;
	else if (document.body)
		pos = document.body.scrollTop

	if (pos < this._stickOnTop_theTop)
		pos = this._stickOnTop_theTop;
	else
		pos += this._stickOnTop_theTop;

	$("#"+elementId).css("top", pos+"px");
	//temp = setTimeout('site._stickOnTop("'+elementId+'")',100);
}

Site.prototype.shopAddShoppingCartItem = function(productTypeTitle,productId,price,productBundleId){
	if(!productBundleId) productBundleId = 0;
	$.ajax({
		data:{
			action:'shopAddShoppingCartItem',
			productTypeTitle:productTypeTitle,
			productId:productId,
			price:price,
			productBundleId:productBundleId
		}
	});

}

Site.prototype.shopUpdateShoppingCartItem = function(productTypeTitle,productId,quantity){
	$.ajax({
		data:{
			action : "shopUpdateShoppingCartItem",
			productTypeTitle : productTypeTitle,
			productId : productId,
			quantity : quantity
		}
	});

}

Site.prototype.shopProcessShoppingCartPayment = function(shipAddressId,billAddressId){
	$.ajax({
		data: {
			action : "shopProcessShoppingCartPayment",
			shipAddressId : shipAddressId,
			billAddressId : billAddressId
		}
	});

}

Site.prototype.userLogin = function(){
	$.ajax({
		data:{
			action:'userLogin',
			username: this._getValue("userLoginUsername") ,
			password: this._getValue("userLoginPassword") ,
			remember: ($("#userLoginRemember").attr("checked") ? 1 : 0)  ,
			return_action: this._getValue("userLoginReturnAction")
		}
	});

}

Site.prototype.userLogout = function(){
	$.ajax({
		data:{
			action:'userLogout'
			}
	});

}

Site.prototype.userSendLogin = function(){
	$.ajax({
		data : {
			action : "userSendLogin",
			username : this._getValue("userLoginAssistantUserName"),
			email : this._getValue("userLoginAssistantEmail")
		}
	});

}

Site.prototype.userUpdatePassword = function(){
	$.ajax({
		data : {
			action : "userUpdatePassword",
			existing_password : this._getValue("userExistingPassword"),
			password : this._getValue("userRetypePassword"),
			retype_password: this._getValue("userRetypePassword"),
			id : this._getValue("userId")
		}
	});

}

Site.prototype.miscProductGlscKoscCalculateRate = function(){
	if(this._getValue("productKosCountryRateId")=="0"){
		return;
	}
	$.ajax({
		data : {
			action : 'miscProductGlscKoscCalculateRate',
			from_product_kos_county_id : this._getValue("fromProductKosCountyId"),
			product_kos_country_rate_id : this._getValue("productKosCountryRateId")
		}
	});

}

Site.prototype.miscProductGlscKoscCalculateRate2 = function(){
	$.ajax({
		data : {
			action : "miscProductGlscKoscCalculateRate2",
			from_product_kos2_county_id : this._getValue("fromProductKos2CountyId"),
			product_kos2_country_rate_id : this._getValue("productKosCountryRateId")
		}
	});

}

Site.prototype.miscProductGlscKoscRefreshDestinationCallTypes2 = function(){
	$.ajax({
		data : {
			action : "miscProductGlscKoscRefreshDestinationCallTypes2",
			product_kos2_country_id : $("#toProductKos2CountyId").val()
		}
	});

}

Site.prototype.miscProductAdditionalFeatureCalculateRate = function(serviceType){
	$.ajax({
		data : {
			action : "miscProductAdditionalFeatureCalculateRate",
			callOrigin : this._getValue("callOrigin"),
			callDestination : this._getValue("callDestination"),
			serviceType : serviceType
		}
	});

}
Site.prototype.miscProductCbccCalculateRate = function(){
	$.ajax({
		data:{
			action:'miscProductCbccCalculateRate',
			callOrigin: this._getValue("callOrigin") ,
			callDestination: this._getValue("callDestination")
		}
	});

}



Site.prototype.miscProductAdditionalFeatureShowLoaderImage = function() {
	var RatesCalculatorResultBlock = $("#smallAdditionalFeatureRatesCalculatorResultBlock");
	RatesCalculatorResultBlock.html('<img src="/site/images/loader.gif" alt="Loading... please be patient..." title="Loading... please be patient..." />');
}

Site.prototype.miscProductGlscKoscRefreshDestinationCallTypes = function(){
	$.ajax({
		data : {
			action : "miscProductGlscKoscRefreshDestinationCallTypes",
			product_kos_country_id : $('#toProductKosCountyId').val()
		}
	});

}

Site.prototype.userCheckLogin = function(redirectToUrl){
	$.ajax({
			data:{
				action:'userCheckLogin',
				redirectToUrl:redirectToUrl
			}
		});

}
Site.prototype.miscProductCbccUserLogin = function(){
	$.ajax({
		data:{
			action:'miscProductCbccUserLogin',
			pin: this._getValue("pin")
		}
	});

}

Site.prototype.miscProductCbccCardTopUp = function(){
	$.ajax({
		data:{
			action:'miscProductCbccCardTopUp',
			pin: this._getValue("topUpPin")
		}
	});

}

Site.prototype.miscProductCbccUserLogout = function(){
	$.ajax({
		data : {
			action : "miscProductCbccUserLogout"
		}
	});

}

Site.prototype.userLogin = function(){
	$.ajax({
		data:{
			action:'userLogin',
			username: this._getValue("userLoginUsername") ,
			password: this._getValue("userLoginPassword") ,
			remember: ($("#userLoginRemember").attr("checked") ? 1 : 0)  ,
			return_action: this._getValue("userLoginReturnAction")
		}
	});

}

Site.prototype.miscProductCbccUserAddOrUpdateCallbackNumber = function(){
	$.ajax({
		data : {
			action : "miscProductCbccUserAddOrUpdateCallbackNumber",
			callback : this._getValue("callback")
		}
	});
}
Site.prototype.miscProductCbccUserAddOrUpdateEmail = function(){
	$.ajax({
		data:{
			action:'miscProductCbccUserAddOrUpdateEmail',
			email: this._getValue("email")
		}
	});

}


Site.prototype.miscSendMessageToCs = function(){
	$.ajax({
		data : {
			action : "miscSendMessageToCs",
			name : this._getValue("name"),
			email : this._getValue("email"),
			telephone : this._getValue("telephone"),
			message : this._getValue("message")
		}
	});

}

Site.prototype.userSudo = function(){
	$.ajax({
		data : {
			action : "userSudo",
			loginAsUsername : this._getValue("loginAsUsername")
		}
	});

}

var ajaxError = function(XMLHttpRequest, textStatus, errorThrown){
	alert(
		'Error occurred during interaction with the server, please report to the system administrator!\n'
		+XMLHttpRequest.responseText
	);
	return false;
};

var ajaxSuccess = function(xml){
	$(xml).find('ajax-response').children().each(function(){
		switch($(this).attr('type')){
			case 'element':
				$('#'+$(this).attr('id')).html($(this).text());
			break;
			case 'refresh':
				location.reload(true);
			break;
			case 'redirect':
				document.location =$(this).attr('value');
			break;
			case 'notice':
				$('#noticeArea').html($(this).attr('value'))
					.css({position:"absolute",top:String(getY()+10)+"px",left:String(getX())+"px",display:"block"})
					.animate({opacity: 1.0}, 3000)
					.fadeOut('slow',function(){
						$(this).hide();
					});
			break;
			case 'newWindow':
				self.window.open($(this).attr('value'));
			break;
			case 'back':
				self.window.history.go(-1);
			break;
			default:
				alert('No sepcial action assigned for callback, pelase check!');
			break;
		}
	})
};

$(function(){
	DEBUG = false;
	$.ajaxSetup({
		url:"/core/service.php?brand_name=site",
		type:"POST",
		dataType:"xml",
		global:true,
		error:ajaxError,
		success:ajaxSuccess
	});
	site = new Site();
	textareas = new Array();	//For tinyMCE
});