/*
Misc javascript functions used throughout the cart
*/

// -------------------------------------------------------------------
function GoTo(thisURL) {
     //alert(thisURL);
     document.location = thisURL;
}
// -------------------------------------------------------------------
function OpenWindow(thisURL,x,y) {
    var gWin;
    if(x == undefined){
        gWin = window.open(thisURL,'GraphicWindow','resizable=0','scrollbars=0');
    }
    else{
        gWin = window.open(thisURL,'GraphicWindow','width='+x+',height='+y+',resizable=0,scrollbars=0');
    }
}

// -------------------------------------------------------------------
function popup(img,h,w,title){

    var sWidth = screen.width / 2 - w;

     var win = window.open("","graphics","top=100,left=" + sWidth + ",scrollbars=0,resizable=1");
     win.resizeTo(w,h + 100);
     win.document.open("text/html", "replace");
     win.document.write("<html><head><title>"+title+"</title></head><body topmargin=0 leftmargin=0 marginwidth=0 marginheight=0>");
     win.document.write("<img src="+img+" width="+w+" height="+h+"><br>");
     win.document.write("<form><div align=center><input type=button name=Close value=\"Close Window\" onclick=window.close()></div></form>");
     win.document.write("</body></html>");
     win.status = "Zoom Image of " + title;
     win.focus();
}

// -------------------------------------------------------------------
function flipIt(elementID){
    if(document.all){
        whichEl = document.all[elementID];
    }
    else{
        whichEl = document.getElementById(elementID);
    }
    disp = whichEl.style.display;
    viz = whichEl.style.visibility;

    if(disp == ""){
        whichEl.style.display = "none";
    }
    elseif(disp == "none"){
        whichEl.style.display = "";
    }
    
    if(viz == "" || viz == "visible"){
        whichEl.style.visibility = 'hidden';
    }
    elseif(viz == "hidden"){
        whichEl.style.visibility = 'visible';
    }
    
    //whichEl.style.display = (whichEl.style.display == "none" ) ? "" : "none";
    //whichEl.style.visibility = 'visible';

}

// -------------------------------------------------------------------
function getStyle(elem, name) {
    // J/S Pro Techniques p136
    if (elem.style[name]) {
        return elem.style[name];
    } else if (elem.currentStyle) {
        return elem.currentStyle[name];
    }
    else if (document.defaultView && document.defaultView.getComputedStyle) {
        name = name.replace(/([A-Z])/g, "-$1");
        name = name.toLowerCase();
        s = document.defaultView.getComputedStyle(elem, "");
        return s && s.getPropertyValue(name);
    } else {
        return null;
    }
}


// -------------------------------------------------------------------
function qtyCheck(form){

	var qtySelected = parseInt(0);

	for(i = 0; i < form.elements.length; i++){

		var fieldName = form.elements[i].name;

		//item[SHOES][quantity]

        if(typeof(fieldName) != 'undefined' && fieldName.indexOf('quantity') !=-1){
        
            var fieldValue = parseInt(form.elements[i].value);
            if(!isNaN(fieldValue) && fieldValue > 0){
            
                qtySelected += fieldValue;

				// get the item_no
				var id = fieldName.replace('][quantity]','');
				id = fieldName.replace('item[','');
				
				if(id != ''){

					// Check availability
					var availFld = 'item[' + id + '][available]';
					var minField = 'item[' + id + '][minimum]';

					var pName = "product";
					var nameFld = 'item[' + id + '][name]';
					
					if(typeof(form.elements[nameFld]) != 'undefined'){
						pName = form.elements[nameFld].value;
						var pattern = /\s{2,}/g;
						pName = pName.replace(pattern,"");
					}

					if(typeof(form.elements[availFld]) != 'undefined'){
						var qtyAvailable = parseInt(form.elements[availFld].value);
						if(fieldValue > qtyAvailable){
							if(qtyAvailable <= 0){
								alert("Sorry, the '" + pName + "' is not available at this time.");
							}
							else{
								alert("There are only " + qtyAvailable + " of the '" + pName + "s' available.\n\n" +
										"Please reduce the quantity so it is equal to or fewer than " + qtyAvailable + " item(s).");
							}
							form.elements[i].value = "";
							form.elements[i].focus();
							return false;
						}
					}
					if(typeof(form.elements[minField]) != 'undefined'){
						var qtyMinimum = parseInt(form.elements[minField].value);
						if(fieldValue < qtyMinimum){
							alert("You must purchase a minimum of " + qtyMinimum + " '" + pName + "s'.");
							form.elements[i].value = "";
							form.elements[i].focus();
							return false;
						}
					}
				}
            }
            //if(fieldValue != '' && isNaN(fieldValue)){
				//alert("You have not entered a valid quantity for the item.");
				//form.elements[i].focus();
				//return false;
            //}
        }
    }

    if(qtySelected == 0){
        alert("You have not entered a quantity of an item to add to the cart.");
        return false;
    }

    return true;
}








