﻿//will use the following functions from a mother js
/*
function popup(content,onoff)
{
	var classname = onoff==1?"popupDivON":"popupDivOFF";
	document.getElementById("popupDiv").innerHTML = content;
	document.getElementById("popupDiv").className = classname;
}

function fillContent(cont,append)
{
		try{
		if(append==1)
			document.getElementById(contentSpace).innerHTML = document.getElementById(contentSpace).innerHTML + cont;
		else
			document.getElementById(contentSpace).innerHTML = cont;		
		resizeMainCont();
		}catch(e){alert(e);}
}

function getControl(obj, param)
{
	popup(waitImg,1);
	aj.callPage("index.php", eval(obj+"Callback"), "POST", "control="+obj+"&param="+param);
}

function getAction(action, qs, method)
{
	popup(waitImg,1);
	if(method=="" || method=="GET")
		aj.callPage("index.php?action="+action+qs, eval(action+"Callback"));
	if(method=="POST")
	{
		q = "";
		if(qs != "")
			q = "&"+qs;
		aj.callPage("index.php", eval(action+"Callback"), "POST", "action="+action + q);
	}
}
*/
function include(file)
{
	var script  = document.createElement('script');
	script.src  = file;
	script.type = 'text/javascript';
	script.defer = true;
	document.getElementsByTagName('head').item(0).appendChild(script);
}

var ajs = new DCRE_GLM.AJAX();

//connector with main.js getPage
function getEPage(code, qs, post)
{
	getPage(code+"&"+qs);//, post);
}

function ajCall(action, qs, method)
{
	//popup(waitImg,1);
	if(method=="" || method=="GET")
		ret = ajs.callPage("/eshop/eshop.php?code="+action+qs, "");
	if(method=="POST")
	{
		q = "";
		if(qs != "")
			q = "&"+qs;
		ret = ajs.callPage("/eshop/eshop.php", "", "POST", "code="+action + q, false);
	}
	return ret;
}

//the above function will call back the following
function PICKITEMCallback(response)
{
	popup("", 0);
	fillContent(response,0);
}

function eshopCartCallback(response)
{
	document.getElementById("eshopCartDiv").innerHTML = response;
}

function ADDPRODTOCARTCallback(response)
{
	//alert(response);
	getControl("eshopCart", page);
	popup("", 0);
}

function MYCARTCallback(response)
{
	popup("", 0);
	fillContent(response,0);
}

function UPDATEQTYCallback(response)
{
	//fillContent(response,0);
	getEPage("MYCART", "", "POST");
	getControl("eshopCart", page);
	popup("", 0);
}

function DELETEPRODCallback(response)
{
	//fillContent(response,0);
	getEPage("MYCART", "", "POST");
	getControl("eshopCart", page);
	popup("", 0);
}

function CHECKOUTCallback(response)
{
	fillContent(response,0);
	popup("", 0);
}

function APPARELORDERCallback(response)
{
	fillContent(response,0);
	popup("", 0);
}


function e_pickItem(type, prod, color)
{
	getEPage("PICKITEM", "type=" + type + "&prod=" + prod + "&color=" + color, "POST");
	//getAction("PICKITEM", "type=" + type + "&prod=" + prod + "&color=" + color, "POST");
}

function e_add2cart(pid)
{
	qty = document.getElementById("qtyfld").value;
	size = document.getElementById("sizefld").value;
	color = document.getElementById("colorfld").value;
	
	getAction("ADDPRODTOCART", "pid=" + pid + "&qty=" + qty + "&size=" + size + "&color=" + color, "POST");
}

function deleteOrdProd(prodType, prodCode, size, color)
{
	getAction("DELETEPROD", "type="+prodType+"&prodCode="+prodCode+"&size="+size+"&color="+color, "POST");	
}

function saveOrdProdQty(prodType, prodCode, size, color, qtyObj)
{
	var qty = document.getElementById(qtyObj).value;
	
	getAction("UPDATEQTY", "type=" +  prodType + "&prodCode="+prodCode+"&qty="+qty+"&size="+size+"&color="+color, "POST");
}

function e_checkout(step, qty)
{
	if(qty > 0)	
		getEPage("CHECKOUT", "step="+step, "POST");		
}

function getCO(st)
{
	if(st == 4)
	{
		proftype = document.getElementById("editprofile").value;
		editProfile(proftype);
		return;
	}
	e_checkout(st)	;
}

function regTable(id)
{
	if(id == 1)
	{
		document.getElementById("regProfTR").style.display = "none";
		document.getElementById("regAfmTR").style.display = "none";
		document.getElementById("regDoyTR").style.display = "none";
	}
	else
	{
		document.getElementById("regProfTR").style.display = "";
		document.getElementById("regAfmTR").style.display = "";
		document.getElementById("regDoyTR").style.display = "";
	}	
	/*
	document.getElementById("personTable").style.display = "none";
	document.getElementById("compTable").style.display = "none";
	
	if(id == 1)
		document.getElementById("personTable").style.display = "";
		
	
	if(id == 2)
		document.getElementById("compTable").style.display = "";

	document.getElementById("editprofile").value = id;
	*/						
	//clearReg();		
}

//this is called when the user selects a country from the ddlb
//val=country, id=0
function countrySelectedGUEST(val, id)
{
	countrySelected(val, id);
	selectShipAddressGUEST();
}

function countrySelected(val, id)
{
	if(val == "Greece")
		document.getElementById("locTR"+id).style.display = "";
	else
		document.getElementById("locTR"+id).style.display = "none";
}

function selectShipAddressGUEST()
{
	
	cnt = document.getElementById("regCountry").value;
	response = ajCall("GETSHIPPINGDDLBGUEST", "cnt=" + cnt, "POST");
	
	popup("", 0);
	loc = document.getElementById("regLocation").value;
	document.getElementById("shipselectorDiv").innerHTML = response;
	
	response = ajCall("GETSHIPPINGGUEST", "cnt="+cnt+"&loc="+loc, "POST");
	
	popup("", 0);
	vals = response.split("---");
	
	document.getElementById("fedexcost").value = vals[0];
    document.getElementById("eltacost").value = vals[1];
	document.getElementById("eltacostadi").value = vals[2];
	document.getElementById("acscost").value = vals[3];
	document.getElementById("acscostadi").value = vals[4];
	
	document.getElementById("shipSelector").value = 0;
	document.getElementById("shippingtd").innerHTML = "0.00";
	document.getElementById("totalsum").innerHTML = document.getElementById("cartcost").value;
	//document.getElementById("dotheorder").style.display = "none";
}
/*
function GETSHIPPINGDDLBGUESTCallback(response)
{
	popup("", 0);
	loc = document.getElementById("regLocation").value;
	document.getElementById("shipselectorDiv").innerHTML = response;
	
	ajCall("GETSHIPPINGGUEST", "cnt="+cnt+"&loc="+loc, "POST", 1);
}

function GETSHIPPINGGUESTCallback(response)
{
	popup("", 0);
	vals = response.split("---");
	
	document.getElementById("fedexcost").value = vals[0];
    document.getElementById("eltacost").value = vals[1];
	document.getElementById("eltacostadi").value = vals[2];
	document.getElementById("acscost").value = vals[3];
	document.getElementById("acscostadi").value = vals[4];
	
	document.getElementById("shipSelector").value = 0;
	document.getElementById("shippingtd").innerHTML = "0.00";
	document.getElementById("totalsum").innerHTML = document.getElementById("cartcost").value;
	document.getElementById("dotheorder").style.display = "none";
}
*/

function isThereAddr2()
{
	//insShippingAddressSubmitGuest();
	msg1 = "Πρέπει να επιλέξετε Χώρα για να μπορέσει να γίνει υπολογισμός του κόστους μεταφοράς.";
	msg2 = "Πρέπει να επιλέξετε Τοποθεσία για να μπορέσει να γίνει υπολογισμός του κόστους μεταφοράς.";
	
	if(lang == "EN")
	{
		msg1 = "You must choose a Country.";
		msg2 = "You must choose a Location.";
	}
	
	country = document.getElementById("regCountry").value;
	loca = document.getElementById("regLocation").value;
		
	if(country == 0)
	{
		alert(msg1);
		return false;
	}	
	
	if(country == "Greece" && loca == "0")
	{
		alert(msg2);
		return false;
	}
	return true;
	
}

function calcCost(val)
{
	tot = document.getElementById("cartcost").value;
	ship = "0.00";
	
	//document.getElementById("dotheorder").style.display = "none";
	///document.getElementById("paypalbt").style.display = "none";
	
	if(val == "FEDEX")
	{
		ship = document.getElementById("fedexcost").value;
	//	document.getElementById("paypalbt").style.display = "";
	}
	
	if(val == "ELTAPAY")
	{	
		ship = document.getElementById("eltacost").value;
	//	document.getElementById("paypalbt").style.display = "";
	}
	
	if(val == "ELTAAND")
	{	
		ship = document.getElementById("eltacostadi").value;
		//document.getElementById("dotheorder").style.display = "";
	}
	
	if(val == "ACSPAY")
	{	
		ship = document.getElementById("acscost").value;
		//document.getElementById("paypalbt").style.display = "";
	}
	
	if(val == "ACSAND")
	{	
		ship = document.getElementById("acscostadi").value;
		//document.getElementById("dotheorder").style.display = "";
	}
	
	if(val == "LOCAL")
	{	
		ship = "0.00";
		//document.getElementById("dotheorder").style.display = "";
	}
	
	var totn = new Number(tot);
	var shipn = new Number(ship);
	
	shipnf = shipn.toFixed(2);
	totnf = totn.toFixed(2);
	
	total = parseFloat(shipn) + parseFloat(totn);
	ntot = new Number(total);
	
	document.getElementById("shippingtd").innerHTML = ship;
	document.getElementById("totalsum").innerHTML = ntot.toFixed(2);
	document.getElementById("paypalprice").value =  ntot.toFixed(2);
}

function showProdCateg(categ)
{
	divs = document.getElementById("contSpace").getElementsByTagName("div");
	
	for(i=0;i<divs.length;i++)
	{
		if(divs[i].getAttribute("isCont") == 1)
			divs[i].style.display = "none";
	}
	document.getElementById(categ+"DIV").style.display = "";
}

function sendOrder(paypal, islogged)
{	
	//if(islogged == 1)
	//	sendOrderMEMBER(paypal);
	//else
	sendOrderGUEST(paypal);
}

/*
function sendOrderMEMBER(paypal)
{
	msg1 = "Παρακαλώ επιλέξτε Διεύθυνση Αποστολής";
	msg = "Παρακαλώ επιλέξτε Τρόπο Αποστολής";
	if(lang == "EN")
	{
		msg = "Please choose Shipping Method";
		msg1 = "Please enter a Shipping Address";
	}
	
	if(document.getElementById("shipSelector").value == 0)
	{
		alert(msg);
		return false;
	}
	
	shippingWay = document.getElementById("shipSelector").value;
	shippingCost = document.getElementById("shippingtd").innerHTML;
	addrId = document.getElementById("addrSelect").value;
	cost = document.getElementById("totalmoney").innerHTML;
	discount = document.getElementById("totaldiscount").innerHTML;
	fpa = document.getElementById("totalfpa").innerHTML;
	theFPA = document.getElementById("theFPA").value;
	
	if(addrId == "" || addrId == "0")
	{
		alert(msg1);
		return;
	}
	
	qs = 'shipping=' + shippingWay + '&shippingcost=' + shippingCost + '&cost=' + cost + '&discount=' + discount + "&addrid=" + addrId + "&fpa=" + fpa + "&theFPA=" + theFPA;

	res = aj.callPage("eshop.php", "", "POST", "code=SUBMITORDER&" + qs);
	
	success = "Η παραγγελία σας αποθηκεύτηκε επιτυχώς.<br>Μπορείτε να παρακολουθήσετε την κατάσταση της παραγγελίας σας επιλέγοντας <strong>'Η παραγγελία μου'</strong> από το μενού πάνω δεξιά.<br> Αν κάνατε αγορά χωρίς να συνδεθείτε πρώτα στο σύστημα, σας έχει αποσταλεί το αντίστοιχο email. <br><br> Ευχαριστούμε.";
	failure = "Κάποιο πρόβλημα παρουσιάστηκε. Παρακαλώ προσπαθήστε πάλι πατώντας <strong>'πληρωμή'</strong> από το μενού αριστερά.";
			
	if(lang == "EN")
	{
		success = "Your order has been placed successfully.<br>You can monitor your order's status by clicking <strong>My Order</strong> on the upper right menu.<br> If you made the transaction without logging in, an email has been sent to you.<br><br>Thank you.";
		failure = "An error has occurred while processing your order. Please try again by clicking <strong>'checkout'</strong> on the left menu.";
	}
	
	if(paypal == 0)
	{
		msg = res == 0?failure:success;

		getPage("MYORDER","");
		reload("HEADER", "");
		
		document.getElementById("genPopup").innerHTML = msg;
		document.getElementById("genPopup").style.display = "";
		jQuery.facebox(genPopup);
	}
	else
	{
		if(res == 0)
		{
			msg = failure;
			document.getElementById("MAINDIVCONT").innerHTML = msg;
			reload("HEADER", "");
			//reload("SIDEMENU", "");
		}
		else
		{
			document.getElementById("custompaypal").value = res;
			document.forms.paypalform.submit();
		}	
	}
		
	return false;
}
*/

function sendOrderGUEST(paypal)
{
	isThereAddr2();
	
	if (!insShippingAddressSubmitGuest())
	{
		return false;
	}

	shippingWay = document.getElementById("shipSelector").value;
	shippingCost = document.getElementById("shippingtd").innerHTML;
//	addrId = document.getElementById("addrSelect").value;
	cost = document.getElementById("totalmoney").innerHTML;
	discount = document.getElementById("totaldiscount").innerHTML;
	fpa = document.getElementById("totalvat").innerHTML;
	
	//if(addrId == "" || addrId == "0")
	//{
	//	alert(msg1);
	//	return;
	//}
	
	qs = 'shipping=' + shippingWay + '&shippingcost=' + shippingCost + '&cost=' + cost + '&discount=' + discount + "&fpa=" + fpa;

	//res = aj.callPage("eshop.php", "", "POST", "code=SUBMITORDERGUEST&" + qs);
	res = ajCall("SUBMITORDERGUEST", qs, "POST");

	success = "Η παραγγελία σας αποθηκεύτηκε επιτυχώς.\r\nΜπορείτε να παρακολουθήσετε την κατάσταση της παραγγελίας σας επιλέγοντας 'Η παραγγελία μου'.\r\nΣας έχει αποσταλεί το αντίστοιχο email. \r\n\r\n Ευχαριστούμε.";
	failure = "Κάποιο πρόβλημα παρουσιάστηκε. Παρακαλώ προσπαθήστε πάλι.";
			
	if(lang == "EN")
	{
		success = "Your order has been placed successfully.<br>You can monitor your order's status by clicking <strong>My Order</strong> on the upper right menu.<br> If you made the transaction without logging in, an email has been sent to you.<br><br>Thank you.";
		failure = "An error has occurred while processing your order. Please try again by clicking <strong>'checkout'</strong> on the left menu.";
	}
	
	if(paypal == 0)
	{
		msg = success;
		if(res == 0 || res == -100)
			msg = failure;
			
		alert(msg);
	}
	else
	{
		if(res == 0)
		{
			msg = failure;
			alert(msg);
		}
		else
		{
			//alert("paypal is tough!");
			document.getElementById("custompaypal").value = res;
			document.forms.paypalform.submit();
		}	
	}
	//-----------------------INTEGRATION------------------//
	//this belongs to main.js
	refreshPage("APPAREL");
	return false;
}

function insShippingAddressSubmitGuest()
{
	eml = "Το email που έχετε εισάγει δεν είναι σωστό";
	eml2 = "Τα 2 email που έχετε εισάγει δεν είναι ίδια";
	msg = "Πρέπει να συμπληρώσετε όλα τα πεδία, στα 'Στοιχεία Αποστολής'";
	err = "Δεν μπορείτε να εισάγετε πάνω από 3 διευθύνσεις";
	err2 = "Φαίνεται πως έχετε ήδη χρησιμοποιήσει αυτό το email για παραγγελία στο παρελθόν. Για να το ξαναχρησιμοποιήσετε θα πρέπει να κάνετε πρώτα σύνδεση στο σύστημα με αυτό το email και τον κωδικό σας.";
	err3 = "Πρέπει να επιλέξετε Τρόπο Αποστολής";
	
	if(lang == "EN")
	{
		eml = "You have entered an invalid email address";
		eml2 = "The 2 email addresses you entered are not the same";
		msg = "All 'Delivery Address' fields are mandatory";
		err = "You cannot insert more than 3 shipping addresses";
		err2 = "The email address you entered belongs to a registered member.";
		err3 = "You have to choose a Shipping Way.";
	}
	
	shippingWay = document.getElementById("shipSelector").value;

	if(shippingWay == 0)
	{
		alert(err3);
		return false;
	}
	
	name = document.getElementById("regName").value;
	address = document.getElementById("regAddress").value;
	city = document.getElementById("regCity").value;
	tk = document.getElementById("regTK").value;
	country = document.getElementById("regCountry").value;
	loca = document.getElementById("regLocation").value;
	//uid = document.getElementById("uid").value;
	email = document.getElementById("regEmail").value;
	email2 = document.getElementById("reg_Email").value;
	
	iscomp = document.getElementById("regCompanyRB").checked;
	
	if(iscomp)
	{
		doy = document.getElementById("regDoy").value;
		afm = document.getElementById("regAfm").value;
		prof = document.getElementById("regProf").value;
		
		if(doy == "" || afm == "" || prof == "")
		{
			alert(msg);
			return false;
		}
	}
	
	if(email == "" || name == "" || address == "" || city == "" || tk == "" || country == 0 || (country == "Greece" && loca == "0"))
	{
		alert(msg);
		return false;
	}	
	else if(!echeck(email))
	{
		alert(eml);
		return false;
	}
	else if(email != email2)
	{
		alert(eml2);
		return false;
	}
	else
	{
		//ret = aj.callPage("eshop.php", "", "POST", "code=ADDSHIPPINADDRESSGUEST&email=" + email + "&uid=" + uid + "&name=" + name + "&address=" + address + "&city=" + city + "&tk=" + tk + "&country=" + country + "&location=" + loca);
		if(iscomp)
			ret = ret = ajCall("ADDSHIPPINADDRESSGUEST", "email=" + email + "&name=" + name + "&address=" + address + "&city=" + city + "&tk=" + tk + "&country=" + country + "&location=" + loca + "&prof=" + prof + "&doy=" + doy + "&afm=" + afm, "POST");
		else	
			ret = ajCall("ADDSHIPPINADDRESSGUEST", "email=" + email + "&name=" + name + "&address=" + address + "&city=" + city + "&tk=" + tk + "&country=" + country + "&location=" + loca, "POST");
		
		if(ret == 1)
			return true;
		else if(ret == -1)
			alert(err);
		else if(ret == -2)
			alert(err2);
		else
			alert(res);	
	}
	
	return false;
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   //alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   // alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    ///alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}
	
	function isNumeric(value) {
  if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
  return true;
}

function APPARELORDERCallback(response)
{
	fillContent(response,0);
}

function showSuborders(id)
{
	more = "δείξε λεπτομέρειες >>";
	less = "κρύψε λεπτομέρειες >>";
	
	if(lang == "EN")
	{
		more = "show details >>";
		less = "hide details >>";
	}
	
	disp = "";
	txt = less;
	if(document.getElementById(id + "_TR").style.display == "")
	{
		disp = "none";
		txt = more;
	}
		
	document.getElementById(id + "_TR").style.display = disp;
	document.getElementById(id + "_TD").innerHTML = txt;
	
	//this is bouncing-betty specific function
	resizeMainCont();
}

function gotoCart(qty)
{
	if(qty > 0)
		getEPage("MYCART", "", "POST");
}

function e_login()
{
	err1 = "πρέπει να συμπληρώσετε και τα 2 πεδία";
	err2 = "το email που έχετε εισάγει δεν είναι έγκυρο";
	
	if(lang == "EN")
	{
		err1 = "You must complete both fields";
		err2 = "This is not a valid email address";
	}
	
	eml = document.getElementById("e_emlinput").value;
	pwd = document.getElementById("e_pwdinput").value;
	
	if(eml == "" || pwd == "")
	{
		alert(err1);
		return;
	}
	
	if(!echeck(eml))
	{
		alert(err2);
		return;
	}
	
	//this is from main.js
	login2();
	return;
}
