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("billingAddress2").value = document.getElementById("address2").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("billingAddress2").value = "";
      document.getElementById("billingCity").value = "";
      document.getElementById("billingState").value = "";
      document.getElementById("billingZip").value = "";
   }
}

function onPageLoad()
{
   showSalesRepSection('applicationSource');
   showPasswordsection();
}



function showPasswordsection()
{
			 if(document.userRegistration.userTypeId == undefined)
			{
			 addPasswordElements();
			}
			else if(document.userRegistration.userTypeId[0].checked)
			{
			 addPasswordElements();
			  document.getElementById('termsAcceptance').disabled = false;
			}else
			{
			  document.getElementById('reqResaleConvert').disabled = true;
		      document.getElementById('termsAcceptance').disabled = true;
			  document.getElementById("passworderror").innerHTML = "";
			  document.getElementById("passworderror").style.visibility = "hidden";
			  document.getElementById("passworderror1").innerHTML = "";
			  document.getElementById("passworderror1").style.visibility = "hidden";
		      document.getElementById("passwordSection").innerHTML = "";
              document.getElementById("passwordSection").style.visibility = "hidden";
		      document.getElementById("reTypePasswordSection").innerHTML = "";
              document.getElementById("reTypePasswordSection").style.visibility = "hidden";
			  document.getElementById("hrLineSection").innerHTML = "";
              document.getElementById("hrLineSection").style.visibility = "hidden";
		   if(document.getElementById('termsAcceptanceError')!=null)
			{
			  document.getElementById("termsAcceptanceError").innerHTML = "";
		      document.getElementById("termsAcceptanceError").style.visibility = "hidden";
			}
			}
}
function addPasswordElements()
{


			  var addTag = "";
			  addTag += "<table><tr><td width=\"39%\" valign=\"top\"><b>Choose<br/>Password:<font color=red>*</font></b></td>";
			  addTag += "<td style=\"padding-left:3px;\" ><input type=\"password\" name=\"password\" id=\"passwordReg\" size=\"22\" >";
			  addTag += "</td></tr></table>";
			  document.getElementById("passwordSection").innerHTML = addTag;
			  document.getElementById("passwordSection").style.visibility = "visible";
			  var addTag1 = "";
			  addTag1 += "<table><tr><td width=\"39%\"><b>Confirm <br/>Password:<font color=red>*</font></b></td>";
			  addTag1 += "<td style=\"padding-left:3px;\"> <input type=\"password\" name=\"reTypepassword\" id=\"reTypepassword\" size=\"22\" > ";
			  addTag1 += "</td> </tr> </table>";
			  document.getElementById("reTypePasswordSection").innerHTML = addTag1;
			  document.getElementById("reTypePasswordSection").style.visibility = "visible";
			  var hrlineTags = "";
			  hrlineTags += "<hr style=\"height:1px;color:#dddddd;padding-top:2px;border-left-width: 0px;\">";
			  document.getElementById("hrLineSection").innerHTML = hrlineTags;
			  document.getElementById("hrLineSection").style.visibility = "visible";
}

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=\"4%\"></td><td width=\"36%\"><b>&nbsp;&nbsp;&nbsp;&nbsp;Sales Rep's <br/>&nbsp;&nbsp;&nbsp;&nbsp;name:<font color=red>*</font></b></td><td width=\"40%\">";
      addTag += "<input type=\"text\" name=\"salesPersonNameDisplay\" id=\"salesPersonNameDisplay\" size=\"20\" maxlength=\"128\" value=\""+ salesPersonName +"\" onBlur=\"setSales_Section('salesPersonNameDisplay')\">";
      addTag += "</td></tr><tr><td>&nbsp;</td><td></td><td colspan=\"2\" align=\"right\">(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=\"6%\"></td><td width=\"30%\"><b>Promo Code:</b></td><td>";
      addTag += "<input type=\"text\" name=\"promoCodeDisplay\" id=\"promoCodeDisplay\" size=\"20\" maxlength=\"128\" 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=\"6%\">&nbsp;</td><td width=\"30%\"><b>Others:</b></td><td>";
         addTag += "<input type=\"text\" name=\"othersDisplay\" id=\"othersDisplay\" size=\"20\" maxlength=\"128\" 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 = "Thank you for your request.\n\n" +
                           "A member of our licensing support team will be in contact with you shortly to set up your account. \n" +
                           "Please note that online registration is not available for Broker/Reseller type of data usage. \n\n"+
                           "Please contact us at 866.360.9580 with any questions. Our customer support team is available Monday through Friday between 7:00 am to 5:00 pm 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++)
   //      {
  //  		 var checkEmailAddress = emailId.toLowerCase().trim();
	// 	 var checkEmailDomain = domains[i].toLowerCase().trim();
   //       if(checkEmailAddress.endsWith(checkEmailDomain))
   //       {
   //           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(linkPage != "cdsRegistration")
    {
		var dom = confirm("Thank you for selecting ListSource for your direct marketing needs. \n\n" +
                         "For a safe and secure purchasing experience, your list purchases can be completed using PayPal"+unescape("%Ae")+". Once you've finished building your list, you will be prompted to enter your PayPal account information to complete your purchase. \n\n" +
                         "If you prefer to use a credit card, please contact our Sales department for further assistance. \n\n" +
                         "Sales can be reached at 866.774.3282. \n\n" +
                         "We appreciate your business !");
        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.endsWith = function(str){return (this.match(str+"$")==str);};

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)
      {
		if(i==0)
		{
		   document.getElementById('reqResaleConvert').disabled = false;
			var pwdValue="";
			var pwdValue1="";
		if(document.getElementById('passwordReg')!=null)
		{
			pwdValue=document.userRegistration.password.value;
			pwdValue1=document.userRegistration.reTypepassword.value;
	    }
		    showPasswordsection();
		if(document.getElementById('passwordReg')!=null)
		{
		    document.userRegistration.password.value=pwdValue;
			document.userRegistration.reTypepassword.value=pwdValue1;
			}
		}
		else
		{
		   document.getElementById('reqResaleConvert').disabled = true;
		   document.getElementById('termsAcceptance').disabled = true;
		   document.getElementById("passwordSection").innerHTML = "";
           document.getElementById("passwordSection").style.visibility = "hidden";
		   document.getElementById("reTypePasswordSection").innerHTML = "";
           document.getElementById("reTypePasswordSection").style.visibility = "hidden";
		   document.getElementById("hrLineSection").innerHTML = "";
           document.getElementById("hrLineSection").style.visibility = "hidden";
		   document.getElementById("passworderror").innerHTML = "";
		   document.getElementById("passworderror").style.visibility = "hidden";
		   document.getElementById("passworderror1").innerHTML = "";
		   document.getElementById("passworderror1").style.visibility = "hidden";
		   if(document.getElementById('termsAcceptanceError')!=null)
			{
		   document.getElementById("termsAcceptanceError").innerHTML = "";
		   document.getElementById("termsAcceptanceError").style.visibility = "hidden";
			}
		}
         document.getElementById("dataUsage").value = selText;
      }
   }
}


function setReqResaleConvertValue()
{
	document.getElementById('reqResaleConvert').value ="";
	if(document.getElementById('reqResaleConvert').checked == true)
	{
	document.getElementById('reqResaleConvert').value = "checked";
	}
}
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;
}

