﻿
function addToCart(handbagId, handbagQty, handleId, handleQty, theButton) {
    
    var hbQ = document.getElementById(handbagQty).value;
    if(handleId != '') {
        var hdQ = document.getElementById(handleQty).value;
    }
    
    if(hbQ == 0 && hdQ == 0) {
        alert('INVALID QUANTITY\nPlease select at least 1 item to add to your cart!');
    } else {
        //alert('Purse: '+hb+', Qty: '+hbq+', Handle: '+hd+', Qty: '+hdq);

        xmlHttp=GetXmlHttpObject()
	    if (xmlHttp==null) {
		    alert ("Your browser does not support AJAX!");
		    return;
	    }
	    
	    if(isNaN(handbagId)) {
	        if(hbQ == 0 || hbQ == '') {
                alert('INVALID QUANTITY\nPlease select at least 1 item to add to your cart!');
                return;
	        }
	    }
	    
	    var strURL1 = '';
	    if(hbQ > 0) {
	        if(isNaN(handbagId)) {
	            if(document.getElementById(handbagId).value == '') {
	                alert('INVALID REQUEST\nPlease select a color option to add to your cart!');
	                return;
	            } else {
	                strURL1 = 'ProductNo1='+document.getElementById(handbagId).value+'&Qty1='+hbQ;
	            }
	        } else {
	            strURL1 = 'ProductNo1='+handbagId+'&Qty1='+hbQ;
	        }
	    }
	    var strURL2 = '';
	    if(hdQ > 0) {
	        if(strURL1 != '') {
	            strURL2 = '&ProductNo2='+handleId+'&Qty2='+hdQ;
	        } else {
	            strURL2 = 'ProductNo2='+handleId+'&Qty2='+hdQ;
	        }
	    }

	    var url="/ajax/cartAddition.aspx?process=add&"+strURL1+strURL2;
	    //alert(url);
	    //http://www.juxtaposie.com/ajax/cartAddition.aspx?ProductNo1=211455&Qty1=2&ProductNo2=211887&Qty2=1

	    xmlHttp.onreadystatechange = stateChanged;
	    xmlHttp.open("GET",url,true);
	    xmlHttp.send(null);	    

	    document.getElementById(theButton).innerHTML = '<img src="/images/btnCartAdded.gif" alt="Item Added" />';
	    document.getElementById(theButton + 'cart').innerHTML = '<strong><a href="/store/cart.aspx">View Cart</a></strong>';
        setTimeout("switchBack('"+theButton+"');", 3000);
    }
}
function stateChanged() {
	if (xmlHttp.readyState==4) {
        var returnValue = xmlHttp.responseText;
		//alert(returnValue);
   	}
}


function switchBack(who) {
    document.getElementById(who).innerHTML = '<img src="/images/btnCart.gif" alt="Add to Cart" />';
}
function fillZero(who) {
    var checkValue = document.getElementById(who).value
    if(checkValue == '' || isNaN(checkValue)) {
        document.getElementById(who).value = '0';
    }
}

function passValue(who, what) {
    document.getElementById(who).value = what;
}

function changeQty(who, action) {
    var currentQty = document.getElementById(who).value
    if(action == '+') {
        document.getElementById(who).value = parseFloat(currentQty) + 1
    } else {
        if(parseFloat(currentQty) > 0) {
            document.getElementById(who).value = parseFloat(currentQty) - 1
        }
    }
}


//-----------------------------------
//common to all ajax javascript calls
//-----------------------------------
function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
//end ajax call