/****************************************
Symantec - US & Canada
Shipping Validation Javascript
Entry-point(s): 
 .Standard Site - SO 289 TT 7
 .Call Center Site - SP 10249 PN 1
Ship Master ID: 10.95
***************************************/

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 onSelectLocation()
{
  check_shipping();
  document.CALFORM.submit();
}

function onSelectMethod()
{
  check_shipping();
  document.CALFORM.submit();
}

function check_shipping()
{
  var passThrough = 0;
  var pname = "";

  // Begining of Shipping Table Info
  var shipTableCheck = 0;
  for(j=0; j<document.CALFORM.elements.length; j++){
    if (document.CALFORM.elements[j].name=="p_name" && document.CALFORM.elements[j].value=="S2")
    for (i=0; i<document.CALFORM.elements[j+1].options.length; i++){
      if (document.CALFORM.elements[j+1].options[i].text == "Air Mail" || document.CALFORM.elements[j+1].options[i].text == "USPS"){
        var shipTableCheck = 1;
        break;
      }
    }
  }


  var locList = new Array();
  var methList = new Array();

  if (shipTableCheck == 1) {
    //Array for CD 2 Go Shipping Table
    locList[0] = "Africa"; methList[0] = "Air Mail";
    locList[1] = "Australia"; methList[1] = "Air Mail";
    locList[2] = "Africa"; methList[2] = "Air Mail";
    locList[3] = "Canada"; methList[3] = "Air Mail";
    locList[4] = "Caribbean / West Indies"; methList[4] = "Air Mail";
    locList[5] = "Central America"; methList[5] = "Air Mail";
    locList[6] = "Europe"; methList[6] = "Air Mail";
    locList[7] = "Japan and Asia"; methList[7] = "Air Mail";
    locList[8] = "Mexico"; methList[8] = "Air Mail";
    locList[9] = "North America"; methList[9] = "USPS";
    locList[10] = "Puerto Rico"; methList[10] = "Air Mail";
    locList[11] = "South America"; methList[11] = "Air Mail";
  }
  else {
    //Array for Symantec - US & Canada Shipping Table
    locList[0] = "Africa"; methList[0] = "UPS International,International Postal";
    locList[1] = "Alaska & Hawaii"; methList[1] = "Economy Postal Mail,UPS Second Day Air,UPS Overnight";
    locList[2] = "Australia & New Zealand"; methList[2] = "UPS International,International Postal";
    locList[3] = "Canada"; methList[3] = "UPS Canadian Economy,UPS International,International Postal";
    locList[4] = "Caribbean"; methList[4] = "UPS International,International Postal,DHL";
    locList[5] = "Europe"; methList[5] = "UPS International,International Postal";
    locList[6] = "Japan & Asia"; methList[6] = "UPS International,International Postal";
    locList[7] = "Lower 48 States"; methList[7] = "UPS Ground,UPS Second Day Air,UPS Overnight,Economy Postal Mail";
    locList[8] = "Mexico"; methList[8] = "UPS International,International Postal,DHL";
    locList[9] = "Middle East"; methList[9] = "UPS International,International Postal";
    locList[10] = "Puerto Rico"; methList[10] = "UPS Second Day Air,International Postal, UPS Overnight";
    locList[11] = "South America"; methList[11] = "UPS International,International Postal,DHL";
  }

  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 = "";
    }
  }
}
