
//stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject()
{
    // will store the reference to the XMLHttpRequest object
    var xmlHttp;
    // if running Internet Explorer
    if(window.ActiveXObject)
    {
        try
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp = false;
        }
    }
    // if running Mozilla or other browsers
    else
    {
        try
        {
            xmlHttp = new XMLHttpRequest();
        }
        catch (e)
        {
            xmlHttp = false;
        }
    }
    // return the created object or display an error message
    if (!xmlHttp)
        alert("Error creating the XMLHttpRequest object.");
    else
        return xmlHttp;
}

function handleServerResponse()
{
//  move forward only if the transaction has completed
    if (xmlHttp.readyState == 4)
    {
//      status of 200 indicates the transaction completed successfully
        if (xmlHttp.status == 200)
        {
//          restart sequence
            setTimeout('process()', 1000);
        }
//      a HTTP status different than 200 signals an error
        else
        {
            alert("There was a problem accessing the server: " + xmlHttp.statusText);
        }
    }
}

function onMouseOverDrawing(id) {
	tid = "t" + id;
	var p = "border";
	var v = "1px solid #0000FF";

	var n = document.getElementById(tid);
	n.style[p] = v;
}

function onMouseOutDrawing(id) {
	tid = "t" + id;
	var p = "border";
	var v = "1px solid #808080";

	var n = document.getElementById(tid);
	n.style[p] = v;
}

function categoryThumbnailsLayoutOver(id) {
	tid = "t" + id;
	var p = "border";
	var v = "1px solid #0000FF";

	var n = document.getElementById(tid);
	n.style[p] = v;

	tlid = "tl" + id;
	var p = "background";
	var v = "#0000FF";

	var n = document.getElementById(tlid);
	n.style[p] = v;

	var p = "color";
	var v = "#FFFFFF";

	var n = document.getElementById(tlid);
	n.style[p] = v;

}

function categoryThumbnailsLayoutOut(id) {
	tid = "t" + id;
	var p = "border";
	var v = "1px solid #808080";

	var n = document.getElementById(tid);
	n.style[p] = v;

	tlid = "tl" + id;
	var p = "background";
	var v = "#FFFFFF";

	var n = document.getElementById(tlid);
	n.style[p] = v;

	var p = "color";
	var v = "#0000FF";

	var n = document.getElementById(tlid);
	n.style[p] = v;

}

function Trim(myval)
{
	var chklen=myval.length; 
	var pos=0;
	mychar = myval.charAt(0);

	while(pos>=0 || lstpos >=0)
	{
		pos=myval.indexOf(" ");
		if (pos==0)
		{
			myval=myval.substring(1,chklen);
			chklen = myval.length;
			mychar = myval.charAt(0);
		}
		lstpos=myval.lastIndexOf(" ");

		if (lstpos==chklen-1)
		{	
			myval=myval.substring(0,chklen-1);
			chklen=myval.length;
			mychar = myval.charAt(chklen-1);
		}
		
		if(mychar!=" ")
			break;

	}
	return myval;			
}

function updateBdQty(uniqId,dbQty)
{
    //alert('here');return false;
    //proceed only if the xmlHttp object isn't busy
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
    {
        
        // execute the quickstart.php page from the server
        //alert(updateLink + "drawingId/" + uniqId + "/qty/" + dbQty);//return false;
        xmlHttp.open("GET", updateLink + "drawingId/" + uniqId + "/qty/" + dbQty, true);
        // define the method to handle server responses
        //xmlHttp.onreadystatechange = handleServerResponse;
        // make the server request
        xmlHttp.send(null);
    }
    else
    {
        // if the connection is busy, try again after one second
        setTimeout('process()', 1000);
    }
}

function updateCartTotal(cId,qelemId,elemId,qntt,prc)
{
    if(qntt >= 1 && qntt < 10)
    {
       
        var elem = eval('document.getElementById("'+elemId+'")');
        var cTotal = 0;
        cTotal = parseInt(qntt) * parseInt(prc);
        elem.value = parseInt(cTotal);
        var amountTotal = 0;
        
        for(i=1;i<=itemCnt;i++)
        {
            var temp = "quantity_"+i;
            tempElem =  eval('document.getElementById("'+temp+'")');
            amountTotal =  parseInt(parseInt(amountTotal) + parseInt(parseInt(tempElem.value) * parseInt('15')));
        }
        document.getElementById('spanAmount').innerHTML = parseInt(amountTotal);

        updateBdQty(cId,qntt);
        //document.getElementById('amount').value = parseInt(amountTotal);
    }
    else
    {
        alert("Quantity should be between 1 to 9 only.");
        var qtyElem = eval('document.getElementById("'+qelemId+'")');
        
        window.setTimeout(function ()
        {
            qtyElem.focus();
        }, 0);
    }
}


function disablePaypal()
{
    var imgObj = document.getElementById("btnPaypal");
    imgObj.disabled = true;
}

/*
function checkQuantity(qEleId)
{
    var qelem = eval('document.getElementById("'+qEleId+'")');
    var qty = qelem.value;
    if(qty<1 && qty >=10)
    {
        window.setTimeout(function ()
        {
            qelem.focus();
        }, 0);
    }
}
*/

function addCartInfo()
{
	// Create Ajax request in order to save cart data in to the database table
	//alert('here');return false;
	//proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
		// execute the quickstart.php page from the server
		//alert(updateLink + "drawingId/" + uniqId + "/qty/" + dbQty);//return false;
		xmlHttp.open("GET", addCartInfoStoreUrlLink, true);
		// define the method to handle server responses
		//xmlHttp.onreadystatechange = handleServerResponse;
		
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
				document.getElementById('payment').submit();
			//	return true;
			}
		}
		
		// make the server request
		xmlHttp.send(null);
		return false;
	}
}

