// Script to contain common JS functions

/* adds product to session var on secure domain */
function add(secure_domain, id)
{
	var width=400;
	var height=160;
	var left = (screen.availWidth - width) / 2;
	var top = 300;
	var url = 'http://'+secure_domain+'/add.php?id='+id;
	var args = 'left='+left+',top='+top+',height='+height+',width='+width+',resizable=yes,menubars=no,status=no,scrollbars=auto';
	
	//alert(url);return (false);

	window.open( url, '_cart', args );
	
	return(false);
}

function gotoOrder()
{
   window.location = "./?action=checkout";
}

function openBasket(url)
{
   var width=540;
   var height=500;
   var left=(screen.availWidth - width) / 2;
   var top=(screen.availHeight - height) / 2;
   var args = "top="+top+",left="+left+",width="+width+",height="+height+",status=no,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no";
   window.open(url, "_basket", args);
   return false;
}

function viewImage ($image_url, width, height) {
	var left=(screen.availWidth - width) / 2;
	var top=(screen.availHeight - height) / 2;
	var args = "top="+top+",left="+left+",width="+width+",height="+height+",status=no,resizable=yes,scrollbars=auto,toolbar=no,menubar=no,location=no";
	window.open(url, "_blank", args);
	return false;

}

function view_larger(image, width, height)
{
	margin = 20;
	width = parseInt(margin+width);
	height = parseInt(margin+height);
	
	toppos = (screen.availHeight - height)/2;
	leftpos = (screen.availWidth - width)/2;
	
	args = "resizable=yes,status=no,scrollbars=auto,left="+leftpos+",top="+toppos+",width=" + width + ",height=" + height;
	//alert(args);
	
	window.open("view_image.php?img="+image, "_image", args);
}

function updateQtyRange(theSelect, productId, script)
{
   selectIndex = theSelect.selectedIndex;
   //alert(script);
   var location = script + "?action=update&id=" + productId + "&qty=" + selectIndex;
   window.location = location;
}

function checkFields(theForm)
{
    for (var ind in reqFieldNames ) {
        var field = eval("theForm.elements['" + reqFieldNames[ind] + "']");
        if (field.type == "text") {
            if(isEmpty(field.value)) {
                alert("Please enter a value for " + field.name);
                field.focus();
                return(false);
            }
        } else if (field.type == "radio") {
            if (!isRadioChecked(field)) {
                alert("Please select a option for " + field.name);
                field.focus();
                return(false);
            }
        }
    }
    
    // we passed so submit the form
    return(true);
}

// Check to see if anything is left after whitespace has gone
function isEmpty(str)
{
    if(str.replace(/\s*/g,"") == "") {
        return(true)
    }
    return(false);
}

function isRadioChecked(ele)
{
    for (i=0; i<ele.length; i++) {
        if (ele[i].checked) {
            return(true);
        }
    }
    return(false);
}

function openWin (url, settings)
{
      window.open(url, "_blank", settings);
      return false;
}