var reqObj = null;
function createRequest(){
	try {
	   reqObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (err)	{
			try {
			reqObj = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (err2) {
			try {
			   reqObj = new XMLHttpRequest();
			}
			catch (err3) {
			   reqObj = null;
			}
		}
	}
	return reqObj;
}
function addCart1(prod,price,qty,ckname){
	xmlhttp = createRequest();
	if(xmlhttp != null){
		var att_val = getattributes(ckname);
		var param    = 'pid=' + prod + '&qty=' + qty + '&price=' + price  + '&action=add2cart';
		
		if(att_val != '')
			param += '&atts=' + att_val;
		
		xmlhttp.open("POST", "add2cart.php?rand="+Math.random(1,10), true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", param.length);
		xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.onreadystatechange = echoCart;
		xmlhttp.send(param);
	}
}

function delCart(cbid) {
	xmlhttp = createRequest();
	if(xmlhttp != null){
		var param    = 'cbid=' + cbid  + '&action=del';
		xmlhttp.open("POST", "add2cart.php?rand="+Math.random(1,10), true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", param.length);
		xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.onreadystatechange = echoCart;
		xmlhttp.send(param);
	}
}

function showCart() {
	xmlhttp = createRequest();
	if(xmlhttp != null){
		var param    = '';
		xmlhttp.open("POST", "add2cart.php?rand="+Math.random(1,10), true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", param.length);
		xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.onreadystatechange = echoCart;
		xmlhttp.send(param);
	}
}


function emptyCart(cbid) {
	if(confirm("Are you sure to empty cart?")){
		xmlhttp = createRequest();
		if(xmlhttp != null){
			var param    = '&action=delall';
			xmlhttp.open("POST", "add2cart.php?rand="+Math.random(1,10), true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.setRequestHeader("Content-length", param.length);
			xmlhttp.setRequestHeader("Connection", "close");
			xmlhttp.onreadystatechange = echoCart;
			xmlhttp.send(param);
		}
	}
}

function echoCart(){
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			var text1  = xmlhttp.responseText;
			document.getElementById('mini_cart_result').innerHTML = text1;
		}
	}
	return true;
}

function getattributes(chkname){
	var frm = document.mnufrm
	var att_arr = '';
	element_len = frm.elements.length;
	for (i=0; i<element_len; i++){
		this_element = frm.elements[i]; 
		if (this_element.type == 'checkbox' && this_element.checked == true && this_element.name.match(chkname)){
			att_arr += this_element.value + ',';
		}
		if (this_element.type == 'select-one' && this_element.value != '' && this_element.name.match(chkname)){
			att_arr += this_element.value + ',';
		}
	}

	return att_arr.substring(0, (att_arr.length-1));
}


function windowopen(URL,widthwin,heightwin){
	 var screenH = screen.height;
	 var screenW = screen.width;
	 var fromLeft = parseInt((screenW-widthwin)/2);
	 var fromTop = parseInt((screenH-heightwin)/2);
	var newwin=window.open(URL,'invitation_guest_email','width='+widthwin+',height='+heightwin+',toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes,top='+fromTop+',left='+fromLeft)
	if(window.focus) newwin.focus();
}

function chk_availabilty(chkval){				
	if(chkval != ''){
		xmlhttp = createRequest();
		if(xmlhttp != null){
			xmlhttp.open("GET", "postcode.php?tochk="+chkval+"&rand="+Math.random(1,10), true);
			xmlhttp.onreadystatechange = post_chk_availabilty;
			xmlhttp.send(null);
		}
	}
	else{
		alert("Enter your postcode");
		return;
	}
}

function post_chk_availabilty(){
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			var text1  = xmlhttp.responseText;
			document.getElementById('chkresult').innerHTML = text1;
		}
	}
	return true;
}

function postcode_checker(pcode){
	if(pcode != ''){
		xmlhttp = createRequest();
		postcode_status = '';
		if(xmlhttp != null){
			document.getElementById('chkresult').innerHTML = '';
			document.getElementById('chkresult').innerHTML = "Validating Postcode...";
			xmlhttp.onreadystatechange = doValidate;
			xmlhttp.open("GET", "SimplyLookup/ajx_postcode_checker.php?postcode="+pcode+"&rand="+Math.random(1,10), true);
			xmlhttp.send(null);
		}
	}
	else{
		alert("Enter your postcode");
		return;
	}
}

function doValidate(){
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			var text1  = xmlhttp.responseText;

			if(text1 == 0){
				document.getElementById('chkresult').innerHTML = 'Invalid postcode!';
			}
			else{
				document.getElementById('chkresult').innerHTML = text1;
			}
		}
	}
}


function postcode_validate(pcode){
	if(pcode != ''){
		xmlhttp = createRequest();
		postcode_status = '';
		if(xmlhttp != null){
			xmlhttp.open("GET", "SimplyLookup/ajx_postcode_checker.php?postcode="+pcode+"&rand="+Math.random(1,10), true);
			xmlhttp.onreadystatechange = pValidate;
			xmlhttp.send(null);
		}
	}
}
var postchkstatus=0;
function pValidate(){
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			var text1  = xmlhttp.responseText;

			if(text1 == 0){
				//alert('Invalid postcode!');
				postchkstatus=1;
			}
			else{
				postchkstatus=0;
			}
		}
	}
}