/*
	Copyright (C) 2009 
	Author: Manolis Koukopoulos
	
	All Rights Reserved.
	
	You are not allowed to use this script for any purpose
	without	the authors permission.
	Using this script part or whole without the authors permissions
	violates copyright infrigment law.
*/

///////// Various operations /////////
String.prototype.trim = function() {
	tmp = this.replace(/^\s+/, '');
	return tmp.replace(/\s+$/, '');
};

function isValidEmail(str) {
	return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str));
}

//Obvious :P gebi is short for document.getElementById ;)
function gebi(elementID) {
	if(elementID) return document.getElementById(elementID);
	else return false;
}

// 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 ajaxCallPage(url, method, container, execf, tMCE){
	setBusy(container);
	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.");
		}
	}
	var 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
			//setNotBusy(); 
			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) {
	//setNotBusy();
	if(gebi(container))	gebi(container).innerHTML = rText;
	if(gebi('script')) { 
		eval($('#script').text());
		gebi('script').parentNode.removeChild(gebi('script')); 
	}
	if(execf) execf.call(this, rText);
}

function sendFormContact(){
	if(!gebi('c_name').value) {
		alert("Παρακαλώ συμπληρώστε το όνομά σας.");
	} else if(!isValidEmail(gebi('c_email').value)) {
		alert("Παρακαλώ συμπληρώστε σωστά το email σας.");
	} else if(!gebi('c_tel').value) {
		alert("Παρακαλώ συμπληρώστε το τηλέφωνο σας.");
	} else if(!gebi('c_message').value) {
		alert("Παρακαλώ γράψτε το μυνημά σας.");
	} else {
		$('#btn_send').hide();
		$('#btn_cancel').hide();
		$('#div_wait').show();
		ajaxCallPage('pages/mailform.send.php?act=contact#c_name;c_tel;c_email;c_message','POST',null,function(rText){
				alert(rText);
				$('#div_wait').hide();
				$('#c_name').val('');
				$('#c_email').val('');
				$('#c_tel').val('');
				$('#c_message').val('');
			});
	}
}

function sendFormSupport(){
	if(!gebi('c_name').value) {
		alert("Παρακαλώ συμπληρώστε το όνομά σας.");
	} else if(!isValidEmail(gebi('c_email').value)) {
		alert("Παρακαλώ συμπληρώστε σωστά το email σας.");
	} else if(!gebi('c_tel').value) {
		alert("Παρακαλώ συμπληρώστε το τηλέφωνο σας.");
	} else if(!gebi('c_message').value) {
		alert("Παρακαλώ γράψτε το μυνημά σας.");
	} else {
		$('#btn_send').hide();
		$('#btn_cancel').hide();
		$('#div_wait').show();
		ajaxCallPage('pages/mailform.send.php?act=support#c_name;c_tel;c_email;c_message','POST',null,function(rText){
				alert(rText);
				$('#div_wait').hide();
				$('#c_name').val('');
				$('#c_email').val('');
				$('#c_tel').val('');
				$('#c_message').val('');
			});
	}
}
function sendFormOrder(){
	if(!gebi('c_name').value) {
		alert("Παρακαλώ συμπληρώστε το όνομά σας.");
	} else if(!isValidEmail(gebi('c_email').value)) {
		alert("Παρακαλώ συμπληρώστε σωστά το email σας.");
	} else if(!gebi('c_address').value) {
		alert("Παρακαλώ συμπληρώστε τη διεύθυνση σας.");
	} else if(!gebi('c_tk').value) {
		alert("Παρακαλώ συμπληρώστε το τ.κ.");
	} else if(!gebi('c_tel').value) {
		alert("Παρακαλώ συμπληρώστε το τηλέφωνο σας.");
	} else if((gebi('c_pes3_pos').value==0) && (gebi('c_pes2_pos').value==0)&& (gebi('c_psp_pos').value==0)&& (gebi('c_xbox_pos').value==0)&& (gebi('c_wii_pos').value==0)&& (gebi('c_pc_pos').value==0)) {
		alert("Παρακαλώ επιλέξτε τουλάχιστον 1 προϊόν.");
	} else {
		$('#btn_send').hide();
		$('#btn_cancel').hide();
		$('#div_wait').show();
		ajaxCallPage('pages/mailform.send.php?act=order#c_name;c_address;c_tk;c_tel;c_email;c_message;c_pes3_pos;c_pes2_pos;c_psp_pos;c_xbox_pos;c_wii_pos;c_pc_pos;c_sum;c_id','POST',null,function(rText){
				alert(rText);
//				$('#div_wait').hide();
//				$('#c_name').val('');
//				$('#c_tk').val('');
//				$('#c_address').val('');
//				$('#c_email').val('');
//				$('#c_tel').val('');
//				$('#c_message').val('');
				$('#c_pes3_pos').val('0');
				$('#c_pes2_pos').val('');
				$('#c_psp_pos').val('0');
				$('#c_xbox_pos').val('0');
				$('#c_pes_pos').val('0');
				$('#c_pc_pos').val('0');
				$('#c_sum').val('0');
			});
	}
}

function sendFormAdv(){
	if(!gebi('c_name').value) {
		alert("Παρακαλώ συμπληρώστε το όνομά σας.");
	} else if(!isValidEmail(gebi('c_email').value)) {
		alert("Παρακαλώ συμπληρώστε σωστά το email σας.");
	} else if(!gebi('c_tel').value) {
		alert("Παρακαλώ συμπληρώστε το τηλέφωνο σας.");
	} else if(!gebi('c_ctitle').value) {
		alert("Παρακαλώ συμπληρώστε την επωνυμία της εταιρίας σας.");
	} else if(!gebi('c_ctitle').value) {
		alert("Παρακαλώ συμπληρώστε την επωνυμία της εταιρίας σας.");
	} else if(!gebi('c_address').value) {
		alert("Παρακαλώ συμπληρώστε την διεύθυνση & αριθμό της εταιρίας σας.");
	} else if(!gebi('c_city').value) {
		alert("Παρακαλώ συμπληρώστε πόλη και Τ.Κ. της εταιρίας σας.");
	} else if(!gebi('c_message').value) {
		alert("Παρακαλώ γράψτε το μυνημά σας.");
	} else {
		$('#btn_send').hide();
		$('#btn_cancel').hide();
		$('#div_wait').show();
		ajaxCallPage('pages/mailform.send.php?act=adv#c_name;c_tel;c_email;c_message;c_ctitle;c_cweb;c_address;c_city','POST',null,function(rText){
				alert(rText);
				$('#div_wait').hide();
				$('#c_name').val('');
				$('#c_tel').val('');
				$('#c_email').val('');
				$('#c_message').val('');
				$('#c_ctitle').val('');
				$('#c_cweb').val('');
				$('#c_address').val('');
				$('#c_city').val('');
				$('#c_message').val('');
			});
	}
}

function headerImage(obj, imgFile, imgLink){
	var num = $(obj).get(0).id.substring(6);
	$('#header_logo').css({backgroundImage:'url(files/images/headers/'+imgFile+')'});
	if(imgLink!='') {
		$('#header_logo').html('<a style="position:absolute; width:909px; height:245px; left: 4px; top: 32px;" href="'+imgLink+'" target="_blank"></a>');
	}
	else {
		$('#header_logo').html('');
	}
	$('#header_switcher>a').each( function() { $(this).css({backgroundImage:'url(images/btn_header.png)'});	});
	$(obj).css({backgroundImage:'url(images/btn_header_over.png)'});
}

function setBusy(container){
	if(gebi(container))	{
		if(container!='chat_wnd' && container!='chat_users' && container!='chat_private_container') 
			$('#'+container).html('<img src="images/loading.gif" width="42" height="42" style="position:absolute; left:'+ Math.round($('#'+container).width() / 2 - 24 ) +'px; top:'+Math.round($('#'+container).height() / 2 - 24 )+'px" />');
	}
}

