
/*
* Log out the user
*/
function logout() {
	$('btns').innerHTML = "Loging out...";
	new Ajax.Updater('btns', 'script/logout', {asynchronous:true, evalScripts:true});
}

/*
* Edit the user's profil
*/
function profilEdit() {
	//$('content').style.display = "none";
	new Ajax.Updater('content', 'script/profilEdit', {onComplete:function(){ new Effect.Appear('content');}, asynchronous:true, evalScripts:true});
}

/*
* Save the user's profil after form verification
* @param Form serialization
*/
function profilSave() {
	
	var msg = '';
	var targetElements = new Array();

	//pro account's check
	if($('accountType').value=="pro") {
		if($('corporation').value=="") { 
			msg += "&#8226; Your company name is required.<br>";
			targetElements[targetElements.length] = 'corporation';
		}
	}
	if($('email').value==""||($('email').value.indexOf("@")<0||($('email').value.indexOf(".")<0))) {
		msg += "&#8226; Enter a valid e-mail address.<br>";
		targetElements[targetElements.length] = 'email';
	}
	if($('mail1').value=="") {
		msg += "&#8226; Address is required.<br>";
		targetElements[targetElements.length] = 'mail1';
	}
	if($('city').value=="") {
		msg += "&#8226; City is required.<br>";
		targetElements[targetElements.length] = 'city';
	}
	if($('country').value=="") {
		msg += "&#8226; Country is required. Select your country.<br>";
		targetElements[targetElements.length] = 'country';
	}
	if(targetElements.length>0) { 
		$('alertMsg').innerHTML = msg;
		new Effect.Appear('alertMsg');
		for(i=0;i<targetElements.length;i++) {
			new Effect.Highlight($(targetElements[i]), {duration:1.5, startcolor:'#FFFFFF', endcolor:'#FF6600'});
		}
	} else {
		new Ajax.Updater('alertMsg', 'script/profilSave', {onComplete:function(){ new Effect.Appear('alertMsg');}, parameters:Form.serialize($('formProfil')) ,asynchronous:true, evalScripts:true});
	}
}


/*
* Save the user's profil after form verification
* @param Form serialization
*/
function profilConfirm() {
	
	var msg = '';
	var targetElements = new Array();

	if($('email').value==""||($('email').value.indexOf("@")<0||($('email').value.indexOf(".")<0))) {
		msg += "&#8226; Your company name is required.<br>";
		targetElements[targetElements.length] = 'email';
	}
	if($('mail1').value=="") {
		msg += "&#8226; Address is required.<br>";
		targetElements[targetElements.length] = 'mail1';
	}
	if($('city').value=="") {
		msg += "&#8226; City is required.<br>";
		targetElements[targetElements.length] = 'city';
	}
	if($('country').value==""||$('country').value==0) {
		msg += "&#8226; Country is required. Select your country.<br>";
		targetElements[targetElements.length] = 'country';
	}
	if(targetElements.length>0) { 
		$('alertMsg').innerHTML = msg;
		new Effect.Appear('alertMsg');
		for(i=0;i<targetElements.length;i++) {
			new Effect.Highlight($(targetElements[i]), {duration:1.5, startcolor:'#FFFFFF', endcolor:'#FF6600'});
		}
	} else {
		new Ajax.Updater('alertMsg', 'script/mailSave', {onComplete:function(){ new Effect.Appear('alertMsg');}, parameters:Form.serialize($('formMail')) ,asynchronous:true, evalScripts:true});
	}
}

/*
* Select a product (index)
* @param productId
*/
function purchaseSelect(productId, price) {
	new Ajax.Updater('productChoice', 'script/my-selection', {parameters:"&code="+productId+"&price="+price ,asynchronous:true, evalScripts:true});
}

/*
* Confirm the selection (my-selection.html)
* @param productId
*/
function purchaseConfirm() {
	//retrieve purchase's informations
	var productsNb = 0;
	var URLVars = "&";
	for(i=0,j=1;i<document.products.length;i++) {
		if(eval("document.purchase.chk"+document.products[i]).checked==true) {
			productsNb++;
			URLVars += "&prod"+j+"="+document.products[i]+"&quantity"+j+"="+$(document.products[i]+"nb").innerHTML+"&price"+j+"="+document.prices[i];
			j++;
		}
	}
	URLVars += "&productsNb="+productsNb;
	//process the page loading
	new Ajax.Updater('alertMsg', 'script/payment', {parameters:URLVars, asynchronous:true, evalScripts:true});
}

/*
* set quantities (my-selection.html)
*/
function count(planIndex, unitPrice, operation) {

	currentVal = Number($('#productDetails_'+planIndex+'_quantity').val());
	newVal = currentVal + Number(operation);
	
	if(newVal < 0)
		newVal = 0;
	
	$('#productDetails_'+planIndex+'_quantity').val(newVal);
	$('#plan'+planIndex).html(newVal);
	
	currentPriceSelection = Number($('#productDetails_'+planIndex+'_price_selection').val());
	newPriceSelection = currentPriceSelection + unitPrice*operation;
	if(newPriceSelection < 0)
		newPriceSelection = 0;
	
	$('#productDetails_'+planIndex+'_price_selection').val(newPriceSelection);

	$('#countTotal').html(getTotal());
}

function getTotal() {
	total = 0;
	$("input[id*='price_selection']").each( function() { total += Number(this.value); } );
	return total;
}
/*
* Edit the user's informations
*/
function mailEdit() {
	//$('usrMail').style.display = "none";
	//new Ajax.Updater('usrMail', 'userInformationsFrSuccess.php', {onComplete:function(){ new Effect.Appear('usrMail');}, asynchronous:true, evalScripts:true});
	
	
	/*$j.ajax(
	{
		type: 'POST',
		processData: true,
		url: '/user/userInformationsFrSuccess.php',
		data: 'var1=val1&var2=val2',
		dataType: ‘php’,
		success: function(data)
		{
		//alert(data);
		$j(’#identifiantConteneur’).html(data);
		}
	}); */
}

/*
* Save the user's informations
*/
function mailSave() {
	var msg = '';
	var targetElements = new Array();

	if($('mail1').value=="") {
		msg += "&#8226; Address is required.<br>";
		targetElements[targetElements.length] = 'mail1';
	}
	if($('city').value=="") {
		msg += "&#8226; City is required.<br>";
		targetElements[targetElements.length] = 'city';
	}
	if($('country').value==0) {
		msg += "&#8226; Country is required. Select your country.<br>";
		targetElements[targetElements.length] = 'country';
	}
	if($('email').value=="") {
		msg += "&#8226; Email address is required.<br>";
		targetElements[targetElements.length] = 'email';
	}
	if(targetElements.length>0) { 
		$('alertMsg').innerHTML = msg;
		new Effect.Appear('alertMsg');
		for(i=0;i<targetElements.length;i++) {
			new Effect.Highlight($(targetElements[i]), {duration:1.5, startcolor:'#FFFFFF', endcolor:'#FF6600'});
		}
	} else {
		$('usrMail').style.display = "none";
		new Ajax.Updater('usrMail', 'script/mailSave', {parameters:Form.serialize($('formMail')),onComplete:function(){ new Effect.Appear('usrMail');}, asynchronous:true, evalScripts:true});
	}
}

/*
* Display the whole bill history (index)
*/
function histShowAll() {
	$('billHistoryTable').innerHTML = "Searching...";
	new Ajax.Updater('billHistoryTable', 'script/histShowAll', {asynchronous:true, evalScripts:true});
	new Effect.Fade($('btnHistory'));
}

/*
* Check software serial number
*/
function checkSerial() {
	if($('#serialNeeded').val(1)) 
	    {
		  var s = $('#serial1').val() + $('#serial2').val() + $('#serial3').val() + $('#serial4').value + $('#serial5').val();
		if(s.length<20) {
			alert("Wrong software serial number.\nPlease check that your entered the correct serial number in the form.");
		} else {
			$.ajax
			({
				type: 'POST',
				processData: true,
				url: '@MoneyOrder',
				data: '"serial="+s&asynchronous:true&evalScripts:true',
				dataType: 'html',
				success: function(data){
				    $j('#identifiantConteneur').html(data);
				}
			});
				
		} 
		
			//new Ajax.Updater('alertMsg', './serialSave', {parameters:"serial="+s, asynchronous:true, evalScripts:true});

		
	} else {
		alert("sendBill();");
		sendBill();
	}
}

/*
* payement validation, then send user to paypal or bank account identification
*/
function sendBill() {
	
	if($('#paymentChoice').val()=='creditCard') {
		
		document.paymentType.submit();	
	} 
	else 
	{
		document.location.href="moneyOrder";
	}
}

/*
* check out user's information (money-order.html)
*/
function checkoutUsrInfo() {
	var required = $('required').value.split("|");
	var msg = "";
	var targetElements = new Array();
	for(i=0;i<required.length;i++) {
		if(required[i]=="corporation" && $(required[i]).value=="") {
			msg += "Corporation name required<br>";
			targetElements[targetElements.length] = required[i];
		} 
		else if(required[i]=="siret" && $(required[i]).value=="") {
			msg += "EIN or SIRET required<br>";
			targetElements[targetElements.length] = required[i];
		}
		else if(required[i]=="numTVA" && $(required[i]).value=="") {
			msg += "VAT number required<br>";
			targetElements[targetElements.length] = required[i];
		}
		else if(required[i]=="country" && $(required[i]).value=="") {
			msg += "Country required<br>";
			targetElements[targetElements.length] = required[i];
		}
	}
	if(targetElements.length>0) {
		$('alertMsg').innerHTML = msg;
		new Effect.Appear('alertMsg');
		for(i=0;i<targetElements.length;i++) {
			new Effect.Highlight($(targetElements[i]), {duration:1.5, startcolor:'#FFFFFF', endcolor:'#FF6600'});
		}
	} else {
		new Ajax.Updater('formCheckout', 'script/checkoutSave', {onComplete:function(){ new Effect.Appear('content');},parameters:Form.serialize($('formCheckout')), asynchronous:true, evalScripts:true});
	}
}
/*
* Animation for the 'login' form
*/
function bounceAnimation(element) {
	element.css({"margin-left": "60px"})
    element.animate({"margin-left": "-=60px"}, 120);
	element.animate({"margin-left": "+=45px"}, 100);
	element.animate({"margin-left": "-=45px"}, 90);
	element.animate({"margin-left": "+=30px"}, 70);
	element.animate({"margin-left": "-=30px"}, 60);
	element.animate({"margin-left": "+=15px"}, 40);
	element.animate({"margin-left": "-=15px"}, 30);
}

function sendEmail(id){		new Ajax.Updater('alertMsg', 'script/sendEmail', {onComplete:function(){ new Effect.Appear('alertMsg');}, parameters: {usrId:id},asynchronous:true, evalScripts:true});}

