/****************************************
DR - Physical On Demand (2006)
Shipping Validation Javascript
Entry-point: SO 289 TT 7 - Shopping Cart
Ship Master ID: 11.28
***************************************/

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();
  // Netfulfilment
  locList[0] = "Africa"; methList[0] = "Royal Mail International";
  locList[1] = "Australia and New Zealand"; methList[1] = "Royal Mail International";
  locList[2] = "Canada"; methList[2] = "UPS Canadian Economy,Canadian Postal";
  locList[3] = "Caribbean"; methList[3] = "Royal Mail International";
  locList[4] = "Europe"; methList[4] = "Royal Mail International";
  locList[5] = "Japan and Asia"; methList[5] = "Royal Mail International";
  locList[6] = "Mexico"; methList[6] = "Royal Mail International";
  locList[7] = "Middle East"; methList[7] = "Royal Mail International";
  locList[8] = "Puerto Rico"; methList[8] = "Royal Mail International";
  locList[9] = "South America"; methList[9] = "Royal Mail International";
  // DSS
  locList[10] = "US Alaska and Hawaii"; methList[10] = "Economy Postal Mail,UPS 2nd Day,UPS Overnight";
  locList[11] = "US Lower 48 States"; methList[11] = "Economy Postal Mail,UPS Ground,UPS 2nd Day,UPS Overnight";
  // 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;
        }
      }
    }
  }
}
