/* replace accented char with orig */
function replaceAccentedChar(txtValue){
    var charBagA = "193,225,192,224,195,227";
    var charBagE = "201,233,200,232";
    var charBagI = "205,237,204,236";
    var charBagN = "209,241";
    var charBagO = "211,243,210,242,213,245";
    var charBagU = "218,250,217,249";
    var charBagY = "221,253";

    var y;
    var charBagName = "AEINOUY";
    var arrCharBagName = charBagName.split("");
    var replaceValue = txtValue;

    for (i=0;i<txtValue.length;i++) {
        ch = txtValue.substring(i,i+1);
        for (k=0;k<arrCharBagName.length;k++) {
            var tempBagArray = eval("charBag" + arrCharBagName[k]).split(",");
            for (j=0;j<tempBagArray.length;j++) {
                if (tempBagArray[j] == ch.charCodeAt(0)) {
                    charCase = eval("charBag" + arrCharBagName[k]).split(",");
                    for (l=0;l<charCase.length;l++) {
                        if (charCase[l] == ch.charCodeAt(0)) {
                            y = l;
                        }
                    }
                    if (y == "1" || y=="3" || y=="5") {
                        replaceValue = replaceValue.replace(ch,arrCharBagName[k].toLowerCase());
                    } else {
                        replaceValue = replaceValue.replace(ch,arrCharBagName[k]);
                    }
                }
            }
        }
    }
    return replaceValue;
}
/* replace bad ascii characters with a blank */
function replaceBadAscii(tempValue){
    /* The first number is reserved only for evaluating greater than condition */
    var badascii = [127,9,19];
    var newValue = tempValue;

    var dynstr = '';
    for (i=0;i<badascii.length;i++) {
           if ( i!=0)   {
            dynstr = dynstr + ' || ' + '(ch.charCodeAt(0) == badascii['+i+'])';
           }else{
            dynstr = '(ch.charCodeAt(0) > badascii['+i+'])';
           }
      }

    for (i=0;i<tempValue.length;i++) {
        ch = tempValue.substring(i,i+1);
        if(eval(dynstr)){
              newValue = newValue.replace(ch, "");
        }
    }
    return newValue;
}
/* Remove lead spaces */
function stringTrim(firstValue){
    var ch = "";
    var spaces="";
    var count=0;
    var chvalue="";
    var finalValue="";

    for(var i=0; i<firstValue.length;i++) {
        ch= firstValue.substring(i,i+1);
        if (ch == " ") {
            spaces ++;
        }else {
            count++;
            if(count == 1) {
                realcount = spaces;
                finalValue = firstValue.substring(realcount,firstValue.length+1);
            }
        }
    }
    return finalValue;
}
/* alert messages */
function alrtFunc(frmName,fieldName,alrtMsg) {
    alert(alrtMsg);
    eval("document."+frmName+"."+fieldName).focus();
    eval("document."+frmName+"."+fieldName).select();
    return false;
}
function takeSpaces(string1){
    var string2 = "",char1="";
    for(var i=0;i<string1.length;i++) {
        char1  = string1.substring(i,i+1);
        if(char1 != " "){string2 = string2 + char1;}
    }
    return string2;
}
function ignoreSpaces(string) {
    var stringOut = "";
    string = '' + string;
    splitstring = string.split(" ");
    for(i = 0; i < splitstring.length; i++) {
        stringOut += splitstring[i];
    }
    return stringOut;
}
function noThanks(){
document.myform.page_position.value = parseInt(document.myform.page_position.value)-1;
document.myform.Cust_NameAddr_Grp.value = 'N';
document.myform.Cust_Email_Grp.value = 'N';
document.myform.Cust_Selection_Grp.value = 'N';
document.myform.Cust_CC_Grp.value = 'N';
submitRegistration();
}
function writeHugWebBug(sid,afk,p1,p2,p3,t3,currentTimeMilli,ip,p4,p5,p6,t4,t5,t6) {

    t1 = document.myform.fromURL.value;
    t2 = document.myform.toURL.value;
    pageName = t1;
    op = 'null';

/*    if (document.myform.secured_protocol.value == 'Y') {
        protocol_base = 'https://';
    } else {
        protocol_base = 'http://';
    }
*/
    //Record the browser name.
    var browserInfo = navigator.appName + "--";

    //Record the exact version number if IE or Netscape only.
    if (browserInfo.indexOf("Microsoft") != -1) {
        //IE
        browserInfo += navigator.userAgent.match(/MSIE \d+.\d+/);
    } else if (browserInfo.indexOf("Netscape") != -1) {
        //Netscape
        browserInfo += navigator.userAgent.match(/Netscape.*/);
    }

    //Record the platform name.
    browserInfo += "--" + navigator.platform;

    //Remove all spaces because their ASCII value, %20, gets printed.
    browserInfo = browserInfo.replace(/ /g,"");

//    var hugBugUrl = "<img src=" + location.protocol + "//" + webBugURL + "/" + currentTimeMilli + "/" + webBugPage +"?||1||||" + afk_html + "||" + pageName_html + "||null||" + browserInfo_html + "||||30||||||||||||||||||||||||||||" + " width='1' height='1'>";
    var hugBugUrl = "<img src=" + location.protocol + "//" + webBugURL + "/" + currentTimeMilli + "/"+ webBugPage +"?||1||" + sid + "||" + afk + "||" + pageName + "||null||" + browserInfo + "||||30||" + p1 + "||" + p2 + "||" + p3 + "||" + t1 + "||" + t2 + "||" + t3 + "||" + op + "||" + ip + "||" + p4 + "||" + p5 + "||" + p6 + "||" + t4 + "||" + t5 + "||" + t6 + " width='1' height='1'>";
    document.write(hugBugUrl);
}
function getParam(name,params){
  var paramName = name + "=";
  var offset = params.indexOf(paramName);

  if (offset == -1)
    return "";

  offset += paramName.length;  // adjust for equal sign

  var length = params.indexOf("&", offset);

  if (length == -1)
    length = params.length;

  return params.substr(offset, length - offset);
}
function getOrd(ordinal, params){
  var offset = 0;
  var index = 0;
  var value = "";
  var pos = params.indexOf(",", offset);

  while (pos > -1)
    {
      if (++index == ordinal)
        {
          value = params.substr(offset, pos - offset);
          break;
        }

      offset = pos + 1;
      pos = params.indexOf(",", offset);
    }

  if ((value == "") && (++index == ordinal))
    value = params.substr(offset, params.length - offset);

  return value;
}
function getWindowSize() {
    if (top == self){
        if (navigator.userAgent.indexOf("MSIE") != -1) {
            var origInnerW = document.body.offsetWidth;
            var origInnerH = document.body.offsetHeight;

            window.resizeTo(800,800);
            document.myform.windowWidth.value =
            800 - (document.body.offsetWidth - origInnerW);
            document.myform.windowHeight.value =
            800 - (document.body.offsetHeight - origInnerH);
            window.resizeTo(document.myform.windowWidth.value,document.myform.windowHeight.value);
        } else {
            document.myform.windowWidth.value=window.innerWidth;
            document.myform.windowHeight.value=window.innerHeight;
        }
    }
    return true;
}
function getEncodeURLParams(){
  var is_input = location.search.indexOf('?');
  var url_params = "";

  if (is_input != -1)
    url_params = location.search.substring(is_input+1, location.search.length);
  return url_params;
}
function submitRegistration() {
  if (document.myform.Cust_NameAddr_Grp.value == 'Y'){
    if (!isValidChar("myform","first_name","Please include your first name.")) return;
    if (!isValidChar("myform","last_name","Please include your last name.")) return;
    if (!isValidAdd1("myform","street_address1","Please include your address.")) return;
    if (!isValidAdd2("myform","street_address2","Please include a valid address.")) return;
    if (!isValidChar("myform","city","Please include your city.")) return;
    if (!isValidST("myform","state","Please include your state.")) return;
    if (!isValidZip("myform","enzp","Please include your valid 5 digit zip code.")) return;
  }

  if(document.myform.Cust_Email_Grp.value == 'Y'){
    if (!isValidEmail("myform","efrom","Please include your email address")) return;
  }else{
    var parameters  = unescape(getParam("params", location.search));
    sid = getOrd(2, parameters);
    document.myform.efrom.value = sid + "@pxp.com"
  }

  if(document.myform.Cust_CC_Grp.value == 'Y'){
    if (!isValidCC("myform", "credit_card_number","credit_card_type"))return;
    if (!isValidCCDate("myform", "credit_card_expiration_month","credit_card_expiration_year"))return;
  }

  var url_params = getEncodeURLParams();
  url_params =  clear_userfields_from_url(url_params);
  for (var i = 0; i<document.myform.elements.length; i++)
      url_params += "&"+document.myform.elements[i].name+"="+document.myform.elements[i].value;
  var action_url = '/ixp/txn_gateway.tmpl?' + url_params;
  getWindowSize();
  document.myform.action = action_url;
  document.myform.submit();
}
function xp_popup(URL){
  var popup_url = URL+"?"+getEncodeURLParams();
  win2=window.open(popup_url, 'win2','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=500,height=300');
}
function popup(popup_url){
  popup1=window.open(popup_url, 'popup1','toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=600,height=400');
}
function clear_userfields_from_url(url_params){
    if(unescape(getParam("zipCodeError", location.search)) == "true"
        || unescape(getParam("emailError", location.search)) == "true"){
      //taking out user info fields in order to get the latest user info field values when user submit everytime
      //there is zip code error or email error
      url_params = url_params.replace("&first_name="+getParam("first_name", location.search),"");
      url_params = url_params.replace("&last_name="+getParam("last_name", location.search),"");
      url_params = url_params.replace("&street_address1="+getParam("street_address1", location.search),"");
      url_params = url_params.replace("&street_address2="+getParam("street_address2", location.search),"");
      url_params = url_params.replace("&city="+getParam("city", location.search),"");
      url_params = url_params.replace("&state="+getParam("state", location.search),"");
      url_params = url_params.replace("&enzp="+getParam("enzp", location.search),"");
      url_params = url_params.replace("&efrom="+getParam("efrom", location.search),"");
      url_params = url_params.replace("&credit_card_type="+getParam("credit_card_type", location.search),"");
      url_params = url_params.replace("&credit_card_expiration_month="+getParam("credit_card_expiration_month", location.search),"");
      url_params = url_params.replace("&credit_card_expiration_year="+getParam("credit_card_expiration_year", location.search),"");
      return url_params;
   }
   return url_params;
}

var parameters = unescape(getParam("params", location.search));
afk = getOrd(1, parameters);
sid = getOrd(2, parameters);
bmts = getOrd(3, parameters);
xpip = getOrd(4, parameters);
p1 = getOrd(5, parameters);
p2 = getOrd(6, parameters);
p3 = getOrd(7, parameters);
t3 = getOrd(10, parameters);
ip = getOrd(12, parameters);
p4 = getOrd(13, parameters);
p5 = getOrd(14, parameters);
p6 = getOrd(15, parameters);
t4 = getOrd(16, parameters);
t5 = getOrd(17, parameters);
t6 = getOrd(18, parameters);
var my_date=new Date();
currentTimeMilli = my_date.getTime();