// JavaScript Document

var sendReq = getXmlHttpRequestObject();
var receiveReq = getXmlHttpRequestObject();
var lastMessage = 0;
var mTimer;

var call_loc = "";
var call_dbname = "";
var call_dbid = "";
var call_dbsel = "";
var call_dbedit = "";
var call_dbidval = "";



function updateContent ( idname, newval ){

	document.getElementById(idname).value = newval;
	document.getElementById('div_search').style.display = "none";

}

//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		document.getElementById('p_status').innerHTML = 
		'Status: Cound not create XmlHttpRequest Object.' +
		'Consider upgrading your browser.';
	}
}


function getAjaxReq ( loc, cmd, param ){

	if (cmd.length > 0 && param.length > 0 ){
		if (receiveReq.readyState == 4 || receiveReq.readyState == 0 ) {
			receiveReq.open("GET", loc + '_ajax/myAjax.php?cmd='+cmd+"&"+param , true);
			receiveReq.onreadystatechange = handleAjaxReceive; 
			receiveReq.send(null);
		}			
	
	}
}

function getShippingMethod (loc, shipSelObj){

	shipID = shipSelObj.value;
	getAjaxReq ( loc, "checkoutTotal", "shipID=" + shipID);

}

function removeFrag( loc ,cartID, caid, catid , pid ){	
	getAjaxReq ( loc, "removeFrag", "cartID=" + cartID + "&ca_id=" + caid+"&catid="+catid+"&pid="+pid);
}

function promptCallback(val) {   

	idname = "id_"+call_dbsel+"_msg";

	if (val.length > 0){
		getAjaxReq ( call_loc, "table_set", "t="+call_dbname+"&tid="+call_dbid+"&tv="+call_dbsel+"&v="+call_val+"&tf="+call_dbedit+"&p="+val+"&defVal="+call_defValue+"&selBox="+call_dbidval );
	}else {

		document.getElementById(idname).innerHTML = "<br><small style='color:#e83c30'>Request Cancelled!</small>";		
		document.getElementById(call_dbidval).value = call_defValue;
	}

	
	call_loc = "";
	call_dbname = "";
	call_dbid = "";
	call_dbsel = "";
	call_dbedit = "";
	call_dbidval = "";
	call_defValue = "";
	call_val = "";

}


function ajaxSaveEmail ( loc, t){


	fName = document.getElementById("name").value;
	fEmail = document.getElementById("email").value;
	
	document.getElementById("sendrequest").innerHTML = "<small><blink>processing...</blink></small>";

	if (t == null) t = 0;

	getAjaxReq ( loc, "add_email", "name="+fName+"&email="+fEmail+"&t="+t);

}

//CART FUNCTIONS ===========

function addFrag (loc, cartID, caid ){

	getAjaxReq ( loc, "addfrag", "&catID="+caid+"&cartID="+cartID);

}


function changeBasketFrag ( loc, cartID,cart_row,basket_id,catid,prodid){

	if ( document.getElementById("sb_basket_prod:"+basket_id) !== null ) pid = document.getElementById("sb_basket_prod:"+basket_id).value;
	getAjaxReq ( loc, "changeBasketFrag", "&cartID="+cartID+"&bid="+basket_id+"&pid="+pid+"&cart_row="+cart_row+"&catid="+catid+"&prodid="+prodid);
}

function changeCartFrag ( loc, cartID,caid,ppid,catID,prodID ){


	if ( document.getElementById("frag_sel:"+ppid) !== null ) pid = document.getElementById("frag_sel:"+ppid).value;
	if ( document.getElementById("qty_sel:"+ppid) !== null ) qty = document.getElementById("qty_sel:"+ppid).value;
	if ( document.getElementById("size_sel:"+ppid) !== null ) size = document.getElementById("size_sel:"+ppid).value;

	getAjaxReq ( loc, "changeCartFrag", "&caid="+caid+"&cartID="+cartID+"&catid="+catID+"&ppid="+ppid+"&pid="+pid+"&qty="+qty+"&size="+size);
}

//==============================


function getCartPrice ( loc , p_id ){

	if ( document.getElementById("prod_qty:"+p_id) !== null ) qty = document.getElementById("prod_qty:"+p_id).value;
	if ( document.getElementById("prod_sel:"+p_id) !== null ) size = document.getElementById("prod_sel:"+p_id).value;

	getAjaxReq ( loc, "getCartPrice", "pid="+p_id+"&qty="+qty+"&size="+size);
}


function cancelBlackout(){

	document.getElementById("blackout").style.display="none";

}

function showProdDetails ( loc , p_id ){


/*
	height = (document.body.offsetHeight<screen.height) ? screen.height+'px' : document.body.offsetHeight+20+'px';
	
	width = document.body.offsetWidth;
	hOfs = document.documentElement.scrollTop;

	
	getAjaxReq ( loc,"prodDetails","ht="+height+"&wi="+width + "&htOfs="+hOfs + "&pid=" + p_id );	 
	document.getElementById("blackout").style.display="";
*/
	window.location = "details.php?pid="+p_id;
}


function addcart ( loc , p_id ){

	if ( document.getElementById("prod_qty:"+p_id) !== null ) qty = document.getElementById("prod_qty:"+p_id).value;
	if ( document.getElementById("prod_sel:"+p_id) !== null ) size = document.getElementById("prod_sel:"+p_id).value;
	window.location = loc + "addcart.php?id="+p_id+"&qty="+qty+"&s="+size;
}


function changeCheckoutState(){

	//alert("yo");

}

function handleAjaxReceive(){

	if (receiveReq.readyState == 4) {
		
		var ret = receiveReq.responseText;

        if (ret == "reload"){
			//document.location.reload();
            window.location = "/cart.php";
		}
		
		var retArr = ret.split('~');
		
		//alert(ret);
		for (i = 0;i < retArr.length;i+=2){

			if (document.getElementById(retArr[i]).type == "select-one") {
				document.getElementById(retArr[i]).value = retArr[i+1];	
			}else {
				document.getElementById(retArr[i]).innerHTML = retArr[i+1];	
			}

		}
		
	}
		
}