var errfound=false;


function getCookie(NameOfCookie){
if (document.cookie.length > 0) { 
	begin = document.cookie.indexOf(NameOfCookie+"="); 
	if (begin != -1) { 
		begin += NameOfCookie.length+1; 
		end = document.cookie.indexOf(";", begin);
		if (end == -1) end = document.cookie.length;
		return unescape(document.cookie.substring(begin, end)); } 
}
return null; 
}

function FindCookie(){
var aff_num=0;
aff_num=getCookie('YouAff');
if (aff_num!=null) {
    document.info.affnum.value=aff_num; }
}

function SignUp() {
    FindCookie();
	document.info.action = "https://www.youdeparted.com/cgi-bin/you/register.cgi"; 
	document.info.method = "POST";
	document.info.submit();
}

function Demo() {
    FindCookie();
	document.info.action = "https://www.youdeparted.com/demo/"; 
	document.info.method = "POST";
	document.info.submit();
}

function Login() {
    FindCookie();
    document.info.action = "https://www.youdeparted.com/login/";
    document.info.method = "POST";
    document.info.submit();
}
	

function makeWin(url) {
	      agent = navigator.userAgent;
	      windowName = "Sitelet";
	      params  = "";
	      params += "toolbar=1,";
	      params += "location=0,";
	      params += "directories=0,";
	      params += "status=0,";
	      params += "menubar=1,";
	      params += "scrollbars=1,";
	      params += "resizable=1,";
	      params += "width=820,";
	      params += "height=600";
	      win = window.open(url, windowName , params);
	      if (agent.indexOf("Mozilla/2") != -1 && agent.indexOf("Win") == -1) {
	          win = window.open(url, windowName , params);
	      }
	      if (!win.opener) {

	          win.opener = window;
	      }
}

// Demo Account Login
function DemoPassinfo() {
errfound = false;
 
if (!ValidLength(document.info.fn_username.value,6))
      error(document.info.fn_username,"You didn't enter your username. It must be at least six characters.");
      
checkInput(document.info.fn_username.value,document.info.fn_username,'username');  
      
if (!ValidLength(document.info.fn_password.value,8))
      error(document.info.fn_password,"You didn't enter a password. It must be at least eight characters.");
      
checkInput(document.info.fn_password.value,document.info.fn_password,'password');
      
if (!ValidLength(document.info.fn_key.value,6))
      error(document.info.fn_key,"You did not enter the complete key. It must be at least six characters.");

checkInput(document.info.fn_key.value,document.info.fn_key,'Secret Key');
     
if (errfound==false)
	{
	document.getElementById('login_screen').style.visibility = "hidden";
	document.getElementById('login_screen').style.display = "";
	document.getElementById('login_logo').style.visibility = "visible";
 	setTimeout("document.getElementById('login_logo').innerHTML = document.getElementById('login_logo').innerHTML",200); 
	document.info.action = "https://www.youdeparted.com/cgi-bin/you/login.cgi";
	document.info.method = "POST";
	document.info.submit();
	}
}

// checks the input for valid chars
function checkInput(elem,base,emess) {
var checkChar=0;
var checkstr =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+@#_!^*";
for (var j=0; j<elem.length; j++) {
	for (var k=0; k<checkstr.length; k++) {
		if (elem.charAt(j) == checkstr.charAt(k)) {
		                  checkChar++;
		}
	}	
}

if (checkChar != elem.length)
	error(base,"Sorry, illegal " + emess + " characters. You can only enter numbers, letters and \+ \@ \# \_ \! \^ \*");
}

// Acount Holder Login
function passinfo() {
errfound = false;
 
if (!ValidLength(document.info.fn_username.value,6))
      error(document.info.fn_username,"You didn't enter your username. It must be at least six characters.");

checkInput(document.info.fn_username.value,document.info.fn_username,'username');
     
if (!ValidLength(document.info.fn_password.value,8))
      error(document.info.fn_password,"You didn't enter a password. It must be at least eight characters.");
      
checkInput(document.info.fn_password.value,document.info.fn_password,'password');
      
if (!ValidLength(document.info.fn_key.value,6))
      error(document.info.fn_key,"You did not enter the complete key. It must be at least six characters.");

checkInput(document.info.fn_key.value,document.info.fn_key,'Secret Key');

if (errfound==false)
	{
	document.getElementById('login_screen').style.visibility = "hidden";
	document.getElementById('login_logo').style.visibility = "visible";
	setTimeout("document.getElementById('login_logo').innerHTML = document.getElementById('login_logo').innerHTML",200);
 	document.info.action = "https://www.youdeparted.com/cgi-bin/you/login.cgi";
	document.info.method = "POST";
	document.info.submit();
	}
}

function alpha(e) {
var k;

document.all ? k = parseInt(e.keyCode) : k = parseInt(e.which);

if (k == 13)
   {
   passinfo();
   return false;
   }
 
else {
return ((k > 47 && k < 58) || (k > 64 && k < 91) || (k > 96 && k < 123) || k == 0 || k == 8 || k == 9 || k == 11 || k == 33 || k == 35 || k == 42 || k == 43 || k == 64 || k == 94 || k == 95  );
}
}

function alpha2(e) {
var k;
document.all ? k = parseInt(e.keyCode) : k = parseInt(e.which);

if (k == 13)
   {
   passinfo();
   return false;
   }
 
else {

return ((k > 47 && k < 58) || (k > 64 && k < 91) || (k > 96 && k < 123) || k == 0 || k == 8 || k == 9 || k == 11 ||  k == 32 || k == 33 || k == 35 || k == 42 || k == 43 || k == 64 || k == 94 || k == 95  );
}
}


// Recipient Login
function change () {

errfound = false;

if (!ValidLength(document.info.fn_bene_username.value,6))
      error(document.info.fn_bene_username,"You didn't enter your username. It must be at least six characters.");

checkInput(document.info.fn_bene_username.value,document.info.fn_bene_username,'username'); 

if (!ValidLength(document.info.fn_bene_code.value,1))
      error(document.info.fn_bene_code,"You didn't enter your password."); 

if (!ValidLength(document.info.fn_bene_code.value,8))
      error(document.info.fn_bene_code,"You didn't enter a password. It must be at least eight characters.");

checkInput(document.info.fn_bene_code.value,document.info.fn_bene_code,'password');
  
if (!ValidLength(document.info.fn_key.value,6))
      error(document.info.fn_key,"You did not enter the complete key. It must be at least six characters.");

checkInput(document.info.fn_key.value,document.info.fn_key,'Secret Key');
if (errfound==false) {
	document.getElementById('login_screen').style.visibility = "hidden";
      document.getElementById('login_logo').style.visibility = "visible";
      setTimeout("document.getElementById('login_logo').innerHTML = document.getElementById('login_logo').innerHTML",200);
	document.info.action = "https://www.youdeparted.com/cgi-bin/you/recipCenter.cgi";
	document.info.method = "POST";
	document.info.submit();
	}
}

function Ralpha(e) {
var k;
document.all ? k = parseInt(e.keyCode) : k = parseInt(e.which);
if (k == 13)
   {
   change();
   return false;
   }
 
else {
return ((k > 47 && k < 58) || (k > 64 && k < 91) || (k > 96 && k < 123) || k == 0 || k == 8 || k == 9 || k == 11 ||  k == 33 || k == 35 || k == 42 || k == 43 || k == 64 || k == 94 || k == 95  );
}
}

function Ralpha2(e) {
var k;
document.all ? k = parseInt(e.keyCode) : k = parseInt(e.which);
if (k == 13)
   {
   change();
   return false;
   }
 
else {
return ((k > 47 && k < 58) || (k > 64 && k < 91) || (k > 96 && k < 123) || k == 0 || k == 8 || k == 9 || k == 11 ||  k == 32 || k == 33 || k == 35 || k == 42 || k == 43 || k == 64 || k == 94 || k == 95  );
}
}

// Demo Recipient Login
function Demopassinfo() {
errfound = false;

if (!ValidLength(document.info.fn_bene_username.value,6))
      error(document.info.fn_bene_username,"You didn't enter your username. It must be at least six characters.");

checkInput(document.info.fn_bene_username.value,document.info.fn_bene_username,'username');
      
if (!ValidLength(document.info.fn_bene_code.value,8))
      error(document.info.fn_bene_code,"You didn't enter a password. It must be at least eight characters.");

checkInput(document.info.fn_bene_code.value,document.info.fn_bene_code,'password');
  
if (!ValidLength(document.info.fn_key.value,6))
      error(document.info.fn_key,"You did not enter the complete key. It must be at least six characters.");

checkInput(document.info.fn_key.value,document.info.fn_key,'Secret Key');
function Dalpha(e) {
var k;

document.all ? k = parseInt(e.keyCode) : k = parseInt(e.which);

if (k == 13)
   {
   Demopassinfo();
   return false;
   }
 
else {
return ((k > 47 && k < 58) || (k > 64 && k < 91) || (k > 96 && k < 123) || k == 0 || k == 8 || k == 9 || k == 11 ||  k == 33 || k == 35 || k == 42 || k == 43 || k == 64 || k == 94 || k == 95  );
}
}

function Dalpha2(e) {
var k;
document.all ? k = parseInt(e.keyCode) : k = parseInt(e.which);

if (k == 13)
   {
   Demopassinfo();
   return false;
   }
 
else {

return ((k > 47 && k < 58) || (k > 64 && k < 91) || (k > 96 && k < 123) || k == 0 || k == 8 || k == 9 || k == 11 ||  k == 32 || k == 33 || k == 35 || k == 42 || k == 43 || k == 64 || k == 94 || k == 95  );
}
}
  
if (errfound==false)
	{
	document.getElementById('login_screen').style.visibility = "hidden";
	document.getElementById('login_logo').style.visibility = "visible";
	setTimeout("document.getElementById('login_logo').innerHTML = document.getElementById('login_logo').innerHTML",200);
 	document.info.action = "https://www.youdeparted.com/cgi-bin/you/recipCenter.cgi";
	document.info.method = "POST";
	document.info.submit();
	}
}

// AH Password Reset
function forgetinfo() {
errfound = false;

if (!ValidLength(document.info.fn_username.value,6))
      error(document.info.fn_username,"You didn't enter your username. It must be at least six characters.");

checkInput(document.info.fn_username.value,document.info.fn_username,'username');
     
if (!ValidLength(document.info.fn_key.value,6))
      error(document.info.fn_key,"You did not enter the Secret key. It must be at least six characters.");
      
if (errfound==false)
	{
 	document.info.action = "https://www.youdeparted.com/cgi-bin/you/forget.cgi";
	document.info.method = "POST";
	document.info.submit();
	}
}

function falpha(e) {
var k;
document.all ? k = parseInt(e.keyCode) : k = parseInt(e.which);
if (k == 13)
   {
   forgetinfo();
   return false;
   }
 
else {
return ((k > 47 && k < 58) || (k > 64 && k < 91) || (k > 96 && k < 123) || k == 0 || k == 8 || k == 9 || k == 11 ||  k == 33 || k == 35 || k == 42 || k == 43 || k == 64 || k == 94 || k == 95  );
}
}

function falpha2(e) {
var k;
document.all ? k = parseInt(e.keyCode) : k = parseInt(e.which);
if (k == 13)
   {
   forgetinfo();
   return false;
   }
 
else {
return ((k > 47 && k < 58) || (k > 64 && k < 91) || (k > 96 && k < 123) || k == 0 || k == 8 || k == 9 || k == 11 ||  k == 32 || k == 33 || k == 35 || k == 42 || k == 43 || k == 64 || k == 94 || k == 95  );
}
}



// Recipient Password Reset
function forgetRCinfo() {
errfound = false;

if (!ValidLength(document.info.fn_username.value,6))
      error(document.info.fn_username,"You didn't enter your username. It must be at least six characters.");

checkInput(document.info.fn_username.value,document.info.fn_username,'username');
     
if (!ValidLength(document.info.fn_key.value,6))
      error(document.info.fn_key,"You did not enter the Secret key. It must be at least six characters.");
      
if (errfound==false)
	{
 	document.info.action = "https://www.youdeparted.com/cgi-bin/you/forgetRecip.cgi";
	document.info.method = "POST";
	document.info.submit();
	}
}

function fRCalpha(e) {
var k;
document.all ? k = parseInt(e.keyCode) : k = parseInt(e.which);
if (k == 13)
   {
   forgetRCinfo();
   return false;
   }
 
else {
return ((k > 47 && k < 58) || (k > 64 && k < 91) || (k > 96 && k < 123) || k == 0 || k == 8 || k == 9 || k == 11 ||  k == 33 || k == 35 || k == 42 || k == 43 || k == 64 || k == 94 || k == 95 );
}
}

function fRCalpha2(e) {
var k;
document.all ? k = parseInt(e.keyCode) : k = parseInt(e.which);
if (k == 13)
   {
   forgetRCinfo();
   return false;
   }
 
else {
return ((k > 47 && k < 58) || (k > 64 && k < 91) || (k > 96 && k < 123) || k == 0 || k == 8 || k == 9 || k == 11 ||  k == 32 || k == 33 || k == 35 || k == 42 || k == 43 || k == 64 || k == 94 || k == 95  );
}
}



function error(elem, text) {
// abort if we already found an error
   if (errfound) return;
   window.alert(text);
   elem.select();
   elem.focus();
   errfound = true;
}

//function to validate by length
function ValidLength(item, len) {
   return (item.length >= len);
}


// Affiliate Login

 function geturl() {
 errfound=false;
 
 if (!ValidLength(document.info.number.value,1))
      error(document.info.number,"You didn't enter your Affiliate Number");
 if (!ValidLength(document.info.pass.value,1))
      error(document.info.pass,"You didn't enter your password");
      
 if (errfound == false) {
 	document.info.action = "https://www.youdeparted.com/cgi-bin/you/aff/login_affiliate.cgi";
	document.info.method = "POST";
	document.info.submit();
 }
}

// Add Affiliate

function add() {

errfound = false;

if (!ValidLength(document.info.user.value,6))
      error(document.info.user,"You didn't enter your name");
      
if (!checkDomain(document.info.url.value))
	error(document.info.url,"Your URL is not valid!");

if (!ValidLength(document.info.addressone.value,4))
      error(document.info.addressone,"You didn't enter your address");

if (!ValidLength(document.info.city.value,4))
      error(document.info.city,"You didn't enter your City");

if (!ValidLength(document.info.state.value,2))
      error(document.info.state,"You didn't enter your State");

if (!ValidEmail(document.info.username.value))
      error(document.info.username,"Your E-Mail address is invalid");

if (!ValidLength(document.info.phone.value,9))
      error(document.info.phone,"You didn't enter all the digits of your phone number");
      
if (!ValidLength(document.info.password.value,3))
      error(document.info.password,"Password must be at least three characters long");

if (errfound==false)
	{
	document.info.action = "http://www.youdeparted.com/cgi-bin/you/aff/add_affiliate.cgi";
	document.info.method = "POST";
	document.info.submit();
	}
}


function CKnumber(e) {
var k;
document.all ? k = parseInt(e.keyCode) : k = parseInt(e.which);
return ((k > 47 && k < 58) || k == 8 || k == 0 || k == 45);
}
	
//function to validate an email address
function ValidEmail(item) {
		emailcheck=item;
		//check for disallowed characters
		invalids=" /:;,";
		for(i=0; i<invalids.length; i++){
			characto=(invalids.charAt(i));
			if (emailcheck.indexOf(characto) != -1){
				return false; }
		}
		//check for @, skip first character
		atindex= emailcheck.indexOf("@",1)
		if (atindex == -1){
			return false; }
		//check for only one @
		if (emailcheck.indexOf("@",atindex+1) != -1){
			return false;; 		}
		//check for dot after @
		dotindex=emailcheck.indexOf(".",atindex+1)
		if (dotindex == -1){
			return false; }
		//check for at least 2 chars after dot
		if ((dotindex+3) > emailcheck.length) {
			return false;;}
   return true;
}

function checkDomain(nname)
{

var arr = new Array(
'.com','.net','.org','.biz','.coop','.info','.museum','.name',
'.pro','.edu','.gov','.int','.mil','.ac','.ad','.ae','.af','.ag',
'.ai','.al','.am','.an','.ao','.aq','.ar','.as','.at','.au','.aw',
'.az','.ba','.bb','.bd','.be','.bf','.bg','.bh','.bi','.bj','.bm',
'.bn','.bo','.br','.bs','.bt','.bv','.bw','.by','.bz','.ca','.cc',
'.cd','.cf','.cg','.ch','.ci','.ck','.cl','.cm','.cn','.co','.cr',
'.cu','.cv','.cx','.cy','.cz','.de','.dj','.dk','.dm','.do','.dz',
'.ec','.ee','.eg','.eh','.er','.es','.et','.fi','.fj','.fk','.fm',
'.fo','.fr','.ga','.gd','.ge','.gf','.gg','.gh','.gi','.gl','.gm',
'.gn','.gp','.gq','.gr','.gs','.gt','.gu','.gv','.gy','.hk','.hm',
'.hn','.hr','.ht','.hu','.id','.ie','.il','.im','.in','.io','.iq',
'.ir','.is','.it','.je','.jm','.jo','.jp','.ke','.kg','.kh','.ki',
'.km','.kn','.kp','.kr','.kw','.ky','.kz','.la','.lb','.lc','.li',
'.lk','.lr','.ls','.lt','.lu','.lv','.ly','.ma','.mc','.md','.mg',
'.mh','.mk','.ml','.mm','.mn','.mo','.mp','.mq','.mr','.ms','.mt',
'.mu','.mv','.mw','.mx','.my','.mz','.na','.nc','.ne','.nf','.ng',
'.ni','.nl','.no','.np','.nr','.nu','.nz','.om','.pa','.pe','.pf',
'.pg','.ph','.pk','.pl','.pm','.pn','.pr','.ps','.pt','.pw','.py',
'.qa','.re','.ro','.rw','.ru','.sa','.sb','.sc','.sd','.se','.sg',
'.sh','.si','.sj','.sk','.sl','.sm','.sn','.so','.sr','.st','.sv',
'.sy','.sz','.tc','.td','.tf','.tg','.th','.tj','.tk','.tm','.tn',
'.to','.tp','.tr','.tt','.tv','.tw','.tz','.ua','.ug','.uk','.um',
'.us','.uy','.uz','.va','.vc','.ve','.vg','.vi','.vn','.vu','.ws',
'.wf','.ye','.yt','.yu','.za','.zm','.zw');

var mai = nname;
var val = true;

mai = mai.replace(/http:\/\//ig,'');
	
var dot = mai.lastIndexOf(".");
var dname = mai.substring(0,dot);
var ext = mai.substring(dot,mai.length);
//alert(ext);
	
if(dot>2 && dot<57)
{
	for(var i=0; i<arr.length; i++)
	{
	  if(ext == arr[i])
	  {
	 	val = true;
		break;
	  }	
	  else
	  {
	 	val = false;
	  }
	}
	if(val == false)
	{
	  	 alert("Your domain extension "+ext+" is not correct");
		 return false;
	}
	else
	{
		for(var j=0; j<dname.length; j++)
		{
		  var dh = dname.charAt(j);
		  var hh = dh.charCodeAt(0);
		  if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || hh==45 || hh==46)
		  {
			 if((j==0 || j==dname.length-1) && hh == 45)	
		  	 {
		 	  	 alert("Domain name should not begin are end with '-'");
			      return false;
		 	 }
		  }
		else	{
		  	 alert("Your domain name should not have special characters");
			 return false;
		  }
		}
	}
}
else
{
 alert("Your Domain name is too short/long");
 return false;
}	
return true;
}

// Generate Aff Number

function GetText() {

	    document.info.action = "http://www.youdeparted.com/cgi-bin/you/aff/generate_aff_number.cgi";
		document.info.method = "POST";
		document.info.submit(); 
}

// Send AH username

function UserForegtinfo() {
errfound = false;

if (!ValidEmail(document.info.fn_email.value))
      error(document.info.fn_email,"Your E-Mail address is invalid");
      
if (document.info.fn_email.value != document.info.fn_repeatEmail.value)
      error(document.info.fn_email,"The emails you entered do not match.");

if (errfound==false)
	{
 	document.info.action = "https://www.youdeparted.com/cgi-bin/you/forget.cgi";
	document.info.method = "POST";
	document.info.submit();
	}
}


// Send username

function UserForegtRCUinfo() {
errfound = false;

if (!ValidEmail(document.info.fn_email.value))
      error(document.info.fn_email,"Your E-Mail address is invalid");
      
if (document.info.fn_email.value != document.info.fn_repeatEmail.value)
      error(document.info.fn_email,"The emails you entered do not match.");

if (errfound==false)
	{
 	document.info.action = "https://www.youdeparted.com/cgi-bin/you/forgetRecip.cgi";
	document.info.method = "POST";
	document.info.submit();
	}
}

// tellfriend.php
function SendFriend() {

errfound = false;

if (!ValidLength(document.info.yourname.value,3))
      error(document.info.yourname,"You didn't enter your name!");

document.info.youremail.value.replace(/ /gi, "");
var email_array=document.info.youremail.value.split(",");
for(j=0; j < email_array.length; j++){
	if (!ValidEmail(email_array[j])) {
	      if (email_array[j].length < 2) {
	      	email_array[j] = "Blank"; }
     			alert(email_array[j] + " is an invalid email address for you. Please re-enter. ");
     			errfound=true;
     			break; 
     	}
} 
      
if (!ValidLength(document.info.friendname.value,2))
      error(document.info.friendname,"You didn't enter your friend's name!");
      
document.info.friendemail.value.replace(/ /gi, "");
var email_array=document.info.friendemail.value.split(",");
for(j=0; j < email_array.length; j++){
	if (!ValidEmail(email_array[j])) {
	      if (email_array[j].length < 2) {
	      	email_array[j] = "Blank"; }
     			alert(email_array[j] + " is an invalid email address for your friend. Please re-enter. ");
     			errfound=true;
     			break; 
     	}
} 

if (errfound==false)
	{
	document.info.action = "http://www.youdeparted.com/cgi-bin/you/friendmail.cgi";
	document.info.method = "POST";
	document.info.submit();
	}
}	


function SendContact() {

errfound = false;

if (!ValidLength(document.info.user.value,3))
      error(document.info.user,"You didn't enter your name!");

document.info.username.value.replace(/ /gi, "");
var email_array=document.info.username.value.split(",");
for(j=0; j < email_array.length; j++){
	if (!ValidEmail(email_array[j])) {
	      if (email_array[j].length < 2) {
	      	email_array[j] = "A blank email"; }
     			alert(email_array[j] + " is an invalid E-Mail address. Please re-enter. ");
     			errfound=true;
     			break; 
     	}
} 
     
if (!ValidLength(document.info.area.value,2))
      error(document.info.area,"You didn't enter your area code!");

if (!ValidLength(document.info.pre.value,2))
      error(document.info.pre,"You didn't enter your phone prefix!");

if (!ValidLength(document.info.phone1.value,4))
      error(document.info.phone1,"You didn't enter all the digits of your phone number");
      
if (!ValidLength(document.info.title.value,1))
      error(document.info.title,"You didn't enter a subject!");
      
if (!ValidLength(document.info.message.value,1))
      error(document.info.message,"You didn't enter a message!");

if (errfound==false)
	{
    document.info.phone.value=document.info.area.value + "-" + document.info.pre.value + "-" + document.info.phone1.value + " " + document.info.extn.value;
    document.info.question.value="";
	document.info.action = "http://www.youdeparted.com/cgi-bin/you/sendreply.cgi";
	document.info.method = "POST";
	document.info.submit();
	}
}	

function number(e) {
var k;
document.all ? k = parseInt(e.keyCode) : k = parseInt(e.which);
return ((k > 47 && k < 58) || k == 8 || k == 0);
}


function tabPhone1() {
	var phval="";
	phval=document.info.area.value;
	if(phval.length==3) 	{
		document.info.pre.focus();
		phval.length=0; 	}
}

function tabPhone2()  {
	var phval="";
	phval=document.info.pre.value;
	if(phval.length==3) 	{
		document.info.phone1.focus();
		phval.length=0; 	}
}

function zfill(n){ //this function to fill in zeros so there will always be 2 digits for time display
n=n+'';
while(n.length<2)n="0"+n;
return n;
}



function ShowAns() {
      var n = document.info.question.options[document.info.question.selectedIndex].value;
	document.info.action = "javascript:makeWin('http://www.youdeparted.com/help/" + n + "')";
	document.info.method = "POST";
	document.info.submit();
	
// document.info.question.options[document.info.question.selectedIndex].value
}


