// JavaScript Document

var MAIN_PATH_HTTP = "http://www.casadelgiocattolo.com/";

Array.prototype.indexOf = function(obj){
	for(var i=0; i<this.length; i++){
		if(this[i]==obj){
			return i;
		}
	}
	return -1;
}


Array.prototype.in_array = function(needle){	
	for(var i=0; i<this.length; i++){
		if(this[i]==needle){
			return true;
		}
	}
	return false;
}






// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// ::::::::::::::::::::::::::             function general ajax             :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function generalAjax(filePHP,data) {
// ajax - filePHP = file php da richiamare (incluso path) - data = dati da inviare a seconda del php richiamato (variabile=valore)
    this.filePHP = filePHP;
    this.data = data;
  
    if (window.XMLHttpRequest) { 
      this.http_request = new XMLHttpRequest();
      if (this.http_request.overrideMimeType) {
        this.http_request.overrideMimeType('text/xml');
      }
    } else if (window.ActiveXObject) { // IE
      try {
        this.http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
        try {
          this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
      }
    }
    // LANCIA RICHIESTA...
    this.http_request.open('POST', this.filePHP, true);
    this.http_request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    this.http_request.send(this.data); 
}



// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// ::::::::::::::::::::::::::             function  show  tab login user                :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function showTabLoginUser(){
	
	var tabLogin = document.getElementById('login_tab');
	
	tabLogin.style.display = "block";
	
}




// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// ::::::::::::::::::::::::::             function  hide  tab login user                ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

function hideTabLoginUser(event){
	
	var tabLogin = document.getElementById('login_tab');
	
	var ievs = (/MSIE (\d+\.\d+);/.test(navigator.userAgent));
	
	if(ievs){
			if((event.toElement.id!="login_tab")&&(event.toElement.id!="boxLogin")&&(event.toElement.parentNode.id!="login_tab")&&(event.toElement.parentNode.id!="content_tab_login")){
					tabLogin.style.display = "none";
			} 	

	} else {
			if((event.relatedTarget.id!="login_tab")&&(event.relatedTarget.id!="boxLogin")&&(event.relatedTarget.parentNode.id!="login_tab")&&(event.relatedTarget.parentNode.id!="content_tab_login")){ 
					tabLogin.style.display = "none";
			}
	}
	
}


// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// ::::::::::::::::::::::::::             function  login user                :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function loginUser(){
	

	var username = document.getElementById('username_tab_login').value.replace(/\s+$|^\s+/g,"");
	var password = document.getElementById('password_tab_login').value.replace(/\s+$|^\s+/g,"");
	
	if((username != "")&&(password != "")){		
			
			var loginCostumer = new generalAjax(MAIN_PATH_HTTP+"modules/ajax_login_user.php","username="+username+"&password="+password+"");
							
			loginCostumer.http_request.onreadystatechange = function(){
					if (loginCostumer.http_request.readyState == 4) {
						  if (loginCostumer.http_request.status == 200) {	
								var result = loginCostumer.http_request.responseText;
								if(result == 1){
									alert("Login effettuato correttamente!");
									location.reload();
								} else {
									alert("Attenzione dati di login errati!");	
								}								
						  }
					}
			}
					
	} else {
		alert("Attenzione per effettuare il login devi completare entrambi i campi!");	
	}
	
}





// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// ::::::::::::::::::::::::::             function  show  tab account user                :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function showTabAccountUser(){
	
	var tabAccount = document.getElementById('account_tab');
	
	tabAccount.style.display = "block";
	
}




// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// ::::::::::::::::::::::::::             function  hide  tab account user                ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

function hideTabAccountUser(event){
	
	var tabAccount = document.getElementById('account_tab');
	
	var ievs = (/MSIE (\d+\.\d+);/.test(navigator.userAgent));
	
	if(ievs){
			if((event.toElement.id!="account_tab")&&(event.toElement.id!="boxAccount")&&(event.toElement.parentNode.id!="account_tab")&&(event.toElement.parentNode.id!="content_tab_account")&&(event.toElement.parentNode.id!="link_profile")){
					tabAccount.style.display = "none";
			} 	

	} else {
			if((event.relatedTarget.id!="account_tab")&&(event.relatedTarget.id!="boxAccount")&&(event.relatedTarget.parentNode.id!="account_tab")&&(event.relatedTarget.parentNode.id!="content_tab_account")&&(event.relatedTarget.parentNode.id!="link_profile")){ 
					tabAccount.style.display = "none";
			}
	}
	
}



// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// ::::::::::::::::::::::::::             function  login user                :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function logoutUser(){	
		
	var logoutCostumer = new generalAjax(MAIN_PATH_HTTP+"modules/ajax_logout_user.php","");
					
	logoutCostumer.http_request.onreadystatechange = function(){
			if (logoutCostumer.http_request.readyState == 4) {
				  if (logoutCostumer.http_request.status == 200) {	
						var result = logoutCostumer.http_request.responseText;
							location.reload();							
				  }
			}
	}
}







// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// ::::::::::::::::::::::::::::        function data control generic field form       :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function controlGenericField(obj, required){
	var value = obj.value.replace(/\s+$|^\s+/g,"");
	if((value == "")&&(required == 1)){
		obj.className = "inputfieldsRegisterUserError";		
		return "error";
	} else {
		obj.className = "inputfieldsRegisterUser";	
		return value;
	}
}



// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// ::::::::::::::::::::::::::::        function control e-mail field form       :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function controlEmailField(obj){
	var value = obj.value.replace(/\s+$|^\s+/g,"");
	if((value == "")||(!value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/))){
		obj.className = "inputfieldsRegisterUserError";		
		return false;
	} else {
		obj.className = "inputfieldsRegisterUser";	
		return value;
	}
}





// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// ::::::::::::::::::::::::::             function  control new user info from account               :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

function controlNewInfoUserAccount(){
	
	var arrayData = new Array();
	var addID = "";
	
	arrayData[0] = controlGenericField(document.getElementById('name'+addID),1);
	arrayData[1] = controlGenericField(document.getElementById('surname'+addID),1);
	arrayData[2] = controlGenericField(document.getElementById('address'+addID),1);
	arrayData[3] = controlGenericField(document.getElementById('city'+addID),1);
	arrayData[4] = controlGenericField(document.getElementById('province'+addID),1);
	arrayData[5] = controlGenericField(document.getElementById('cap'+addID),1);
	arrayData[6] = controlGenericField(document.getElementById('country'+addID),1);
	arrayData[7] = controlEmailField(document.getElementById('e_mail'+addID));
	arrayData[8] = controlGenericField(document.getElementById('tel'+addID),1);
	arrayData[9] = controlGenericField(document.getElementById('fax'+addID),0);
	arrayData[10] = controlGenericField(document.getElementById('company_name'+addID),0);
	arrayData[11] = controlGenericField(document.getElementById('piva'+addID),0);
	arrayData[12] = controlGenericField(document.getElementById('password'+addID),1);
	
	
	
	if(arrayData.in_array("error")){
		document.getElementById('boxErrorOnFormAccount').style.display = "block";
		return;
	} else {
		document.getElementById('boxErrorOnFormAccount').style.display = "none";
		document.getElementById('FormNewUserInfo').submit();
	}
	
}






// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// ::::::::::::::::::::::::::::        Classe che controlla lo scorrimento degli altri prodotti in categoria          ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

function ClassScrollProdInCat(nTot,IDelement){
	
	var obj = document.getElementById(IDelement);
	var nElements = nTot;
	var nStep = Math.ceil(nTot/10);
	var actualPos = 0;
	var idLeft = null;
	var idRight = null;
	var objRowLeft = document.getElementById('leftRowOtherProdInCat');
	var objRowRight = document.getElementById('rightRowOtherProdInCat');
	objRowRight.style.display = "block";
	
	
	// -----------------------------------------------------------------------------------------------------------------
	// ------------------           scroll left            ----------------------------------------------------------------------
	this.left = function(){	
			objRowLeft.style.display = "block";
			if(idRight != null){
				clearTimeout(idRight);
			}
			actualPos = actualPos+1;
			var newPos = actualPos*800;
			if(newPos > (obj.offsetWidth-800)){
				newPos = obj.offsetWidth-800;
				objRowRight.style.display = "none";
			}
			if(actualPos>nStep){
				actualPos = nStep;
			}
			
			for(var a=0; a<nStep; a++){
				document.getElementById('changeStepProdInCat_'+a).style.backgroundColor = "#fff";	
			}			
			document.getElementById('changeStepProdInCat_'+actualPos).style.backgroundColor = "#f00";			
			
			leftScroll(-newPos);
	}
	
	// -----------------------------------------------------------------------------------------------------------------
	// ------------------           scroll right            ---------------------------------------------------------------------
	this.right = function(){	
			objRowRight.style.display = "block";
			if(idLeft != null){
				clearTimeout(idLeft);
			}
			actualPos = actualPos-1;
			var newPos = actualPos*800;
			if(newPos < 0){
				newPos = 0;
				actualPos = 0;				
			}
			if(actualPos == 0){
				objRowLeft.style.display = "none";
			}
			
			for(var a=0; a<nStep; a++){
				document.getElementById('changeStepProdInCat_'+a).style.backgroundColor = "#fff";	
			}			
			document.getElementById('changeStepProdInCat_'+actualPos).style.backgroundColor = "#f00";
			
			rightScroll(-newPos);
	}
	
	// -----------------------------------------------------------------------------------------------------------------
	// ------------------           functions scrolling           ---------------------------------------------------------------
	var leftScroll = function(pos){
		if(obj.offsetLeft > pos){			
			obj.style.marginLeft = obj.offsetLeft-20+"px";
			idLeft = setTimeout(function(){ leftScroll(pos); },1);
		}			
	}
	
	var rightScroll = function(pos){
		if(obj.offsetLeft < pos){			
			obj.style.marginLeft = obj.offsetLeft+20+"px";
			idRight = setTimeout(function(){ rightScroll(pos); },1);
		}			
	}
	
	
	// -----------------------------------------------------------------------------------------------------------------
	// ------------------           change step            -------------------------------------------------------------------
	this.changeStep = function(n){
		for(var a=0; a<nStep; a++){
			document.getElementById('changeStepProdInCat_'+a).style.backgroundColor = "#fff";	
		}
		
		document.getElementById('changeStepProdInCat_'+n).style.backgroundColor = "#f00";
		
		var difference = actualPos-n;
		var kindChange = "positive";
		if(difference < 0){
			kindChange = "negative";
		}
		difference = Math.abs(difference);
		for(var Y=0; Y<difference; Y++){
			if(kindChange == "positive"){
				this.right();
			} else {
				this.left();
			}
		}		
	}
	
	
	// -----------------------------------------------------------------------------------------------------------------
	// ------------------           hover step            ---------------------------------------------------------------------
	this.hoverStep = function(n){
		document.getElementById('changeStepProdInCat_'+n).style.backgroundColor='#a2cf5a';
	}
	
	
	// -----------------------------------------------------------------------------------------------------------------
	// ------------------           out step            -----------------------------------------------------------------------
	this.outStep = function(n){
		if(n == actualPos){
			document.getElementById('changeStepProdInCat_'+n).style.backgroundColor='#f00';
		} else {
			document.getElementById('changeStepProdInCat_'+n).style.backgroundColor='#fff';	
		}
	}
}


// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// ::::::::::::::::::::::::::::        function register new  user       :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

function registerNewUser(){	
	var addID = "";	
	var arrayData = new Array();
	
	arrayData[0] = controlGenericField(document.getElementById('name'+addID),1);
	arrayData[1] = controlGenericField(document.getElementById('surname'+addID),1);
	arrayData[2] = controlGenericField(document.getElementById('address'+addID),1);
	arrayData[3] = controlGenericField(document.getElementById('city'+addID),1);
	arrayData[4] = controlGenericField(document.getElementById('province'+addID),1);
	arrayData[5] = controlGenericField(document.getElementById('cap'+addID),1);
	arrayData[6] = controlGenericField(document.getElementById('country'+addID),1);
	arrayData[7] = controlEmailField(document.getElementById('e_mail'+addID));
	arrayData[8] = controlGenericField(document.getElementById('tel'+addID),1);
	arrayData[9] = controlGenericField(document.getElementById('fax'+addID),0);
	arrayData[10] = controlGenericField(document.getElementById('company_name'+addID),0);
	arrayData[11] = controlGenericField(document.getElementById('piva'+addID),0);
	arrayData[12] = controlGenericField(document.getElementById('password'+addID),1);

	if(arrayData.in_array("error")){
		document.getElementById('boxErrorOnFormNewUser').style.display = "block";
		return;
	} else {
		document.getElementById('boxErrorOnFormNewUser').style.display = "none";		
		
	registerNewUserProcedure(arrayData.join(" ## "));	
		
	}
}




// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// ::::::::::::::::::::::::::::        function register new  user       :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function registerNewUserProcedure(arrayString){
	
	//waitingUpdatingCart();
	
	var registerUser = new asyncCart(MAIN_PATH_HTTP+"modules/ajax_register_user.php","arrayString="+arrayString+"");
	
	registerUser.http_request.onreadystatechange = function(){
			if (registerUser.http_request.readyState == 4) {
				  if (registerUser.http_request.status == 200) {	
				  
						var result = registerUser.http_request.responseText;
						
						if(result == "OK"){							
								// ------  nuovo utente prevedo anche il login automatico appena finita la registrazione  ------------------
								var username = document.getElementById('e_mail').value.replace(/\s+$|^\s+/g,"");
								var password = document.getElementById('password').value.replace(/\s+$|^\s+/g,"");
								
								var loginCostumer = new generalAjax(MAIN_PATH_HTTP+"modules/ajax_login_user.php","username="+username+"&password="+password+"");								
								loginCostumer.http_request.onreadystatechange = function(){
										if (loginCostumer.http_request.readyState == 4) {
											  if (loginCostumer.http_request.status == 200) {	
													var result = loginCostumer.http_request.responseText;
													if(result == 1){
														//hidePopupWaitingUpdate();
														alert("Registrazione e login effettuati con successo!");
														//document.getElementById('boxLogin').style.display = "none";
														//document.getElementById('boxAccount').style.display = "block";
														//changeStepCart(3);
														location.assign(MAIN_PATH_HTTP);
													} 															
											  }
										}
								}	
								// -----------------------------------------------------------------------------------------------------							
						} else {
							//hidePopupWaitingUpdate();
							alert("Attenzione l'email \u00E9 gi\u00E1 utilizzata");	
						}
	
				  }
			}
	}
	
}




