﻿var qta
var euro

function salvataggio_dati(prodo, lng, sid, clien, cod_a){
	var size, prezzoKg, prodo_conf, MP, AG, PD, instant, data, prodo, magg;
	//size = document.getElementById(prodo+"_size").value;
	qta = document.getElementById(prodo+"_qta").value;
	MP = document.getElementById(prodo+"_prezzoMP").value;
	AG = document.getElementById(prodo+"_AG").value;
	PD = document.getElementById(prodo+"_PD").value;
	magg = document.getElementById(prodo+"_magg").value;
	instant = document.getElementById(prodo+"_instant").value;
	data = document.getElementById(prodo+"_data").value;
	prodo_conf=prodo+"_"+size;
	prezzoKg = document.getElementById(prodo+"_prezzo").value;
	if (qta!="") {
		qta = qta.replace(",",".");
		if (isNaN(qta))
			alert("Inserire un valore numerico");
		else {
			//resto = qta % (size/1000);
			//if (resto == 0 && qta!=0 )
				window.location = "putInBasket.asp?PG_sid="+sid+"&PG_lng="+lng+"&pr="+escape(prodo)+"&qta="+qta.replace(".",",")+"&prezzo="+prezzoKg+"&MP="+MP+"&AG="+AG+"&PD="+PD+"&magg="+magg+"&instant="+instant+"&data="+data+"&cl="+clien+"&cod_a="+cod_a;
			//else
			//	alert('Please insert the right quantity for the packaging chosen');
		}
	}
	else
		alert('Please insert quantity');
}

function confirm_remove(cd_prodo, prodo, lng, sid){
	flag = confirm("Do you want to delete "+prodo+" from your basket?"); 
	if (flag==true) window.location = 'removeBasket.asp?PG_sid='+sid+'&PG_lng='+lng+'&pr='+cd_prodo;
}

function go_search(lng, sid){
	window.location = 'search.asp?PG_sid='+sid+'&PG_lng='+lng;
}

function go_search_det(lng, sid, f, np, my, page, ricerca){
	window.location = 'search.asp?PG_sid='+sid+'&PG_lng='+lng+'&f='+f+'&np='+np+'&my='+my+'&page='+page+'&ricerca='+ricerca;
}

function confirm_shop(lng, sid, login){
	window.location = 'http://www.progold.com/eportal/modules/PG_Eshop/pre_eshop.asp?login='+login+'&PG_sid='+sid+'&PG_lng='+lng;
}

function go_orders(lng, sid, f, np, my, page, ricerca){
	window.location = 'myorders.asp?PG_sid='+sid+'&PG_lng='+lng+'&f='+f+'&np='+np+'&my='+my+'&page='+page+'&ricerca='+ricerca;
}

function go_open_order(lng, sid, f, np, my, page, ricerca, c, cc, anno){
	window.location = 'myorders_open.asp?c='+c+'&cc='+cc+'&a='+anno+'&PG_sid='+sid+'&PG_lng='+lng+'&f='+f+'&np='+np+'&my='+my+'&page='+page+'&ricerca='+ricerca;
}

function go_open_order_pdf(cl, cc, c){
	var wnd=window.open('http://www.progold.com/storage/'+cl+'/OrderConfirmation_'+cc+'_'+c+'.pdf', '', 'width=830, height=480, resizable=yes, status=no, scrollbars=yes, location=no');
}

function go_myitems(lng, sid){
	window.location = 'search.asp?my=1&PG_sid='+sid+'&PG_lng='+lng;
}

function go_myaccount(lng, sid, f, np, my, page, ricerca){
	window.location = 'myaccount.asp?PG_sid='+sid+'&PG_lng='+lng+'&f='+f+'&np='+np+'&my='+my+'&page='+page+'&ricerca='+ricerca;
}

function go_portal(){
	window.location = 'http://www.progold.com/PG_portal/main.asp';
}

function logout(lng, sid){
	window.location = 'logout.asp?PG_lng='+lng+'&PG_sid='+sid;
}

function checkFields(lingua) {
	msg='Please, insert both login and password';
	ch_login=document.getElementById('login').value;
	ch_pw=document.getElementById('password').value;
	if ((ch_login=='') || (ch_pw=='')) 
		alert(msg);
	else
		frmLogin.submit();
}
var ricerca_price;
function calc_price(art, op, cl) {
	var size, qta, prodo, om;
	if (op==1)
		qta = document.getElementById(art&'_qta').value;
	else
		qta=0;
	if (qta=='')
		qta=0;
	prodo = art;
	//cl = document.getElementById('cliente').value;
	if (window.XMLHttpRequest) {
		ricerca_price = new XMLHttpRequest();
		ricerca_price.onreadystatechange = ricevi_price;
		ricerca_price.open('GET', 'popola_price.asp?cl='+cl+'&i=1&p='+prodo+'&q='+qta+'&o=0',true);
		ricerca_price.send(null);
		// per IE
	} 
	else if (window.ActiveXObject) {
		ricerca_price = new ActiveXObject("Microsoft.XMLHTTP");
		if (ricerca_price) {
			ricerca_price.onreadystatechange = ricevi_price;
			ricerca_price.open('GET', 'popola_price.asp?cl='+cl+'&i=1&p='+prodo+'&q='+qta+'&o=0',true);
			ricerca_price.send();
		}
    }
}

function ricevi_price() {
	var strRes;
	var arrValori;
	var articolo;
	if (ricerca_price.readyState == 4) {
		strRes=ricerca_price.responseText;
		arrValori=strRes.split("|");
		articolo=arrValori[0].split("|");
		document.getElementById(articolo+"_prezzo").value = arrValori[1].split("|");
		document.getElementById(articolo+"_prezzoMP").value = arrValori[2].split("|");
		document.getElementById(articolo+"_instant").value = arrValori[3].split("|");
		document.getElementById(articolo+"_AG").value = arrValori[4].split("|");
		document.getElementById(articolo+"_PD").value = arrValori[5].split("|");
		document.getElementById(articolo+"_viewprezzo").innerHTML = arrValori[1].split("|") + " &euro;/kg";
		if (arrValori[1].split("|")==0) {
			document.getElementById(articolo+"_noPurchase").style.visibility = "visible";
			document.getElementById(articolo+"_Purchase").style.visibility = "hidden";
		}
		else{
			document.getElementById(articolo+"_noPurchase").style.visibility = "hidden";
			document.getElementById(articolo+"_Purchase").style.visibility = "visible";
		}
    }
}

function rtrim(what)
{
	var i;
	
	i= what.lastIndexOf(' ');
	while ((i==(what.length-1)) && (what.length>0))
	{
		what= what.substr(0,(what.length-1));
		i= what.lastIndexOf(' ');
	}
	return what;
}

function ltrim(what)
{
	var i;
	i= what.indexOf(' ');
	while ((i==0) && (what.length>0))
	{
		what= what.substr(1);
		i= what.indexOf(' ');
	}
	return what;
}

function trim(what)
{
	what= rtrim(what);
	what= ltrim(what);
	return what;
}

function SaveChanges()
{
	if (Check())
	{
		formLogin.submit();
	}
}

function Check()
{
	var valpassword;
	var valLogin;
	
	valLogin = trim(document.getElementById('login').value);
	valpassword = trim(document.getElementById('Password').value);
	
	if (valLogin=='')
	{
		alert('Please, insert a valid login.');
		document.getElementById('login').focus();
		return false;
	}
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if (((document.getElementById('Password').value)!='') && ((document.getElementById('CPassword').value)!='')) {
		if (trim(valpassword).length<6)
		{
			alert('Invalid password. Password must be at least 6 characters long.');
			return false;
		}
		if (!valpassword.match(alphaExp))
		{
			alert('Invalid password. Password must contain alfanumeric characters.');
			return false;
		}
	}
	if (document.getElementById('Password').value!= document.getElementById('CPassword').value)
	{
		alert('Passwords do not match.');
		document.getElementById('CPassword').focus();
		return false;
	}
		
	return true;
}

function submit_NoCC(){
	processNoCC.submit();
}

function submit_CC(){
	paymentCC.submit();
}

function submit_BP(){
	paymentBP.submit();
}

function openPassword(lng){
	window.open('http://www.progold.com/eportal/Forgot_Password.asp?PG_lng='+lng, null, 'height=196,width=350')
}
