var S1_List = 0;
var S2_List = 0;
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 onSelectLocation()
{
  check_shipping();
  document.CALFORM.submit();
}
//--------------------------------------------------------------------------------------------------
function onSelectMethod()
{
  check_shipping();
  document.CALFORM.submit();
}
//-----------------------------------------------------------------------------------------------------

function check_shipping()
{
	var passThrough = 0;
	var pname = "";
	check_options(S1_List, S2_List);

	// Begining of Shipping Table Info
	var locList = new Array();
	var methList = new Array();
  locList[0] = "Africa"; methList[0] = "Priority International Courier";
  locList[1] = "Alaska and Hawaii"; methList[1] = "Priority International Courier";
  locList[2] = "Albania"; methList[2] = "Priority International Courier";
  locList[3] = "Australia and New Zealand"; methList[3] = "Priority International Courier";
  locList[4] = "Austria"; methList[4] = "Standard International,Priority International Courier";
  locList[5] = "Belgium"; methList[5] = "Standard International,Priority International Courier";
  locList[6] = "Bulgaria"; methList[6] = "Standard International,Priority International Courier";
  locList[7] = "Canada"; methList[7] = "Priority International Courier";
  locList[8] = "Cyprus"; methList[8] = "Standard International,Priority International Courier";
  locList[9] = "Czech Republic"; methList[9] = "Standard International,Priority International Courier";
  locList[10] = "Denmark"; methList[10] = "Standard International,Priority International Courier";
  locList[11] = "Estonia"; methList[11] = "Standard International,Priority International Courier";
  locList[12] = "Finland"; methList[12] = "Standard International,Priority International Courier";
  locList[13] = "France"; methList[13] = "Standard International,Priority International Courier";
  locList[14] = "Germany"; methList[14] = "Standard International,Priority International Courier";
  locList[15] = "Greece"; methList[15] = "Standard International,Priority International Courier";
  locList[16] = "Hungary"; methList[16] = "Standard International,Priority International Courier";
  locList[17] = "Ireland"; methList[17] = "Standard International,Priority International Courier";
  locList[18] = "Italy"; methList[18] = "Standard International,Priority International Courier";
  locList[19] = "Japan & Asia"; methList[19] = "Priority International Courier";
  locList[20] = "Latvia"; methList[20] = "Standard International,Priority International Courier";
  locList[21] = "Lithuania"; methList[21] = "Standard International,Priority International Courier";
  locList[22] = "Luxembourg"; methList[22] = "Standard International,Priority International Courier";
  locList[23] = "Malta"; methList[23] = "Standard International,Priority International Courier";
  locList[24] = "Mexico and Caribbean"; methList[24] = "Priority International Courier";
  locList[25] = "Middle East"; methList[25] = "Priority International Courier";
  locList[26] = "Netherlands"; methList[26] = "Standard International,Priority International Courier";
  locList[27] = "Norway"; methList[27] = "Priority International Courier";
  locList[28] = "Poland"; methList[28] = "Standard International,Priority International Courier";
  locList[29] = "Portugal"; methList[29] = "Standard International,Priority International Courier";
  locList[30] = "Romania"; methList[30] = "Standard International,Priority International Courier";
  locList[31] = "Russia"; methList[31] = "Priority International Courier";
  locList[32] = "Slovakia"; methList[32] = "Standard International,Priority International Courier";
  locList[33] = "Slovenia"; methList[33] = "Standard International,Priority International Courier";
  locList[34] = "South America"; methList[34] = "Priority International Courier";
  locList[35] = "Spain"; methList[35] = "Standard International,Priority International Courier";
  locList[36] = "Sweden"; methList[36] = "Standard International,Priority International Courier";
  locList[37] = "Switzerland"; methList[37] = "Priority International Courier";
  locList[38] = "UK"; methList[38] = "Standard International,Priority International Courier";
  locList[39] = "US Lower 48 States"; methList[39] = "Priority International Courier";
  locList[40] = "Yugoslavia"; methList[40] = "Priority International Courier";
	// 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 = "";
		}
	}
}
//-----------------------------------------------------------------------------------------------------

function check_options()
{
  var List_string;
  var S1_flag = 0;
  var S2_flag = 0;
  var S1_key = "onSelectLocation";
  var S2_key = "onSelectMethod";

  for(i=0; i<document.CALFORM.elements.length; ++i)
	{
    if(document.CALFORM.elements[i].name == "p_val")
		{
      if(document.CALFORM.elements[i].type == "select-one")
			{
        List_string += document.CALFORM.elements[i].onchange;
        S1_flag = (List_string.indexOf(S1_key));
        S2_flag = (List_string.indexOf(S2_key));
        if(S1_flag != -1)
				{
          S1_List = 1;
        }
        if(S2_flag != -1)
				{
          S2_List = 1;
        }
      }
    }
  }
}