﻿var xmlHttp;
var message = null;

function getValue(id) {
	var obj = document.getElementById(id);
	if (obj) {
		return encodeURIComponent(obj.value) || '';
	}
}

function setLock(value){

	var url = "ajax/lock.php?lock=" + encodeURIComponent(value);

	xmlHttp=GetXmlHttpObject(noAction);
	xmlHttp.open("POST", url , true);
	xmlHttp.send(null);
	return false;

}


function noAction()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {

		// Front Actions Not Needed 

	}
}

function quotate() {

	document.getElementById('quotate').disabled = true;

	var customer		= getValue('customer');
	var custContact		= getValue('custContact');
	var custPhone		= getValue('custPhone');
	var custMobile		= getValue('custMobile');
	var custEmail		= getValue('custEmail');
	
	var supplier		= getValue('supplier');
	var suppCountry		= getValue('suppCountry');
	var suppCity		= getValue('suppCity');
	var suppAddress		= getValue('suppAddress');
	var suppState		= getValue('suppState');
	var suppPostalCode	= getValue('suppPostalCode');
	var suppContact		= getValue('suppContact');
	var suppPhone		= getValue('suppPhone');
	var suppMobile		= getValue('suppMobile');
	var suppEmail		= getValue('suppEmail');

	var p = getValue('piece1');
	p += ':@:' + getValue('piece2');
	p += ':@:' + getValue('piece3');
	p += ':@:' + getValue('piece4');
	p += ':@:' + getValue('piece5');
	
	var w = getValue('weight1');
	w += ':@:' + getValue('weight2');
	w += ':@:' + getValue('weight3');
	w += ':@:' + getValue('weight4');
	w += ':@:' + getValue('weight5');
	
	var d = getValue('dimensions1');
	d += ':@:' + getValue('dimensions2');
	d += ':@:' + getValue('dimensions3');
	d += ':@:' + getValue('dimensions4');
	d += ':@:' + getValue('dimensions5');
	
	var readyDate	= getValue('readyDate');
	var instruction	= getValue('instruction');

	var url = "shipping/quotation.php?customer=" + customer + 
							"&custContact=" + custContact + 
							"&custPhone=" + custPhone + 
							"&custMobile=" + custMobile + 
							"&custEmail=" + custEmail +
							"&supplier=" + supplier + 
							"&suppCountry=" + suppCountry + 
							"&suppCity=" + suppCity + 
							"&suppAddress=" + suppAddress + 
							"&suppState=" + suppState + 
							"&suppPostalCode=" + suppPostalCode + 
							"&suppContact=" + suppContact + 
							"&suppPhone=" + suppPhone + 
							"&suppMobile=" + suppMobile + 
							"&suppEmail=" + suppEmail + 
							"&p=" + p + 
							"&w=" + w + 
							"&d=" + d + 
							"&readyDate=" + readyDate + 
							"&instruction=" + instruction;

	xmlHttp=GetXmlHttpObject(quotateStateChanged);
	xmlHttp.open("POST", url , true);
	xmlHttp.send(null);
	return false;

}

function quotateStateChanged()
{
	message = document.getElementById('quotateMessage');
	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		
		var response = xmlHttp.responseText.split('@');


		if (response[0] == 'OK' || response[0].indexOf("OK") != -1) {
			message.className = "done";
		} else {
			message.className = "err";
			document.getElementById('quotate').disabled = false;
		}

		message.innerHTML = '<b>' + response[1] + '</b>';
		
	}
}

function reload(id) {
	var f = document.getElementById(id);
	f.src = f.src;
}


function process(){

	document.getElementById('msg').innerHTML = '';
	var user = document.getElementById('username');
	var pass = document.getElementById('password');

	if (!user || !user.value || !pass || !pass.value) {
		alert("Invalid Login Parameters!");
		return;
	}

	//var url = "shipping/check_user_ajax.php?username=" + encodeURIComponent(user.value) + "&password=" + encodeURIComponent(pass.value);

	$.ajax( {
		type : "POST",
		url : "shipping/check_user_ajax.php",
		cache : false,
		data : "username=" + encodeURIComponent(user.value) + "&password=" + encodeURIComponent(pass.value),
		dataType : 'json',
		async : false,
		error : function() {
			alert('System error. Reload page and try again!');
		},
		success : function(response) {
			if (response.status == 'OK') {
				document.location.href="shipping/index.php";
			} else {
				var checkResult = document.getElementById('msg');
				checkResult.innerHTML = response.message;
			}
		}
	});

	//xmlHttp=GetXmlHttpObject(userChecked);
	//xmlHttp.open("POST", url , true);
	//xmlHttp.send(null);

	return false;

}


function userChecked()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {

		if (xmlHttp.responseText == 'OK') {
			document.location.href="shipping/index.php";
		} else {
			var checkResult = document.getElementById('msg');
			checkResult.innerHTML = xmlHttp.responseText;
		}

	}
}

function registration(language){
	
	language = language || 'ka';
	
	var citizen = document.getElementById('non_georgian_citizen');
	
	if (language == 'ka') {
		citizen = 1;
	}
	
	var firstname		= getValue('firstname');
	var firstnamege		= getValue('firstnamege');
	var lastname		= getValue('lastname');
	var lastnamege		= getValue('lastnamege');
	var ngc				= citizen == null ? '1' : citizen.checked ? '1' : '0';
	var identno			= getValue('identno');
	var organization	= getValue('organization');
	var mobile			= getValue('mobile');
	var city			= getValue('city');
	var security_code	= getValue('security_code');
	var address1		= getValue('address1');
	var postalcode		= getValue('postalcode');
	var email			= getValue('email');
	var password		= getValue('passwd');
	var retryPassword	= getValue('retry_password');
	var security_code	= getValue('security_code');
	
	var url = "shipping/register.php?firstname=" + firstname + 
									"&firstnamege=" + firstnamege+
									"&lastname=" + lastname + 
									"&lastnamege=" + lastnamege +
									"&non_georgian_citizen=" + ngc +
									"&identno=" + identno +
									"&organization=" + organization + 
									"&mobile=" + mobile + 
									"&city=" + city + 
									"&address1=" + address1 + 
									"&postalcode=" + postalcode + 
									"&email=" + email + 
									"&password=" + password + 
									"&retryPassword=" + retryPassword + 
									"&security_code=" + security_code + 
									"&language=" + language;

	xmlHttp=GetXmlHttpObject(stateChanged);
	xmlHttp.open("POST", url , true);
	xmlHttp.send(null);
	return false;

}

function registrationB(){

	var firstname		= getValue('firstname');
	var firstnamege		= getValue('firstnamege');
	var lastname		= getValue('lastname');
	var lastnamege		= getValue('lastnamege');
	var isOrg			= document.getElementById('type').checked;
	var identno			= getValue('identno');
	var organization	= getValue('organization');
	var mobile			= getValue('mobile');
	var city			= getValue('city');
	var security_code	= getValue('security_code');
	var address1		= getValue('address1');
	var postalcode		= getValue('postalcode');
	var email			= getValue('email');
	var password		= getValue('passwd');
	var retryPassword	= getValue('retry_password');
	var security_code	= getValue('security_code');

	var url = "shipping/registerB.php?firstname=" + firstname + 
									"&firstnamege=" + firstnamege+
									"&lastname=" + lastname + 
									"&lastnamege=" + lastnamege +
									"&type=" + (isOrg ? 'org' : 'ind') +
									"&identno=" + identno + 
									"&organization=" + organization + 
									"&mobile=" + mobile + 
									"&city=" + city + 
									"&address1=" + address1 + 
									"&postalcode=" + postalcode + 
									"&email=" + email + 
									"&password=" + password + 
									"&retryPassword=" + retryPassword + 
									"&security_code=" + security_code;

	xmlHttp=GetXmlHttpObject(stateChanged);
	xmlHttp.open("POST", url , true);
	xmlHttp.send(null);
	return false;

}

function checkOrg() {
	var org = document.getElementById('organization');
	var lab = document.getElementById('identnolabel');

	lab.innerHTML = (org.disabled ? "საიდენტიფიკაციო კოდი (9 ციფრი)" : "პირადი ნომერი (11 ციფრი)");
	org.disabled = !org.disabled;

}

function checkEmail(email){

	var url = "shipping/ajax/checkemail.php?email=" + encodeURIComponent(email);

	xmlHttp=GetXmlHttpObject(checkEmailResponse);
	xmlHttp.open("POST", url , true);
	xmlHttp.send(null);
	return false;

}


function checkEmailResponse()
{
	message = document.getElementById('message');
	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		
		var response = xmlHttp.responseText;

		if (response == 'FAIL') {
			message.className = "default";
			while(message.firstChild) { 
				message.removeChild(message.firstChild); 
			}
		} else {
			message.className = "err";
			message.innerHTML = '<b>' + response + '</b>';
		}
	
	}
}


function resetPassword(lang) {
	var email			= getValue('email');
	var security_code	= getValue('security_code');

	var url = "shipping/resetpassword.php?email=" + email + "&security_code=" + security_code + '&lang=' + (lang || 'ka');

	xmlHttp=GetXmlHttpObject(resetPasswordResponse);
	xmlHttp.open("POST", url , true);
	xmlHttp.send(null);
	return false;
}

function resetPasswordResponse()
{
	message = document.getElementById('pwdRstMessage');

	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		
		var response = xmlHttp.responseText.split('@');


		if (response[0] == 'OK') {
			message.className = "done";
			document.getElementById('resetPassword').disabled = true;
		} else {
			message.className = "err";
		}
		if (response[2] && response[2] == 'reload') {
			reload('pwdRstCaptcha');
		}
		message.innerHTML = '<b>' + response[1] + '</b>';
		

	}
}


function stateChanged()
{
	message = document.getElementById('message');
	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		
		var response = xmlHttp.responseText.split('@');


		if (response[0] == 'OK' || response[0].indexOf("OK") != -1) {
			message.className = "done";
			document.getElementById('register').disabled = true;
		} else {
			message.className = "err";
		}
		if (response[2] && response[2] == 'reload') {
			message.innerHTML = '<b>' + response[1] + '</b>';
			reload('captcha');
		} else {
			message.innerHTML = '<b>' + response[1] + '</b>';
		}
		

	}
}


function stateChanged2()
{
	message = document.getElementById('message');

	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		
		var response = xmlHttp.responseText.split('@');


		if (response[0] == 'OK') {
			message.className = "okStyle";
			document.getElementById('register').disabled = true;
		} else {
			message.className = "errStyle";
		}
		if (response[2] && response[2] == 'reload') {
			reload();
		}
		message.innerHTML = '<b>' + response[1] + '</b>';
		

	}
}

function GetXmlHttpObject(handler){

	var objXmlHttp=null;
	if (navigator.userAgent.indexOf("Opera")>=0) {
		objXmlHttp=new XMLHttpRequest();
		objXmlHttp.onload=handler;
		objXmlHttp.onerror=handler;
		return objXmlHttp;
	}

	if (navigator.userAgent.indexOf("MSIE")>=0) {

		var strName="Msxml2.XMLHTTP";
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0) {
			strName="Microsoft.XMLHTTP";
		}
		
		try {
			objXmlHttp=new ActiveXObject(strName);
			objXmlHttp.onreadystatechange=handler;
			return objXmlHttp;
		} catch(e) {
			alert("Error. Scripting for ActiveX might be disabled");
			return;
		}
	}
	
	if (navigator.userAgent.indexOf("Mozilla")>=0) {
		
		objXmlHttp=new XMLHttpRequest();
		objXmlHttp.onload=handler;
		objXmlHttp.onerror=handler;
		return objXmlHttp;

	}
}

function enter(e) {
	var code = e.keyCode || e.which;
	if (code == 13) {
		document.getElementById('enterButton').click();
	}
}

function setCookie(c_name, value, expiredays) {

	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value)
			+ ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());

}

function getCookie(c_name) {

	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1)
				c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
	return "";
}

