var freeEmailDomains;
function checkKey(field, nextField ,e)
{
   if(document.getElementById(field).value.length == 3)
   {
      document.getElementById(nextField).focus();
   }
}

function isNotInteger(elementId)
{
   var val = document.getElementById(elementId).value;
   val = Trim(val);
   if(isInteger(val))
   {
      return false;
   }
   else
   {
      return true;
   }
}

function addBillingAddress(elementId)
{
   var val = document.getElementById(elementId).checked;
   if(val)
   {
      document.getElementById("billingAddress1").value = document.getElementById("address1").value;
      document.getElementById("billingCity").value = document.getElementById("city").value;
      document.getElementById("billingState").value = document.getElementById("state").value;
      document.getElementById("billingZip").value = document.getElementById("zip").value;
   }
   else
   {
      document.getElementById("billingAddress1").value = "";
      document.getElementById("billingCity").value = "";
      document.getElementById("billingState").value = "";
      document.getElementById("billingZip").value = "";
   }
}

function onPageLoad()
{
   showSalesRepSection('applicationSource');
}

function cdsAccountOnPageLoad()
{
   showCDSAccountSection('paymentOption');
}

function showSalesRepSection(elementId)
{
   var selIndex = document.getElementById(elementId).selectedIndex;
   var selText = document.getElementById(elementId).options[selIndex].text;
   var selVal = document.getElementById(elementId).options[selIndex].value;
   if((selIndex != undefined || selIndex != "") && selVal == "1")
   {
      var salesPersonName = document.getElementById("salesPersonName").value;
      document.getElementById("promoCode").value = "";
      var addTag = "<table border=\"0\" width=\"90%\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"10%\"></td><td width=\"85%\"><b>Sales Rep's name:</b></td><td>";
      addTag += "<input type=\"text\" name=\"salesPersonNameDisplay\" id=\"salesPersonNameDisplay\" size=\"15\" value=\""+ salesPersonName +"\" onBlur=\"setSales_Section('salesPersonNameDisplay')\">";
      addTag += "</td></tr><tr><td>&nbsp;</td><td colspan=\"2\" align=\"right\">&nbsp;(example: Last Name, First Name).</td></tr></table>";
      document.getElementById("promoCode_sales_Section").innerHTML = addTag;
      document.getElementById("promoCode_sales_Section").style.visibility = "visible";
   }

   else if((selIndex != undefined || selIndex != "") && selVal == "2")
   {
      var promoCode = document.getElementById("promoCode").value;
      document.getElementById("salesPersonName").value = "";
      var addTag = "<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"5%\"></td><td width=\"35%\"><b>Promo Code:</b></td><td>";
      addTag += "<input type=\"text\" name=\"promoCodeDisplay\" id=\"promoCodeDisplay\" size=\"10\" value=\""+ promoCode +"\" onBlur=\"setPromoCode_Section('promoCodeDisplay')\">";
      addTag += "</td></tr></table>";
      document.getElementById("promoCode_sales_Section").innerHTML = addTag;
      document.getElementById("promoCode_sales_Section").style.visibility = "visible";
   }
   else if((selIndex != undefined || selIndex != "") && selVal == "3")
      {
         var addTag = "<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><td width=\"15%\">&nbsp;</td><td width=\"25%\"><b>Others:</b></td><td>";
         addTag += "<input type=\"text\" name=\"othersDisplay\" id=\"othersDisplay\" size=\"10\" maxlength=\"25\" onBlur=\"setOthers_Section('othersDisplay')\">" ;
         addTag += "</td></table>";
         document.getElementById("promoCode_sales_Section").innerHTML = addTag;
         document.getElementById("promoCode_sales_Section").style.visibility = "visible";
      }

   else
   {
      document.getElementById("promoCode_sales_Section").innerHTML = "";
      document.getElementById('promoCode_sales_Section').style.visibility = "hidden";
   }
}

function setSales_Section(elementId)
{
   document.getElementById("salesPersonName").value = document.getElementById(elementId).value;
}

function setPromoCode_Section(elementId)
{
   document.getElementById("promoCode").value = document.getElementById(elementId).value;
}

function setOthers_Section(elementId)
{
   document.getElementById("others").value = document.getElementById(elementId).value;
}

function showCDSAccountSection(elementId)
{
   showcdshelpText("paymentOption");
   var selIndex = document.getElementById(elementId).selectedIndex;
   var selText = document.getElementById(elementId).options[selIndex].text;
   var selVal = document.getElementById(elementId).options[selIndex].value;
   if((selIndex != undefined || selIndex != "") && selVal == "CDS")
   {
      var accountValue = document.getElementById('cdsAccount').value;
      var accountPrefixValue = document.getElementById('cdsAccountPrefix').value;
      var addTag = "<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"20%\"><b>Existing Account #:</b></td><td>";

      addTag += "<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"10%\">";
      addTag += "<select name=\"cdsAccountPref\" id=\"cdsAccountPref\" onChange=\"checkCdsAccountPrefix('cdsAccountPref')\">";
      addTag += "<option value=\"\">---</option>";
      addTag += "<option value=\"RR\" ";
         if(accountPrefixValue == "RR"){addTag += "SELECTED";}
      addTag += ">RR</option>";
      addTag += "<option value=\"RK\" ";
         if(accountPrefixValue == "RK"){addTag += "SELECTED";}
      addTag += ">RK</option>";
      addTag += "<option value=\"RP\" ";
         if(accountPrefixValue == "RP"){addTag += "SELECTED";}
      addTag += ">RP</option>";
      addTag += "</select></td><td>";
      addTag += "<input type=\"text\" name=\"cdsAccountInput\" id=\"cdsAccountInput\" value=\""+ accountValue +"\" size=\"10\" maxlength=\"6\" onBlur=\"checkCdsAccount('cdsAccountInput')\">";
      addTag += "</td></tr></table>";

      addTag += "</td></tr>";
      addTag += "<tr bgcolor=\"#eeeeee\"><td height=\"10\"><img border=\"0\" src=\"/list/images/1minibox.gif\" width=\"1\" height=\"1\"></td></tr></table>";

      document.getElementById("cdsaccountSection").innerHTML = addTag;
      document.getElementById("cdsaccountSection").style.visibility = "visible";
   }
   else
   {
      document.getElementById('cdsAccount').value = "";
      document.getElementById('cdsAccountPrefix').value = "";
      document.getElementById("cdsaccountSection").innerHTML = "";
      document.getElementById('cdsaccountSection').style.visibility = "hidden";
   }

}

function showCDSAlertText()
{
   var alertString = "Choosing this option will allow you to be billed later, however,\n each list purchase will be submitted to our Custom Data Solutions (CDS) department for fulfillment.\n" +
                           "Once your list is ready for pick-up, you will be notified via email.  You should also select this option if you anticipate any single purchase to be over $2,500.";
   alert(alertString);
}

function showCDSSectionVisibility(elementId)
{
   var selIndex = document.getElementById(elementId).selectedIndex;
   var selText = document.getElementById(elementId).options[selIndex].text;
   var selVal = document.getElementById(elementId).options[selIndex].value;
   if((selIndex != undefined || selIndex != "") && selVal == "CDS")
   {
      var accountValue = document.getElementById('cdsAccount').value;
      var accountPrefixValue = document.getElementById('cdsAccountPrefix').value;
      var addTag = "<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">";
      addTag += "<td width=\"30%\"><font class=\"bodyTextBld\">Existing Account #:</font></td><td>";

      addTag += "<select name=\"cdsAccountPref\" id=\"cdsAccountPref\" onChange=\"checkCdsAccountPrefix('cdsAccountPref')\">";
      addTag += "<option value=\"\">---</option>";
      addTag += "<option value=\"RR\" ";
         if(accountPrefixValue == "RR"){addTag += "SELECTED";}
      addTag += ">RR</option>";
      addTag += "<option value=\"RK\" ";
         if(accountPrefixValue == "RK"){addTag += "SELECTED";}
      addTag += ">RK</option>";
      addTag += "<option value=\"RP\" ";
         if(accountPrefixValue == "RP"){addTag += "SELECTED";}
      addTag += ">RP</option>";
      addTag += "</select>";

      addTag += "&nbsp;<input type=\"text\" name=\"cdsAccountInput\" id=\"cdsAccountInput\" value=\""+ accountValue +"\" size=\"10\" maxlength=\"6\" onBlur=\"checkCdsAccount('cdsAccountInput')\">";
      addTag += "</td></table>";

      document.getElementById("cdsaccountSection").innerHTML = addTag;
      document.getElementById("cdsaccountSection").style.visibility = "visible";
   }
   else
   {
      document.getElementById("cdsaccountSection").innerHTML = "";
      document.getElementById('cdsaccountSection').style.visibility = "hidden";
   }

}

function showcdshelpText(elementId)
{
   var selIndex = document.getElementById(elementId).selectedIndex;
   var selText = document.getElementById(elementId).options[selIndex].text;
   var selVal = document.getElementById(elementId).options[selIndex].value;
   if((selIndex != undefined || selIndex != "") && selVal == "CDS")
   {
      var addTag = "<table border=\"0\">";

      addTag += "<tr><td>&nbsp;</td><td>To qualify for Offline fulfillment and billing, you must have an existing account number. If you do not have one, don't remember what it is, or want more information about this option, please contact one of our account executives toll-free at 866.774.3282. </td></tr>";
      addTag += "<tr><td>&nbsp;</td><td>If you know your account number, please proceed with the following steps.</td></tr>";
      addTag += "<tr bgcolor=\"#eeeeee\">";
      addTag += "<td height=\"10\"><img border=\"0\" src=\"/list/images/1minibox.gif\" width=\"1\" height=\"1\"></td></tr>";

      addTag += "<tr><td>&nbsp;</td><td>Please enter your 8-character alpha numeric account number that begins with the prefix \"RR\",\"RK\" or \"RP\".</td></tr>";
      addTag += "<tr><td>&nbsp;</td><td>Please select the prefix from the drop down box,then input the remainder of your unique account number in the text box.</td></tr>";
      addTag += "<tr bgcolor=\"#eeeeee\"><td height=\"5\"><img border=\"0\" src=\"/list/images/1minibox.gif\" width=\"1\" height=\"1\"></td></tr>";
      addTag += "</table>";

      document.getElementById("cdshelpText").innerHTML = addTag;
      document.getElementById("cdshelpText").style.visibility = "visible";
   }
   else
   {
      document.getElementById("cdshelpText").innerHTML = "";
      document.getElementById("cdshelpText").style.visibility = "hidden";
   }
}

function checkCdsAccount(elementId)
{
   document.getElementById('cdsAccount').value =  document.getElementById(elementId).value;
}

function checkCdsAccountPrefix(elementId)
{
   var selIndex = document.getElementById(elementId).selectedIndex;
   var selText = document.getElementById(elementId).options[selIndex].text;
   var selVal = document.getElementById(elementId).options[selIndex].value;
   document.getElementById('cdsAccountPrefix').value =  document.getElementById(elementId).options[selIndex].value;
}

/*function validateEmailDomains(frm)
{
    var domainStatus = false;
    var emailId = document.getElementById("email").value
    var domains = freeEmailDomains.substring(1,freeEmailDomains.length-1)
    domains = domains.split(",");
    for(var i=0; i<domains.length;i++)
    {
         if((emailId.toLowerCase().trim()).match(domains[i].toLowerCase().trim()))
         {
             domainStatus = true;
         }
    }
   // alert(domainStatus)
    if(domainStatus)
    {
       var dom = confirm("Thank you for selecting ListSource for your direct marketing needs. Unfortunately we have been experiencing a high level of fraudulent payment transactions associated with “free” email accounts (hotmail, yahoo, gmail, etc).   As a result, we require users with free email accounts to use PayPal for list purchases.   At time of purchase you will enter in your PayPal account information.  If you would prefer to purchase a list using a credit card, please re-register with ListSource using another type of email address (provided by either an internet service provider, a company, academic institution, organization, government, etc.)  Please contact our sales department at 866.774.3282 if you require additional assistance.  We apologize for this inconvenience.");
        if(dom)
        {
            document.getElementById("freeEmailDomainStatus").value = "Y";
            onClickSubmit(frm);
            //frm.submit();
            return true;
        }
        else
        {
            return false;
        }
    }
     else
    {
       onClickSubmit(frm);
       //frm.submit();
    }
    return true;
}*/

function validateEmailDomains(frm)
{
   var dataUsageStatus = true;
   var freeTrialStatus;
   var domainStatus = false;
    
   for (var i=0; i < document.userRegistration.userTypeId.length; i++)
   {
      if (document.userRegistration.userTypeId[i].checked)
      {
         if(document.userRegistration.userTypeId[i].value == "199912401")
         {
            dataUsageStatus = false;
         }
      }
   }


   if(!dataUsageStatus)
   {
      var alertString = "Online registration is not available for your selected type of data usage.\n\n" +
                        "Thank you for your request. A member of our Licensing Support Team will be in contact with you shortly to set up your account.\n" +
                        "If you have any questions, please call toll-free 866.360.9580 Mondays through Fridays,7.00am to 5.00pm Pacific Time." ;


      alert(alertString);
      frm.submit();
      //document.getElementById("freeTrialStatus").value = "N";
     // return true;
   }
   else
   {
       var emailId = document.getElementById("email").value
       if(isObjExist(freeEmailDomains))
       {
        var domains = freeEmailDomains.substring(1,freeEmailDomains.length-1)
        domains = domains.split(",");
        for(var i=0; i<domains.length;i++)
        {
         if((emailId.toLowerCase().trim()).match(domains[i].toLowerCase().trim()))
         {
             domainStatus = true;
         }
        }
       }

      if(linkPage == 'subscriptionRegistration')
      {
         var selection = confirm("  Do you wish to go for a free trial?   ");
         if (selection == true)
         {
            document.getElementById("freeTrialStatus").value = "Y";
         }
         else
         {
            document.getElementById("freeTrialStatus").value = "N";
            domainStatus = false;
         }
      }
      else
      {
         document.getElementById("freeTrialStatus").value = "Y";
      }


     if(domainStatus && linkPage != "cdsRegistration")
    {
       var dom = confirm("Thank you for selecting ListSource for your direct marketing needs. Unfortunately we have been experiencing a high level of fraudulent payment transactions associated with \"free\" email accounts (hotmail, yahoo, gmail, etc).   As a result, we require users with free email accounts to use PayPal for list purchases.   At time of purchase you will enter in your PayPal account information. \n\n" +
                         "If you would prefer to purchase a list using a credit card, please re-register with ListSource using another type of email address (provided by either an internet service provider, a company, academic institution, organization, government, etc.)\n\n" +
                         "Please contact our sales department at 866.774.3282 if you require additional assistance.  We apologize for this inconvenience.");
        if(dom)
        {
            document.getElementById("freeEmailDomainStatus").value = "Y";
            frm.submit();
            return true;
        }
        else
        {
            return false;
        }
    }
    else
     {
         frm.submit();
         return true;
     }
   }
}

function isObjExist(elementID)
{
   return elementID != undefined && elementID != null;
}

String.prototype.trim = function() {return this.replace(/^\s+|\s+$/, ''); }

function addDataUsageText(elementId,selText)
{
   for (var i=0; i < document.userRegistration.userTypeId.length; i++)
   {
      if (document.userRegistration.userTypeId[i].checked)
      {
         document.getElementById("dataUsage").value = selText;
      }
   }
}

function isInteger(elementId)
{
   var val = document.getElementById(elementId).value;
   val = Trim(val);
   if (isBlank(val))
   {
      return false;
   }
   for (var i = 0; i < val.length; i++)
   {
      if (!isDigit(val.charAt(i)))
      {
         return false;
      }
   }
   return true;
}

function isBlank(val)
{
   val = Trim(val);
   if (val == null)
   {
      return true;
   }
   for (var i = 0; i < val.length; i++)
   {
      if ((val.charAt(i) != ' ') && (val.charAt(i) != "\t") && (val.charAt(i) != "\n") && (val.charAt(i) != "\r"))
      {
         return false;
      }
   }
   return true;
}

function isDigit(num)
{
   num = Trim(num);
   if (num.length > 1)
   {
      return false;
   }
   var string = "1234567890";
   if (string.indexOf(num) != -1)
   {
      return true;
   }
   return false;
}

function Trim(s)
{
   while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
   {
      s = s.substring(1,s.length);
   }

   while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
   {
      s = s.substring(0,s.length-1);
   }
   return s;
}


function validateEmail()
{
   var addr = document.getElementById("email").value;
   addr = Trim(addr);
   alert("emailVal"+emailVal);
   if (addr == '')
   {
      alert('email address is mandatory');
      return false;
   }

   var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
   for (i=0; i<invalidChars.length; i++)
   {
      if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
         alert('email address contains invalid characters');
         return false;
      }
   }
   for (i=0; i<addr.length; i++)
   {
      if (addr.charCodeAt(i)>127)
      {
         alert("email address contains non ascii characters.");
         return false;
      }
   }

   var atPos = addr.indexOf('@',0);
   if (atPos == -1)
   {
      alert('email address must contain an @');
      return false;
   }
   if (atPos == 0)
   {
      alert('email address must not start with @');
      return false;
   }
   if (addr.indexOf('@', atPos + 1) > - 1)
   {
      alert('email address must contain only one @');
      return false;
   }
   if (addr.indexOf('.', atPos) == -1)
   {
      alert('email address must contain a period in the domain name');
      return false;
   }
   if (addr.indexOf('@.',0) != -1)
   {
      alert('period must not immediately follow @ in email address');
      return false;
   }
   if (addr.indexOf('.@',0) != -1)
   {
      alert('period must not immediately precede @ in email address');
      return false;
   }
   if (addr.indexOf('..',0) != -1)
   {
      alert('two periods must not be adjacent in email address');
      return false;
   }

   var suffix = addr.substring(addr.lastIndexOf('.')+1);
   if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu'
         && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz'
         && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info'
         && suffix != 'pro' && suffix != 'museum')
   {
      alert('invalid primary domain in email address');
      return false;
   }
   return true;
}
