/** Cart Summary form **/
function update_cart(form){
	var f = form;
	var i = 0;
	//alert(form.elements[0].name);
	for (i=0;i<form.length;i++){
		if ((form.elements[i].name == 'del') && (form.elements[i].checked)) {
			form.elements[i-1].value=0;
		}
	}
	return checkCartNumbers(form); // should always be true
	
}

function checkCartNumbers(theForm) {
  var x = 0;
  for (x=0; x < theForm.elements.length; x++) {
    if ( (theForm.elements[x].type == "text") && !isNumberGtZero(theForm.elements[x].value) ) {
         theForm.elements[x].value=1;
    }
  }
  return true;
}

/** Shipping form **/
function check_shipping(form){

var ZIP_CNT = 5;
var error = "";
var errorFields = Array()

if (form.shipname.value == "") {
	error += "* Ship to name is reqired.\n" 
	errorFields[errorFields.length] = 'label_shipname';
}
  
if (form.shipaddr1.value == "" ) {
	error += "* Shipping address is required.\n" 
	errorFields[errorFields.length] = 'label_shipaddr1';
}

if (form.shipcity.value == "" ) {
    error += "* City in the shipping address is required.\n"
	errorFields[errorFields.length] = 'label_shipcity';
}

if( (form.shipcountry.options[form.shipcountry.selectedIndex].value == "") || (form.shipcountry.selectedIndex <= 0) ) {
    error += "* You must select a country for shipping.\n"
	errorFields[errorFields.length] = 'label_shipcountry';
}

if( form.shipzip.value != "" ) {
    if( (form.shipcountry.options[form.shipcountry.selectedIndex].value == "1") && (is_number( form.shipzip.value, ZIP_CNT )==false)) {
      error += "* The Zip Code must be a " +ZIP_CNT +" digit number.\n"
		errorFields[errorFields.length] = 'label_shipzip';

    }
} else if (form.shipcountry.options[form.shipcountry.selectedIndex].value == "1") {
    error += "* Zip code in the shipping address  is required.\n"
	errorFields[errorFields.length] = 'label_shipzip';

}

if (form.shipcountry.options[form.shipcountry.selectedIndex].value == "1") {
    if (form.shipstate.value == "") {
       error += "* Please specify a state for a US shipping address.\n"
	   errorFields[errorFields.length] = 'label_shipstate';

    }
}
  // check for a selected shippingoption
    var found = 0;
    for (var i=0; i<form.shipmethod.length; i++)
    {
	if (form.shipmethod[i].checked == true)
	{
	   found++;	
	   if (form.shipmethod[i].value != 'INTERNATL' &&
	       form.shipcountry.options[form.shipcountry.selectedIndex].value != 1)
           {
	       error += "* The shipping method for a non-US address MUST be International.\n";
			errorFields[errorFields.length] = 'label_shipcountry';

           }

	   if (form.shipmethod[i].value != 'USMAIL' &&
	       form.shipcountry.options[form.shipcountry.selectedIndex].value == 1)
           {
	       error += "* The shipping method for a US address should be Standard Mail (Ground).\n";
		   errorFields[errorFields.length] = 'label_shipcountry';
           }

	}
    }

	// make sure a shipping option is selected.
if (found == 0) {
error += "* Please select a shipping method for your order.\n";
}

if (error == "") {
	return true;
} else {
error_notify(errorFields)
	alert('Please correct the following errors: \n\n' + error);
	return false;
}
}

function check_billing(form) {
 
var ZIP_CNT = 5;
var status = 1;
var error = "";
var errorFields = Array()

if (form.cardholder.value == "") {
	error += "* Card holder name is reqired.\n"; 
	errorFields[errorFields.length] = 'label_cardholder';
}

if (form.cardnumber.value == "") {
	error += "* Card number is reqired.\n"; 
	errorFields[errorFields.length] = 'label_cardnumber';
} else if (!is_number(form.cardnumber.value, 16)) {
	error += "* Credit card number must be 16 digits only.\n";
	errorFields[errorFields.length] = 'label_cardnumber';
}

if (form.billname.value == "") {
  error += "* Billing name is reqired.\n"; 
	errorFields[errorFields.length] = 'label_billname';
}
  
if (form.billaddr1.value == "" ) {
    error += "* Billing address is required.\n"; 
	errorFields[errorFields.length] = 'label_billaddr1';
}


if (form.billcity.value == "" ) {
    error += "* City in the billing address is required.\n";
	errorFields[errorFields.length] = 'label_billcity';
}

if ( (form.billcountry.options[form.billcountry.selectedIndex].value == "") || (form.billcountry.selectedIndex <= 0) ) {
    error += "* You must select a country for billing\n";
	errorFields[errorFields.length] = 'label_billcountry';
}

if( form.billzip.value != "" ) {
    if( (form.billcountry.options[form.billcountry.selectedIndex].value == "1") && (is_number( form.billzip.value, ZIP_CNT )==false)) {
      error += "* The Zip Code must be a " +ZIP_CNT +" digit number.\n"
	  errorFields[errorFields.length] = 'label_billzip';
    }
} else if (form.billcountry.options[form.billcountry.selectedIndex].value == "1") {
    error += "* Zip code in the billing address  is required.\n";
	errorFields[errorFields.length] = 'label_billcountry';
}

if (form.billcountry.options[form.billcountry.selectedIndex].value == "1") {
    if (form.billstate.value == "") {
       error += "* Please specify a state for a US billing address.\n";
	errorFields[errorFields.length] = 'label_billstate';
    }
}

if (error == "") {
	return true;
} else {
error_notify(errorFields)
	alert('Please correct the following errors: \n\n' + error);
	return false;
}

}


function error_notify(what) {
if (!document.getElementById) return;
 
if (document.modifiedFields) {
	for (var i=0; i<document.modifiedFields.length; i++) {
		var obj = document.getElementById(document.modifiedFields[i])
		if (obj) obj.style.color = document.modifiedFieldsValue[i];
	}
}

if (what.length) {
	document.modifiedFields = what
	document.modifiedFieldsValue = Array()
	for (var i=0;i<what.length;i++) {
		var obj = document.getElementById(what[i]);
		if (obj) {
		//alert(obj)
			document.modifiedFieldsValue[i] = obj.style.color
			obj.style.color = 'red';
	
		}
	}
}
}


function is_number(any_string, max)
{
  var count = 1;
  var str = any_string + "";
  var str_len = str.length;
  var new_str = "";

   for(count=0; count<str_len; count++)
  {
    if( ((str.substring(count,count+1)>= 'A') && (str.substring(count,count+1)<= 'Z')) ||
        ((str.substring(count,count+1)>= 'a') && (str.substring(count,count+1)<= 'z')) )
    {
      return false;
    }    

    if( (str.substring(count,count+1)>= '0') && (str.substring(count,count+1)<='9') ) {
      new_str += str.substring(count,count+1);
      //alert("str[" + count + "]: " + str.substring(count,count+1)); 
    }
  }
  //alert("new string: " + new_str);
  if( new_str.length != max )
    return false;
  else
    return new_str;
}

function pageInitCheckShippingType()
{
    var found = 0;
    for (var i=0; i<document.shipForm.shipmethod.length; i++)
    {
	if (document.shipForm.shipmethod[i].checked == true)
	{ found++; }
    }

    // if nothing has yet to be selected, select the default shipping.
    if (found == 0)
    {
	selectDefaultShipping();
    }
}

function selectDefaultBilling() {
	return;
}
// select the default shipping method.  This should be called when the page
// is reloaded and nothing is selected (i.e. no prior selection), or when
// the country changes from US to non-US or visa versa.
function selectDefaultShipping()
{

    var usCC = 1; // countrycode for US
	var f = document.shipForm

    // check currently selected country
    var countryFormObj = f.shipcountry;
    var selectedCC = countryFormObj.options[countryFormObj.selectedIndex].value;

    var defaultType = "";

    if (selectedCC == usCC)
    {  defaultType = "USMAIL";  }
    else { defaultType = "INTERNATL"; }

    for (var i=0; i<f.shipmethod.length; i++)
    {
	if (f.shipmethod[i].value == defaultType) 
	{ 
	  f.shipmethod[i].checked = true; 
	}
	else { f.shipmethod[i].checked = false; }
    }
}



function isNumberGtZero(teststr) {
   if ( (teststr == "") || (isNaN(Math.abs(teststr)) && (teststr.charAt(0) != '#'))) {
      return false;
   }
   return (0 <= teststr);
}

   function popUp(url) {
      sealWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=450');
      self.name = "mainWin"; }
