/****************************************
 DSS - Default w/ Intl Mail (2008)
 Shipping Validation Javascript
 Entry-point: SO 289 TT 7 - Shopping Cart
 Ship Master ID: 10.87
 ***************************************/

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.

//--------------------------------------------------------------------------------------------------
//Update button
function onShippingChange()
{
  check_shipping();
  document.CALFORM.submit();
}
//Select OnChange
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] = "International Postal,UPS International";
  locList[1] = "Alaska and Hawaii"; methList[1] = "Economy Postal Mail,UPS 2nd Day,UPS Overnight";
  locList[2] = "Australia and New Zealand"; methList[2] = "International Postal,UPS International";
  locList[3] = "Canada"; methList[3] = "International Postal,UPS Canadian Economy,UPS International";
  locList[4] = "Caribbean"; methList[4] = "International Postal,UPS International";
  locList[5] = "Europe"; methList[5] = "International Postal,UPS International";
  locList[6] = "Japan and Asia"; methList[6] = "International Postal,UPS International";
  locList[7] = "Lower 48 States"; methList[7] = "Economy Postal Mail,UPS Ground,UPS 2nd Day,UPS Overnight";
  locList[8] = "Mexico"; methList[8] = "International Postal,UPS International";
  locList[9] = "Middle East"; methList[9] = "International Postal,UPS International";
  locList[10] = "Puerto Rico"; methList[10] = "International Postal,UPS 2nd Day";
  locList[11] = "South America"; methList[11] = "International Postal,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 = "";
		}
	}
}
//-----------------------------------------------------------------------------------------------------

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;
        }
      }
    }
  }
}
