// JavaScript Document

var busyImg = "<img src=\"images/busy.gif\" width=\"16\" height=\"16\" />";
var collectionScroll = false;
var pGender = 0;
var pBodypart = 0;
var pType = 0;
var pSeries = 0;

function gebi(elementID) {
	return document.getElementById(elementID);	
}

function isValidEmail(str) {
	return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str));
}
function isValidUsername(str) {
	return !(/\W/.test(str));
}

function getClubCard(){
	ajaxCallPage('components/customer.clubcard.php#c_club_card','GET',null, function(rText){
		if(rText.substring(0,1)=='1'){
			alert(rText.substring(1));
			$('#c_club_card').val('');
		} else {
			if(confirm('Θέλετε να σημπληρωθούνε τα στοιχεία αυτόματα με τα στοιχεία της κάρτας σας;')){
				var cVals = rText.split('|||');
				$('#c_club_card').val(cVals[0]);
				$('#c_lastname').val(cVals[1]);
				$('#c_firstname').val(cVals[2]);
				$('#c_address').val(cVals[3]);
				$('#c_area').val(cVals[4]);
				$('#c_city').val(cVals[5]);
				$('#c_postcode').val(cVals[6]);
				$('#c_tel').val(cVals[7]);
				$('#c_mobile').val(cVals[8]);
				$('#c_email').val(cVals[9]);
			}
		}
	});
}
function catCategories(Tg_id, Tbd_id, Tt_id){
	/* Tg_id: gender
	   Tbd_id: bodypart
	   Tt_id: type
	   Ty_id: synth */
	if(Tg_id && !Tbd_id && !Tt_id) {
		loadUrl('components/catalog.categories.php?ajc=1&g_id='+Tg_id,'div_catalog',0);
		pGender = Tg_id;
		pBodypart = 0;
		pType = 0;
		loadUrl('components/catalog.collections.php?ajc=1&g_id='+Tg_id+'&bd_id='+Tbd_id+'&t_id='+Tt_id,'div_collections',0);
		loadUrl('components/catalog.synthesis.php?ajc=1&g_id='+Tg_id+'&bd_id='+Tbd_id+'&t_id='+Tt_id,'div_synths',0);
		loadUrl('components/products.php?g_id='+Tg_id+'&bd_id='+Tbd_id+'&t_id='+Tt_id,'div_content',0);
	}
	if(Tg_id && Tbd_id && !Tt_id) {
		loadUrl('components/catalog.categories.php?ajc=1&g_id='+Tg_id+'&bd_id='+Tbd_id,'div_catalog',0);
		pGender = Tg_id;
		pBodypart = Tbd_id;
		pType = 0;
		loadUrl('components/catalog.collections.php?ajc=1&g_id='+Tg_id+'&bd_id='+Tbd_id+'&t_id='+Tt_id,'div_collections',0);
		loadUrl('components/catalog.synthesis.php?ajc=1&g_id='+Tg_id+'&bd_id='+Tbd_id+'&t_id='+Tt_id,'div_synths',0);
		loadUrl('components/products.php?g_id='+Tg_id+'&bd_id='+Tbd_id+'&t_id='+Tt_id,'div_content',0);
	}
	if(Tg_id && Tbd_id && Tt_id) {
		loadUrl('components/catalog.categories.php?ajc=1&g_id='+Tg_id+'&bd_id='+Tbd_id,'div_catalog',0);
		pGender = Tg_id;
		pBodypart = Tbd_id;
		pType = Tt_id;
		loadUrl('components/catalog.collections.php?ajc=1&g_id='+Tg_id+'&bd_id='+Tbd_id+'&t_id='+Tt_id,'div_collections',0);
		loadUrl('components/catalog.synthesis.php?ajc=1&g_id='+Tg_id+'&bd_id='+Tbd_id+'&t_id='+Tt_id,'div_synths',0);
		loadUrl('components/products.php?g_id='+Tg_id+'&bd_id='+Tbd_id+'&t_id='+Tt_id,'div_content',0);
	}
	dhtmlHistory.add("cat::"+Tg_id+","+Tbd_id+","+Tt_id);
}

function setColScroll(cS) {
	collectionScroll = cS;	
}

function moveColDiv(dirn) {
	var top = parseInt(gebi('div_collection').style.top.substring(0,gebi('div_collection').style.top.length-2));
	if(collectionScroll){
		if(dirn=="down"){
			if((Math.abs(top)+500) < gebi('div_collection').offsetHeight) {
				gebi('div_collection').style.top = (top - 5) + "px";
				setTimeout("moveColDiv('down')",10);
			}
		}
		if(dirn=="up"){
			if(top < 0){
				gebi('div_collection').style.top = (top + 5) + "px";
				setTimeout("moveColDiv('up')",10);
			}
		}
	}
}

function toggleVisibility(elemId, action){
	var elem = gebi(elemId);
	if(action=="hide") {
		elem.style.height="0px";
		elem.style.visibility="hidden";
		elem.style.overflow="hidden";
	} else if(action=="show") {
		elem.style.height="auto";
		elem.style.visibility="visible";
		elem.style.overflow="visible";
	} else {
		if(elem.style.visibility!="hidden"){
			elem.style.height="0px";
			elem.style.visibility="hidden";
			elem.style.overflow="hidden";
		} else {
			elem.style.height="auto";
			elem.style.visibility="visible";
			elem.style.overflow="visible";
		}
	}
}	

function positionBasket(){
	var elem = gebi('div_basket');
	var docWidth = document.body.offsetWidth;
	var elemWidth = elem.style.width.substring(0,elem.style.width.length-2);
	elem.style.left = (docWidth - parseInt(elemWidth) - 20) + "px";
}

function positionWishlist(){
	var elem = gebi('div_wishlist');
	var docWidth = document.body.offsetWidth;
	var elemWidth = elem.style.width.substring(0,elem.style.width.length-2);
	elem.style.left = (docWidth - parseInt(elemWidth) - 20) + "px";
}

function positionLogin(){
	var elem = gebi('div_loginbox');
	var docWidth = document.body.offsetWidth;
	var elemWidth = elem.style.width.substring(0,elem.style.width.length-2);
	elem.style.left = ((docWidth - parseInt(elemWidth)) / 2 ) + "px";
}
// XHR Operations
function createXHR() {
	if (window.XMLHttpRequest)
		var req = new XMLHttpRequest();
	else if(window.ActiveXObject)
		var req = new ActiveXObject("MSXML2.XMLHTTP.3.0");
	return req;
}

function loadUrl(url, container, histAdd) {
	if(histAdd > 0) {
		dhtmlHistory.add('lu::'+url+'::'+container+'::'+histAdd);
	}
	gebi('div_busy').style.visibility = "visible";
	if(container == "div_content") {
		gebi('div_cnt_busy').style.visibility = "visible";
	}
	var req = null;
	req = createXHR();
	req.onreadystatechange = function() {
		if (req.readyState == 4 && req.status == 200 && req.responseText) { // Complete
			loadLoginStatus();
			basketSet();
			wishlistSet();
			gebi(container).innerHTML = req.responseText;	
			gebi('div_busy').style.visibility = "hidden";
			if(container == "div_content") {
				gebi('div_cnt_busy').style.visibility = "hidden";
			}
			resizeProdList();
			sIFR.replaceElement(named({sSelector:".sifr_1", sFlashSrc:"images/sifr.swf", sColor:"#D6D6D6", sBgColor:"#FFFFFF", sWmode:"transparent", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=center&offsetTop=0"}));
			sIFR.replaceElement(named({sSelector:".sifr_2", sFlashSrc:"images/sifr.swf", sColor:"#8c8c8c", sBgColor:"#FFFFFF", sWmode:"transparent", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=center&offsetTop=0"}));
			sIFR.replaceElement(named({sSelector:".sifr_3", sFlashSrc:"images/sifr.swf", sColor:"#8c8c8c", sBgColor:"#FFFFFF", sWmode:"transparent", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=center&offsetTop=0"}));
			sIFR.replaceElement(named({sSelector:".sifr_4", sFlashSrc:"images/sifr.swf", sColor:"#FFFFFF", sBgColor:"#FFFFFF", sWmode:"transparent", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0"}));
			if(url.split("?")[0] == "components/product.view.php"){
				for(i=0;i<url.split("&").length;i++){
					if(url.split("&")[i].substring(0,2)=="c=") {
						showAvailability(url.split("&")[0].split("=")[1]);
						break;
					}
				}
			}
			
		}
	};
	req.open("GET", url);
	req.send("");
}

function ajaxCallPage(url, method, container, execf, tMCE){
	gebi('div_busy').style.visibility = "visible";

	method = (method=='GET' || method=='POST') ? method : 'GET';
	var varSend ="";
	var queryStr = (url.split("#")[1]) ? url.split("#")[1] : '';
	var nUrl = url.split("#")[0].split("?")[0];
	var exQstr = (url.split("#")[0].split("?")[1]) ? url.split("#")[0].split("?")[1] : '' ;
	
	if(queryStr.length > 0) { 
		for(i=0;i<queryStr.split(";").length;i++){
			if(gebi(queryStr.split(";")[i])) varSend += "&" + queryStr.split(";")[i] + "=" + encodeURIComponent(gebi(queryStr.split(";")[i]).value);
			else alert("WARNING: Element defined in url with id '"+queryStr.split(";")[i]+"' does not exist.");
		}
	}
	//alert(nUrl+"\n\n"+exQstr+"\n\n"+varSend);
	rq = createXHR();
	rq.onreadystatechange = function() {
		if (rq.readyState == 4 && rq.status == 200 && rq.responseText) { // Complete
			performPostLoadOperations(rq.responseText, container, execf, tMCE);

		} else if(rq.readyState == 4 && rq.status == 404) { // Page not found
			gebi('div_busy').style.visibility = "hidden"; alert("404 ERROR: Page not found.\n\n"+nUrl+"\n\n ");
		}
	};
	
	if(method=='GET') { rq.open("GET", nUrl + "?" + exQstr + varSend); rq.send(""); } 
	else { rq.open("POST", nUrl + "?" + exQstr); rq.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); rq.send(varSend); }
}

function performPostLoadOperations(rText, container, execf, tMCE) {
	gebi('div_busy').style.visibility = "hidden";
	if(gebi(container))	gebi(container).innerHTML = translate(rText,adminLanguage);
	if(gebi('script')) { eval(gebi('script').innerHTML.replace(/&gt;/g,">").replace(/&lt;/g,"<").replace(/&amp;/g,"&")); gebi('script').parentNode.removeChild(gebi('script')); }
	if(execf) execf.call(this, rText);
}

function setSelectedColor(c,i) {
	gebi("s_color").value = c;
	var cnt=0;
	while(gebi('a_color_'+cnt)){
		if(cnt==i) 
			gebi('a_color_'+cnt).style.color="#ff0000";
		else
			gebi('a_color_'+cnt).style.color="#ffffff";
		cnt++;
	}
}
function setSelectedSize(s,i) {
	gebi("s_size").value = s;
	var cnt=0;
	while(gebi('a_size_'+cnt)){
		if(cnt==i) gebi('a_size_'+cnt).style.color="#ff0000";
		else gebi('a_size_'+cnt).style.color="#ffffff";
		cnt++;
	}
}

function showAvailability(u_id) {
	var url = "components/products.availability.php?"+
				"&u_id=" + u_id +
				"&clr="+gebi("s_color").value	+
				"&size="+gebi("s_size").value;

	if(gebi("s_color").value > 0 && gebi("s_size").value > 0) {
		gebi('div_busy').style.visibility = "visible";	
		var req = createXHR();
		req.onreadystatechange = function() {
			if (req.readyState == 4 && req.status == 200 && req.responseText) { // Complete
				var respArr = req.responseText.split("|||");
				gebi("div_availability").innerHTML = respArr[0];
				gebi("div_addtobasket").innerHTML = respArr[1];
				gebi("div_price").innerHTML = respArr[2];				
				gebi('div_busy').style.visibility = "hidden";	
			}
		};
		req.open("GET", url);
		req.send("");
	}
}

function basketSet(action, u_id, quantity) {
	var container = "div_basket";
	var url = "components/basket.php?action="+action;
	
	url += (u_id > 0) ? "&u_id="+u_id : "";	
	url += (quantity > 0) ? "&qty="+quantity : "";	

	gebi('div_busy').style.visibility = "visible";	
	var req = createXHR();
	req.onreadystatechange = function() {
		if (req.readyState == 4 && req.status == 200 && req.responseText) { // Complete
			gebi(container).innerHTML = req.responseText;
			gebi('div_busy').style.visibility = "hidden";	
		}
	};
	req.open("GET", url);
	req.send("");
}

function wishlistSet(action, item_id){
	var container = "div_wishlist";
	var url = "components/wishlist.php?action="+action;
	
	url += (item_id) ? "&item="+item_id : "";	

	gebi('div_busy').style.visibility = "visible";	
	var req = createXHR();
	req.onreadystatechange = function() {
		if (req.readyState == 4 && req.status == 200 && req.responseText) { // Complete
			gebi(container).innerHTML = req.responseText;
			gebi('div_busy').style.visibility = "hidden";	
		}
	};
	req.open("GET", url);
	req.send("");
}

function sendContactForm(){
	if(!gebi('c_firstname').value) {
		if(lang=='gr') alert("Παρακαλώ συμπληρώστε το όνομά σας.");
		if(lang=='en') alert("Please input your first name.");
	} else if(!gebi('c_lastname').value) {
		if(lang=='gr') alert("Παρακαλώ συμπληρώστε το επίθετο σας.");
		if(lang=='en') alert("Please input your last name.");
	} else if(!isValidEmail(gebi('c_email').value)) {
		if(lang=='gr') alert("Παρακαλώ συμπληρώστε σωστά το email σας.");
		if(lang=='en') alert("Please enter a valid email address.");
	} else {
		$('#c_sendbtn').hide();
		$('#div_sending').show();
		var rq = createXHR();
		rq.onreadystatechange = function() {
			if (rq.readyState == 4 && rq.status == 200 && rq.responseText) {
				if(rq.responseText.substring(0,1)=="1") {
					alert(rq.responseText.substring(1,rq.responseText.length));	
				} else {
					if(rq.responseText =='done') {
						$('#div_sending').hide();
						$('#div_senddone').show();
					} else { 
						alert(rq.responseText);
					}
				}
		}};
		
		rq.open("POST", "components/contactform.send.php"); 
		rq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		rq.send(
				"&c_lastname=" + encodeURIComponent(gebi("c_lastname").value) +
				"&c_firstname=" + encodeURIComponent(gebi("c_firstname").value) +
				"&c_email=" + encodeURIComponent(gebi("c_email").value) +
				"&c_message=" + encodeURIComponent(gebi("c_message").value) 
				);	
	}
}

function registerCustomer(action){
	if(!gebi('c_firstname').value) {
		if(lang=='gr') alert("Παρακαλώ συμπληρώστε το όνομά σας.");
		if(lang=='en') alert("Please input your first name.");
	} else if(!gebi('c_lastname').value) {
		if(lang=='gr') alert("Παρακαλώ συμπληρώστε το επίθετο σας.");
		if(lang=='en') alert("Please input your last name.");
	} else if(!isValidEmail(gebi('c_email').value)) {
		if(lang=='gr') alert("Παρακαλώ συμπληρώστε σωστά το email σας.");
		if(lang=='en') alert("Please enter a valid email address.");
	} else if(!gebi('c_tel').value) {
		if(lang=='gr') alert("Παρακαλώ συμπληρώστε το τηλεφωνό σας χωρίς κενά και παύλες. (πχ. 2101234567)");
		if(lang=='en') alert("Please enter your phone number.");
	} else if(!gebi('c_address').value) {
		if(lang=='gr') alert("Παρακαλώ συμπληρώστε διεύθυνση / αριθμό.");
		if(lang=='en') alert("Please enter your address and number.");
	} else if(!gebi('c_area').value) {
		if(lang=='gr') alert("Παρακαλώ συμπληρώστε δήμο / περιοχή.");
		if(lang=='en') alert("Please enter your state / area / municipality.");
	} else if(!gebi('c_city').value) {
		if(lang=='gr') alert("Παρακαλώ συμπληρώστε πόλη.");
		if(lang=='en') alert("Please enter your city.");
	} else if(!gebi('c_postcode').value) {
		if(lang=='gr') alert("Παρακαλώ συμπληρώστε Τ/Κ.");
		if(lang=='en') alert("Please enter your zip code.");
	} else if(!gebi('c_agree').checked) {
		if(lang=='gr') alert("Αν θέλετε να συνεχίσετε, πρέπει να συμφωνήσετε με τους όρους χρήσης και πωλήσεων.");
		if(lang=='en') alert("If you want to continue you have to agree with the terms of use and terms of sales.");
	} else {
		var rq = createXHR();
		rq.onreadystatechange = function() {
			if (rq.readyState == 4 && rq.status == 200 && rq.responseText) {
				if(rq.responseText.substring(0,1)=="1") {
					alert(rq.responseText.substring(1,rq.responseText.length));	
				} else {
					if(action=="checkout"){
						loadUrl('components/checkout.confirm.php','div_content',1);
					} else if(action=="checkout_new") {
						gebi('div_content').innerHTML = "";
						loadUrl('components/customer.login.php?act=checkout_new','div_loginbox');
						toggleVisibility('div_loginbox','show');
					} else {
						loadUrl("components/homepage.php?ajaxcall=1","div_content",1);
						toggleVisibility('div_loginbox','show');
					}
				}
		}};
		
		var c_receivemails = (gebi('c_receivemails').checked) ? 1 : 0;
		rq.open("POST", "components/customer.save.php"); 
		rq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		rq.send(
				"&c_lastname=" + encodeURIComponent(gebi("c_lastname").value) +
				"&c_firstname=" + encodeURIComponent(gebi("c_firstname").value) +
				"&c_password=" + encodeURIComponent(gebi("c_password").value) +
				"&c_email=" + encodeURIComponent(gebi("c_email").value) +
				"&c_address=" + encodeURIComponent(gebi("c_address").value) +
				"&c_area=" + encodeURIComponent(gebi("c_area").value) +
				"&c_city=" + encodeURIComponent(gebi("c_city").value) +
				"&c_postcode=" + encodeURIComponent(gebi("c_postcode").value) +
				"&c_country_id=" + encodeURIComponent(gebi("c_country_id").value) +
				"&c_tel=" + encodeURIComponent(gebi("c_tel").value) +
				"&c_mobile=" + encodeURIComponent(gebi("c_mobile").value) +
				"&c_club_card=" + encodeURIComponent(gebi("c_club_card").value) +
				"&c_receivemails=" + encodeURIComponent(c_receivemails) +
				"&c_tim_bisname=" + encodeURIComponent(gebi("c_tim_bisname").value) +
				"&c_tim_prof=" + encodeURIComponent(gebi("c_tim_prof").value) +
				"&c_tim_afm=" + encodeURIComponent(gebi("c_tim_afm").value) +
				"&c_tim_doy=" + encodeURIComponent(gebi("c_tim_doy").value) +
				"&c_tim_address=" + encodeURIComponent(gebi("c_tim_address").value) +
				"&c_tim_city=" + encodeURIComponent(gebi("c_tim_city").value) +
				"&c_tim_area=" + encodeURIComponent(gebi("c_tim_area").value) +
				"&c_tim_tel=" + encodeURIComponent(gebi("c_tim_tel").value) 
				);	
	}

}

function loadLoginStatus(){
	var req = null;
	req = createXHR();
	req.onreadystatechange = function() {
		if (req.readyState == 4 && req.status == 200 && req.responseText) { // Complete
			gebi("div_l_username").innerHTML = req.responseText;
		}
	};
	req.open("GET", "components/login.status.php?ajaxcall=1");
	req.send("");
}

function loginUser(action) {
	var r_username = gebi("r_username").value;
	var r_password = gebi("r_password").value;
	var r_remember = (gebi("r_remember").checked) ? "yes" : "no";
	var rq = createXHR();
	rq.onreadystatechange = function() {
		if (rq.readyState == 4 && rq.status == 200 && rq.responseText) { // Complete
			var respText = rq.responseText.split("||");
			if(respText[0]=="success") {
				if(action=="checkout_new" || action=="checkout") {
					gebi("div_login_status").innerHTML = rq.responseText;
					basketSet();
					wishlistSet();
					loadUrl('components/checkout.confirm.php','div_content',1);
				}
				loadLoginStatus();
				toggleVisibility('div_loginbox');
			} else {
				gebi("div_login_status").innerHTML = rq.responseText;
			}
		}
	};
	rq.open("GET", "components/login.submit.php?" +
			 "&u=" + encodeURIComponent(r_username) +
			 "&p=" + encodeURIComponent(r_password) +
			 "&k=" + encodeURIComponent(r_remember)
			 );
	rq.send("");	
}

function logoutUser() {
	if(gebi("div_login")) gebi("div_login").innerHTML = "";
	var rq = createXHR();
	rq.onreadystatechange = function() {
		if (rq.readyState == 4 && rq.status == 200 && rq.responseText) { // Complete
			alert(rq.responseText);
			location.href='index.php';
		}
	};
	rq.open("GET", "components/logout.submit.php");
	rq.send("");
}

function keyLoginUser(evt, action){
	if(evt.keyCode == 13) loginUser(action);
}

function resizeProdList(){
	if(gebi('tb_prod')){
		var prodCellSize = gebi('li_prod_cell').offsetWidth;
		var prodsFit = Math.floor((document.body.offsetWidth-200) / prodCellSize);
		gebi('tb_prod').style.width = ( prodsFit * prodCellSize) + "px";
		gebi('div_cnt_busy').style.left = Math.round((gebi('div_content').offsetWidth / 2)) + "px";
	}
}

function proceedToPayment() {
	gebi('div_busy').style.visibility = "visible";
	gebi('div_cnt_busy').style.visibility = "visible";
	var rq = createXHR();
	rq.onreadystatechange = function() {
		if (rq.readyState == 4 && rq.status == 200 && rq.responseText) { // Complete
			gebi('div_content').innerHTML = rq.responseText;	
			gebi('div_busy').style.visibility = "hidden";
			gebi('div_cnt_busy').style.visibility = "hidden";
		}
	};
	rq.open("POST", "components/checkout.payment.php");
	rq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	rq.send("&comments=" + encodeURIComponent(gebi('o_comments').value) + 
			"&tim=" + gebi('o_timyes').value);
}

