var countBoolean=true;
var tmpQty="";
var tmpCount=0;
function defineVar(val){
var tmpval="";
tmpCount = tmpCount + 1;
 if(tmpCount == 1){
  tmpval = "dtl_qty" + val;
   for (i = 0; i < document.CALFORM.elements.length; i++){
    if (document.CALFORM.elements[i].value == tmpval){
     tmpQty = document.CALFORM.elements[i+1].value;
    }
   }
 }
}
function setBoolean(){
 countBoolean=false;
}

function updateQty(val){
var tempval = "";
var tempval2 = 0;
var tempval3 = 0;
var tempval4 = 0;
if(countBoolean){
 tempval = "dtl_qty" + val;
  for (i = 0; i < document.CALFORM.elements.length; i++){
   if (document.CALFORM.elements[i].value == tempval){
    tempval2 = document.CALFORM.elements[i+1].value;
    tempval3 = tempval2 - 0;
    tempval4 = tempval3 + 1;
    tempval5 = tempval4 + "";
    document.CALFORM.elements[i+1].value = tempval5;
    document.CALFORM.submit();
   }
  }
}
if(countBoolean == false){
 document.CALFORM.submit();
}
}
function deleteQty(val){
var tempval = "";
var tempval2 = 0;
var tempval3 = 0;
var tempval4 = 0;
if(countBoolean){
 tempval = "dtl_qty" + val;
  for (i = 0; i < document.CALFORM.elements.length; i++){
   if (document.CALFORM.elements[i].value == tempval){
    tempval2 = document.CALFORM.elements[i+1].value;
    tempval3 = tempval2 - 0;
    tempval4 = tempval3 - 1;   
    if (tempval4 >=0){
     tempval5 = tempval4 + "";
     document.CALFORM.elements[i+1].value = tempval5;
     document.CALFORM.submit();
    }
    else{
     document.CALFORM.submit();
    }
   }
  }
}
if(countBoolean == false){
 document.CALFORM.submit();
}
}
function deleteLine(val){
var tempval = "";
var tempval2 = 0;
var tempval3 = "";
 tempval = "dtl_qty" + val;
  for (i = 0; i < document.CALFORM.elements.length; i++){
   if (document.CALFORM.elements[i].value == tempval){
    tempval2 = 0;
    tempval3 = tempval2 + "";
     document.CALFORM.elements[i+1].value = tempval3;
     document.CALFORM.submit();
   }
  }
} 

var S1_List = 1;
var S2_List = 1;
var locationMethods = new Array();

//Whenever user change ship location or method, the select attribute OnChange calls following fuctions.

function onShippingChange()
{
  check_shipping();
  document.CALFORM.submit();
}

function check_shipping()
{
	var passThrough = 0;
	var pname = "";

	// Begining of Shipping Table Info
	var locList = new Array();
	var methList = new Array();
locList[0] = "Africa"; methList[0] = "UPS International";
locList[1] = "Alaska and Hawaii"; methList[1] = "UPS 2nd Day,UPS Overnight,Economy Postal Mail";
locList[2] = "Australia and New Zealand"; methList[2] = "UPS International";
locList[3] = "Canada"; methList[3] = "Purolator,UPS International";
locList[4] = "Caribbean"; methList[4] = "UPS International";
locList[5] = "Europe"; methList[5] = "UPS International";
locList[6] = "Japan and Asia"; methList[6] = "UPS International";
locList[7] = "Lower 48 States"; methList[7] = "UPS Ground,USPS Priority Mail,UPS 2nd Day,UPS Overnight,Economy Postal Mail";
locList[8] = "Mexico"; methList[8] = "UPS International";
locList[9] = "Middle East"; methList[9] = "UPS International";
locList[10] = "Puerto Rico"; methList[10] = "UPS 2nd Day,UPS International";
locList[11] = "South America"; methList[11] = "UPS International";
	// End of Shipping Table Info
  for(i=0; i<document.CALFORM.elements.length; i++){
    if(document.CALFORM.elements[i].name == "p_name"){
			pname = document.CALFORM.elements[i].value;
      if(pname == "S1"){
        if(S1_List == 1){
          for(j=0; j<document.CALFORM.elements[i+1].options.length; j++){
            if(document.CALFORM.elements[i+1].options[j].selected){
							for(z=0; z < locList.length; z++){
								if (locList[z] == document.CALFORM.elements[i+1].options[j].text){
									get_locationMethods(methList[z]);
									passThrough = 1;
								}
							}
						}
					}
				}
			}
			else{
			 if(pname == "S2"){
			   if(S2_List == 1){
			     if (passThrough > 0){
  			     for(j=0; j<document.CALFORM.elements[i+1].options.length; j++){
	             if(document.CALFORM.elements[i+1].options[j].selected){
			         var selectedMethod = document.CALFORM.elements[i+1].options[j].text;
               var exists = 0;
							 for(w=0; w <locationMethods.length; w++){
                  if(selectedMethod == locationMethods[w]){
                    exists = 1;
									}
							 }
							 if(exists == 0){
                  var errMessage = "We're sorry.\n\nWe do not use ";
									errMessage += selectedMethod;
									errMessage += " to ship to your location."
									if (locationMethods.length > 1){
									   errMessage += "\n\nThe option/s available for this location are:\n";
									   for(m = 0; m < locationMethods.length - 1; m++){
												errMessage +=locationMethods[m] + ", ";
									   }
									   errMessage += "and " + locationMethods[locationMethods.length-1];
									 }
									 errMessage += "\n\nFor now, we will calculate your charge using ";
									 errMessage += locationMethods[0];
									 for(v=0; v<document.CALFORM.elements[i+1].options.length; v++){
                      if(document.CALFORM.elements[i+1].options[v].text == locationMethods[0]){
  											locIndex = v;
										  }
									 }
									 alert(errMessage);
									 document.CALFORM.elements[i+1].selectedIndex = locIndex;
   								 return;
								  }
							 }
	           }
					 }
				  }
			   }
		  }
	 }
  }
}
//-----------------------------------------------------------------------------------------------------
function get_locationMethods(methString)
{
	locationMethods.length = 1;
	var methIndex = 0;
	var currLoc = "";
	for(y=0; y <= methString.length; y++)
	{
		if(methString.charAt(y) != "," && y != methString.length)
		{
			currLoc += methString.charAt(y);

		}
		else
		{
			for(x=0; x < currLoc.length; x++)
			{
				if(currLoc.charAt(x) != " ")
				{
					currLoc = currLoc.substr(x)
					x=currLoc.length;
				}
			}
			for(x=currLoc.length; x > 0; x--)
			{
				if(currLoc.charAt(x-1) != " ")
				{
					currLoc = currLoc.substr(0,x);
					x = 0;
				}
			}
			locationMethods[methIndex]=currLoc;
			methIndex++;
			currLoc = "";
		}
	}
}
//-----------------------------------------------------------------------------------------------------

