/**
* 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 Affiliate() {}

Affiliate.prototype._getValue = function(elementId) {
	try{
		return $('#'+elementId).val();
	}catch(e){
		alert(elementId+' is not valid!');
	}
}

Affiliate.prototype.getClientHeight = function(){
	var winH = 0;
	if (navigator.appName.indexOf("Microsoft")!=-1)
		winH = document.body.offsetHeight;
	else
		winH = window.innerHeight;
	return winH;
}

Affiliate.prototype.getClientWidth = function(){
	var winW=0;
	if (navigator.appName.indexOf("Microsoft")!=-1)
		winW = document.body.offsetWidth;
	else
		winW = window.innerWidth;

	return winW;
}
/*
Affiliate.prototype.loaderShow =function(){
	$('loader').style.top=this.getClientHeight()-50+'px';
	$('loader').style.left = '0px'
	RicoUtil.toViewportPosition($('loader'));
	$('loader').style.display='block';
}

Affiliate.prototype.loaderHide = function(){
	$('loader').style.display='none';
}
*/

/**
* Set a content to the div 'noticeArea'
*/

Affiliate.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);
    }
}
/**
* Display a message service response's type 'notice'
*/

Affiliate.prototype.notice = function(responseValue){
	affiliate.noticeAreaContent(responseValue, 5000);
}
/**
* Display a message while a service s loading
*/

Affiliate.prototype.load = function(content,timeOut){
	if(!content){
		content = 'We are processing your request...';
	}
	affiliate.noticeAreaContent(content,timeOut);
}

Affiliate.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('affiliate._stickOnTop("'+elementId+'")',100);
}

Affiliate.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")
		}
	});
}

Affiliate.prototype.userLogout = function(){
	$.ajax({
		data : {
			action:"userLogout"
			}
	});
}
Affiliate.prototype.userSendLogin = function(){
	$.ajax({
		data : {
			action : "userSendLogin",
			username : this._getValue("userLoginAssistantUserName"),
			email : this._getValue("userLoginAssistantEmail")
		}
	});
}

Affiliate.prototype.userUpdatePassword = function(){
	$.ajax({
		data : {
			action : "userUpdatePassword",
			existing_password : this._getValue("userExistingPassword"),
			password : this._getValue("userPassword"),
			retype_password:this._getValue("userRetypePassword"),
			id : this._getValue("userId")
		}
	});
	site.load(' ',1);
}

Affiliate.prototype.userCheckLogin = function(redirectToUrl){
	$.ajax({
			data:{
				action:'userCheckLogin',
				redirectToUrl:redirectToUrl
			}
		});
}

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);modified by arno 2009-4-8
				location.href=location.href;
			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=affiliate",
		type:"POST",
		dataType:"xml",
		global:true,
		error:ajaxError,
		success:ajaxSuccess
	});
	affiliate = new Affiliate();
	textareas = new Array();	//For tinyMCE
});
