/**
* 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 Agent() {
	this.version = "1.0";
	this.requestBase = "/agentservice.php?platform_name=mars&brand_name=call28";

	ajaxEngine.registerRequest("agentService",this.requestBase);
}

Agent.prototype._getValue = function(elementId) {
	try{
		return $(elementId).value;
	}catch(e){
		alert(elementId+' is not valid!');
	}
}

Agent.prototype.chooseLocation = function(location){
	ajaxEngine.sendRequest("agentService",
		"action="+"chooseLocation",
		"country_code=" + location
	);
	return true;
}

Agent.prototype.pageInitial = function(){
	ajaxEngine.registerAjaxElement("noticeArea");
}

Agent.prototype.getClientHeight = function(){
	var winH = 0;
	if (navigator.appName.indexOf("Microsoft")!=-1)
		winH = document.body.offsetHeight;
	else
		winH = window.innerHeight;
	return winH;
}

Agent.prototype.getClientWidth = function(){
	var winW=0;
	if (navigator.appName.indexOf("Microsoft")!=-1)
		winW = document.body.offsetWidth;
	else
		winW = window.innerWidth;

	return winW;
}
/*
Agent.prototype.loaderShow =function(){
	$('loader').style.top=this.getClientHeight()-50+'px';
	$('loader').style.left = '0px'
	RicoUtil.toViewportPosition($('loader'));
	$('loader').style.display='block';
}

Agent.prototype.loaderHide = function(){
	$('loader').style.display='none';
}
*/

/**
* Set a content to the div 'noticeArea'
*/
Agent.prototype.noticeAreaContent = function(content,timeOut){
	var noticeArea = $("noticeArea");
	noticeArea.style.position = "absolute";
	noticeArea.style.top =  getY()+30+'px';
	noticeArea.style.left = "40%";
	noticeArea.style.display = "block";
	noticeArea.innerHTML = content + "<br/>" ;

    if(timeOut > 0){
	   self.setTimeout('$("noticeArea").style.display="none";$("noticeArea").innerHTML=""',timeOut);
    }
}

/**
* Display a message service response's type 'notice'
*/
Agent.prototype.notice = function(responseValue){
	agent.noticeAreaContent(responseValue, 5000);
}

/**
* Display a message while a service s loading
*/
Agent.prototype.load = function(content,timeOut){
	if(!content){
		content = 'We are processing your request...';
	}
	agent.noticeAreaContent(content,timeOut);
}

Agent.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).style.top = pos+'px';
	//temp = setTimeout('agent._stickOnTop("'+elementId+'")',100);
}

Agent.prototype.shopAddShoppingCartItem = function(productTypeTitle,productId,price,productBundleId){
	if(!productBundleId) productBundleId = 0;

	ajaxEngine.sendRequest("agentService",
		{parameters:"action="+"shopAddShoppingCartItem"
		+"&productTypeTitle=" + productTypeTitle
		+"&productId="+productId
		+"&price="+price
		+"&productBundleId="+productBundleId}
	);
}

Agent.prototype.shopProcessShoppingCartPayment = function(shipAddressId,billAddressId){
	ajaxEngine.sendRequest("agentService",
		{parameters:"action="+"shopProcessShoppingCartPayment"
		+"&shipAddressId=" + shipAddressId
		+"&billAddressId=" + billAddressId,
		onSuccess:this.loaderHide}
	);
}

Agent.prototype.userLogin = function(){
	ajaxEngine.sendRequest("agentService",
		"action="+"userLogin",

		"username="+this._getValue("userLoginUsername"),
		"password="+this._getValue("userLoginPassword"),
		"return_action="+this._getValue("userLoginReturnAction")
	);
}

Agent.prototype.userLogout = function(){
	ajaxEngine.sendRequest("agentService",
		{parameters:"action="+"userLogout"}
	);
}

Agent.prototype.userSendLogin = function(){
	ajaxEngine.sendRequest("agentService",
		"action="+"userSendLogin",

		"username="+this._getValue("userLoginAssistantUserName"),
		"email="+this._getValue("userLoginAssistantEmail")
	);
}

Agent.prototype.userUpdatePassword = function(){
	ajaxEngine.sendRequest("agentService",
		"action="+"userUpdatePassword",

		"existing_password="+this._getValue("userExistingPassword"),
		"password="+this._getValue("userPassword"),
		"retype_password="+this._getValue("userRetypePassword"),
		"id="+this._getValue("userId")
	);
}

Agent.prototype.miscProductGlscKoscCalculateRate = function(){
	ajaxEngine.sendRequest("agentService",
		"action="+"miscProductGlscKoscCalculateRate",
		"from_product_kos_county_id="+this._getValue("fromProductKosCountyId"),
		"product_kos_country_rate_id="+this._getValue("productKosCountryRateId")
	);
	agent.load(' ',1);
}

Agent.prototype.miscProductGlscKoscRefreshDestinationCallTypes = function(){
	var toProductKosCountyId = document.getElementById('toProductKosCountyId').value;
	ajaxEngine.sendRequest("agentService",
		"action="+"miscProductGlscKoscRefreshDestinationCallTypes",
		"product_kos_country_id="+toProductKosCountyId
	);
	agent.load(' ',1);
}

Agent.prototype.userCheckLogin = function(redirectToUrl){
	ajaxEngine.sendRequest("agentService",
		"action="+"userCheckLogin",
		"redirectToUrl="+redirectToUrl);
}

var agent = new Agent();