//----------------------------------------------------------------
//  Group DCA Registration Page Javascript functions
//  Copyright © 2003 Group DCA
//----------------------------------------------------------------

//----------------------------------------------------------------
// Notes:
//	1. Optional parameters are enclosed in brackets->[optional_param]
//----------------------------------------------------------------

//----------------------------------------------------------------
// Registration Validation
//----------------------------------------------------------------
var   iValidatingElement = -1;

//these arrays contains the relative object number of the input item for all variables that
//are required to be entered on the form.  these items also match up with the editdiv#
//for each variable within each td block.  If an item is not required, there must be an
//editdiv# statement for that variable at the bottom of the html.  This is done to keep
//all the index loops in sync for the editdiv# stuff when displaying the individual error
//text for each input variable  
var	  requiredElmiron = new Array(0,1,2,5);	
var	  requiredRegister = new Array(0,1,2,4,5,6,7,8,9,14,15,16,17,19,20,21,22); 	
var	  requiredArray = new Array();	

function testalert() {
  alert("HEREU");
  }


function iValidation(sType, fMinimum, fMaximum, iLength, iForm, iElement, sElementName)
{
	var NumericValue;
	var AtSignIndex;
	var PeriodIndex;
	var HoldAddress;

	if (
		iValidatingElement != iElement
        && iValidatingElement != -1
       )
		return false;

	iValidatingElement = iElement;

	if (sType.indexOf('Required') >= 0)
	{
		if (
	        document.forms[iForm].elements[iElement].value == null
            ||
	        document.forms[iForm].elements[iElement].value == ""
	        ||
	        document.forms[iForm].elements[iElement].value == " "
	       )

		{
			alert("Required Field");
			document.forms[iForm].elements[iElement].focus();
			return false;
		}
	}

	if (
        sType.indexOf('E-Mail') >= 0
        && document.forms[iForm].elements[iElement].value != ""
       )
	{
		AtSignIndex = document.forms[iForm].elements[iElement].value.indexOf('@');
		if (
	        AtSignIndex <= 0
	        ||
	        AtSignIndex == document.forms[iForm].elements[iElement].value.length - 1
	       )
		{
			alert("Invalid E-Mail Address");
			document.forms[iForm].elements[iElement].focus();
			return false;
		}
	}

	if (
        sType.indexOf('Number') >= 0
        && document.forms[iForm].elements[iElement].value != ""
       )
	{
		if (isNaN(document.forms[iForm].elements[iElement].value))
		{
			alert("Invalid Number");
			document.forms[iForm].elements[iElement].focus();
			return false;
		}
	}

	if (
        sType.indexOf('Integer') >= 0
        && document.forms[iForm].elements[iElement].value != ""
       )
	{
		PeriodIndex = document.forms[iForm].elements[iElement].value.indexOf('.');
		if (PeriodIndex >= 0)
		{
			alert("Field Cannot Contain a Decimal Point");
			document.forms[iForm].elements[iElement].focus();
			return false;
		}
	}

	if (
        sType.indexOf('Minimum') >= 0
        && document.forms[iForm].elements[iElement].value != ""
       )
	{
		NumericValue = parseFloat(document.forms[iForm].elements[iElement].value);
		if (NumericValue < fMinimum)
		{
			alert("Number Must be Greater Than or Equal to " + fMinimum);
			document.forms[iForm].elements[iElement].focus();
			return false;
		}
	}

	if (
        sType.indexOf('Maximum') >= 0
        && document.forms[iForm].elements[iElement].value != ""
       )
	{
		NumericValue = parseFloat(document.forms[iForm].elements[iElement].value);
		if (NumericValue > fMaximum)
		{
			alert("Number Must be a Number Less Than or Equal to " + fMaximum);
			document.forms[iForm].elements[iElement].focus();
			return false;
		}
	}

	if (
        sType.indexOf('Length') >= 0
        && document.forms[iForm].elements[iElement].value != ""
       )
	{
		if (document.forms[iForm].elements[iElement].value.length < iLength)
		{
			alert("Field Must have a Length Greater Than or Equal to " + iLength);
			document.forms[iForm].elements[iElement].focus();
			return false;
		}
	}


	if (
        sType.indexOf('NoPOBox') >= 0
        && document.forms[iForm].elements[iElement].value != ""
       )
	{


		HoldAddress = document.forms[iForm].elements[iElement].value.toUpperCase();

		if (HoldAddress.indexOf('PO BOX') >= 0 ||
			HoldAddress.indexOf('P.O. BOX') >= 0 ||
			HoldAddress.indexOf('POBOX') >= 0 ||
			HoldAddress.indexOf('P.O.BOX') >= 0 ||
			HoldAddress.indexOf('P.O.') >= 0)
		{
			alert("Address cannot be a PO Box");
			document.forms[iForm].elements[iElement].focus();
			return false;
		}
	}


	if (sType.indexOf('Email') >= 0 && document.forms[iForm].elements[iElement].value != "") {
	    if (checkEmail(iForm,iElement) == false) {
	   	   return false;
	   	}
	}

	checkOne(iForm,iElement);
	iValidatingElement = -1;
}

      function checkEmail(iForm, iElement) {
		iValidatingElement = iElement;

        var str=document.forms[iForm].elements[iElement].value;

        var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid email address. Please"
	                   +" check the location of the '@' sign."  );
		   document.forms[iForm].elements[iElement].focus();
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr-1 || str.substr(lstr-1,1)==dot) {
		   alert("Invalid email address. Please"
	                   +" check the location of the '.' (period character)."  );
		   document.forms[iForm].elements[iElement].focus();
		   return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   alert("Invalid email address. Please"
	                   +" remove extra '@' signs."  );
		   document.forms[iForm].elements[iElement].focus();
		   return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   alert("Invalid email address. Please"
	                   +" check location of period character."  );
		   document.forms[iForm].elements[iElement].focus();
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		   alert("Invalid email address. Please"
	                   +" enter period character after '@' sign."  );
		   document.forms[iForm].elements[iElement].focus();
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		   alert("Invalid email address. Please"
	                   +" remove any spaces."  );
		   document.forms[iForm].elements[iElement].focus();
		    return false;
		 }

	iValidatingElement = -1;
      }



function iPacCodeCheck(iLength, iForm, iElement)
{
	if (
		iValidatingElement != iElement
        && iValidatingElement != -1
       )
		return false;

		PeriodIndex = document.forms[iForm].elements[iElement].value.indexOf("_");
		if ( PeriodIndex == 0)
		{
			return true;
		}
	iValidatingElement = iElement;

		if (
	        document.forms[iForm].elements[iElement].value == null
            ||
	        document.forms[iForm].elements[iElement].value == ""
	        ||
	        document.forms[iForm].elements[iElement].value == " "
	       )
		{
		return true;
		}
	if (document.forms[iForm].elements[iElement].value.length < iLength)
	{
		alert("Field Must have a Length Greater Than or Equal to " + iLength);
		document.forms[iForm].elements[iElement].focus();
		return false;
	}
	iValidatingElement = -1;
}

function iPasswordCheck(iLength, iForm, iElement)
{
	if (
		iValidatingElement != iElement
        && iValidatingElement != -1
       )
		return false;

	iValidatingElement = iElement;

	if (document.forms[iForm].elements[iElement].value.length < iLength
        && document.forms[iForm].elements[iElement].value != "")
	{
		alert("Field Must have a Length Greater Than or Equal to " + iLength);
		document.forms[iForm].elements[iElement].focus();
		return false;
	}

	if (document.forms[iForm].elements[iElement].value == document.forms[iForm].elements[iElement - 1].value
        && document.forms[iForm].elements[iElement - 1].value != ""
        && document.forms[iForm].elements[iElement].value != "")
	{
		alert("User Password Must Not Be the Same as User Login");
		document.forms[iForm].elements[iElement].focus();
		return false;
	}

	checkOne(iForm,iElement);
	iValidatingElement = -1;
}

function iPasswordConfirmCheck(iLength, iForm, iElement)
{
	if (
		iValidatingElement != iElement
        && iValidatingElement != -1
       )
		return false;

	iValidatingElement = iElement;

	if (document.forms[iForm].elements[iElement].value.length < iLength
        && document.forms[iForm].elements[iElement].value != "")
	{
		alert("Field Must have a Length Greater Than or Equal to " + iLength);
		document.forms[iForm].elements[iElement].focus();
		return false;
	}

	if (document.forms[iForm].elements[iElement].value != document.forms[iForm].elements[iElement - 1].value
		&& document.forms[iForm].elements[iElement - 1].value != ""
        && document.forms[iForm].elements[iElement].value != "")
	{
		alert("User Password Must Be the Same as User Password Confirm");
		document.forms[iForm].elements[iElement].focus();
		return false;
	}

	checkOne(iForm,iElement);
	iValidatingElement = -1;
}

function showorhide1(f)
//this function is used to show or hide the 'Processing Your Registration' text
//1=show 0=hide
{
if(f==1){visi="visible";}
else{visi="hidden";}
if(document.layers){

document.regmsg.visibility=visi;
}
if(document.all){

document.all.regmsg.style.visibility=visi;
}
if(document.getElementById){

document.getElementById("regmsg").style.visibility=visi;
}
} 

function showorhide2(f,i)
//this function is used to show or hide each individual
//validation message for all required form fields 
{
var element = "editdiv" + i;

if(f==1){visi="visible";}
else{visi="hidden";}

if(document.layers){
document.editdiv.visibility=visi;
}

if(document.all){
eval("document.all.editdiv" + i + ".style.visibility=visi");
}

if(document.getElementById){
document.getElementById(element).style.visibility=visi;
}
} 

function resetAll(iForm)
//this function resets all the validation text strings for
//the required form fields based on the array defined on top
//of this script. the appropriate array gets passed in and
//set in the iSubmit function. 
{
  for (var i=0; i<requiredArray.length; i++)
  {
	showorhide2(0,requiredArray[i]);
  }
  document.forms[iForm].elements[1].focus();
  return true;
}

function checkOne(iForm,iElement)
//this function checks the validity for a single form field.
//the array goes backwards so the focus can be set to the topmost
//form field that is required and not yet entered.
//the 7,8 and 9 stuff is for the 3 parts of the telephone
//number (these are relative form objects 7, 8 and 9)
{
var errorFlag = false;
var j;

	if (iElement==7||iElement==8||iElement==9)
	   for (var i=7;i<=9;i++)
	   {
		j=9;
		if (document.forms[iForm].elements[i].value == "" ||
		    document.forms[iForm].elements[i].value == " " ||
		    document.forms[iForm].elements[i].value == null)
			{
			showorhide2(1,9);
			errorFlag = true;
			break;
			}
		}
	else if (document.forms[iForm].elements[iElement].value == "" ||
	    document.forms[iForm].elements[iElement].value == " " ||
	    document.forms[iForm].elements[iElement].value == null)
		{
		   showorhide2(1,iElement);
		   errorFlag = true;
		}
	else
		j=iElement;

	if (errorFlag == false)
		showorhide2(0,j);	
}

function checkAll(iForm)
//this function checks that data has been entered for all required 
//form fields. the array goes backwards so the focus can be set to
//the topmost form field that is required and not yet entered.
//the 7,8 and 9 stuff is for the 3 parts of the telephone
//number (these are relative form objects 7, 8 and 9)
{
var errorFlag = false;
var j;
  for (var i=requiredArray.length-1; 0<=i; i--)
  {
	if (document.forms[iForm].elements[requiredArray[i]].value == "" ||
	    document.forms[iForm].elements[requiredArray[i]].value == " " ||
	    document.forms[iForm].elements[requiredArray[i]].value == null)
	{
		errorFlag = true;
		if(requiredArray[i]==7 || requiredArray[i]==8)
		 j=8;
		else
		 j=i;
		showorhide2(1,requiredArray[j]);
	}
  }
  if (errorFlag == true)
  {
	  document.forms[iForm].elements[requiredArray[j]].focus();
	  return false;
  }
  else
	  return true;
}

function iSubmit(iForm,btn,action)
//the 'btn' variable is passed in to disable the submit button so the cgi program can
//only be called once (problem where people kept clicking it). While it's disabled, the
//'Processing your Registration" text appears on the page.  The 'action' variable shows
//what html page the 'Submit' is coming from. This is so the proper 'requiredArray' index
//can be set based on the form on the html page. required arrays per form are defined at 
//the top of this script
{
	if (action == 'elmiron')
	{
		requiredArray = requiredElmiron;
		//set the login to be the same as the email address for the elmiron quick register
		document.forms[iForm].elements[6].value = document.forms[iForm].elements[2].value;
	} else {
		requiredArray = requiredRegister;
	}
	resetAll(iForm);	
	if(checkAll(iForm) == true)
	{
		btn.disabled = true;
		showorhide1(1);
		document.forms[iForm].submit();
		return false;
	}
}

function write_numbers() {
     for (var i = 20; i < 25; i++ ) 
       document.write(' i = '  + i  +  '<br>'  );
  }


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() {
  var i,j=0,x,a=MM_swapImage.arguments;
   document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//----------------------------------------------------------------
// CheckLogin (site, calledfrom)
//
// This determines if a user is logged in by checking for the session cookie.
// If it's set, the doc's last name is then retrieved from the 'ln' cookie.
//If logged in already, the login/password entry fields are replaced with
//custom controls to perform functions against their account/profile    
// The parameters are:
//		site - valid DCA site variable (iex, dca, gtails, psr)
//		calledfrom - either 'cgi' or 'html' depending on whether its
//				called directly from an html page or from a page built via cgi-bin
//----------------------------------------------------------------

function CheckLogin(site,calledfrom) {
    var lname="";
    var sid="";
    var slash="'/'";
    var bslash="'../'";

    lname = GetCookie("ln");
    sid = GetCookie("session_id");
    if (sid == null)
    {
	   if (calledfrom == "html")
       {
          document.write('<img src="images/h_username.gif" alt="Username" width="57" height="15"><br>');
          document.write('<input type="text" name="login" size="12" maxlength="80"><br>');
          document.write('<img src="images/h_password.gif" alt="Password" width="54" height="15"><br>');
          document.write('<input type="password" name="password" size="12" maxlength="12">');
          document.write('<br><br>');
          document.write('<input name="submit" type="image" src="images/bn_login.gif" alt="Login" width="65" height="21" border="0">');
	   }
	   else
	   {
	      document.write('<img src="../images/h_username.gif" alt="Username" width="57" height="15"><br>');
          document.write('<input type="text" name="login" size="12" maxlength="80"><br>');
          document.write('<img src="../images/h_password.gif" alt="Password" width="54" height="15"><br>');
          document.write('<input type="password" name="password" size="12" maxlength="12">');
          document.write('<br><br>');
          document.write('<input name="submit" type="image" src="../images/bn_login.gif" alt="Login" width="65" height="21" border="0">');
	   }
    }
    else
    {
	   document.write('<p><font color="#FFFFFF" size="3" face="Verdana, Arial, Helvetica, sans-serif"><strong><br>'); 

	   if (lname == null)
	   {
	      document.write('Welcome Doctor<br><br>');
	   }
	   else
	   {
	      document.write('Welcome <br>');
	      document.write('Dr. ' + lname + '<br><br>');
	   }
	   if (calledfrom == "html")
	   {
	      document.write('<form name="form1" method="post" action="../cgi-bin/oraactl_psr.wrp"><br>');
		  document.write('<input type="hidden" name="site" value="' + site + '">');
		  document.write('<input type="image" src="images/bn_account.gif" border="0" align="left" ></form>');
          document.write('<br><a href="http://s3a.dotcomadvisors.com/pls/portal30/production.dca_rpt_' + site + '.show"><img src="images/bn_profile.gif" border="0"></a><br>');
	      document.write('<a href="javascript:logOffSite(' + slash + ')"><img src="images/bn_signout.gif" border="0"></a>');
	   }
	   else
	   {
 	      document.write('<form name="form1" method="post" action="../cgi-bin/oraactl_psr.wrp"><br>');
		  document.write('<input type="hidden" name="site" value="' + site + '">');
		  document.write('<input type="image" src="../images/bn_account.gif" border="0" align="left"></form>');
          document.write('<br><a href="http://s3a.dotcomadvisors.com/pls/portal30/production.dca_rpt_' + site + '.show"><img src="../images/bn_profile.gif" border="0"></a><br>');
          document.write('<a href="javascript:logOffSite(' + bslash + ')"><img src="../images/bn_signout.gif" border="0"></a>');
	   }
    }
}

function CheckDisplayTabs(site,calledfrom) {
    var lname="";
    var sid="";
    var slash="'/'";
    var bslash="'../'";
	var image41="'Image41','','images/main/menu_b_on.gif'";
	var image41pre="'Image41','','images/main/menu_b_pre_on.gif'";
	var image51="'Image51','','images/main/menu_c_on.gif'";
	var image51bs="'Image51','','../images/main/menu_c_on.gif'";
 	var linkName=document.URL;

	if (document.URL.indexOf('gtailsdev') >= 0)
	{
		var linkToUse = "dcadev";
		var portalSchema = "studiodev";
	}
	else
	{		 
		var linkToUse = "mygraphicdetail";
		var portalSchema = "production";
	}

    sid = GetCookie("session_id");
    if (sid == null)
    {
	   if (calledfrom == "html")
	   {	
          document.write('<td><a href="join.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(' + image41pre + ',1)">');
          document.write('<img src="images/main/menu_b_pre_off.gif" alt="Join Our g-Team" name="Image41" width="108" height="18" border="0" id="Image41"></a></td>');
          document.write('<td><a href="rewards.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(' + image51 + ',1)">');
          document.write('<img src="images/main/menu_c_off.gif" alt="g-team Rewards" name="Image51" width="110" height="18" border="0" id="Image51"></a></td>');
	   }
	   else
	   {
          document.write('<td><a href="../join.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(../' + image41pre + ',1)">');
          document.write('<img src="../images/main/menu_b_pre_off.gif" alt="Join Our g-Team" name="Image41" width="108" height="18" border="0" id="Image41"></a></td>');
          document.write('<td><a href="../rewards.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(../' + image51 + ',1)">');
          document.write('<img src="../images/main/menu_c_off.gif" alt="g-team Rewards" name="Image51" width="110" height="18" border="0" id="Image51"></a></td>');
	   }	
    }
    else
    {
	   if (calledfrom == "html")
	   {
	      document.write('<td><form name="form5" method="post" action="../cgi-bin/oraactl_psr.wrp">');
		  document.write('<input type="hidden" name="site" value="' + site + '">');
		  document.write('<input type="image" src="images/main/menu_b_off.gif" align="right" border="0"></form></td>');
          document.write('<td><a href="http://s3a.' + linkToUse + '.com/pls/portal30/' + portalSchema + '.dyn_newa_rpt.show?p_arg_names=practid&p_arg_values=' + sid + '&p_arg_names=pointid&p_arg_values=999&p_arg_names=rewards&p_arg_values=T" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(' + image51bs + ',1)">');
          document.write('<img src="images/main/menu_c_off.gif" alt="g-team Rewards" name="Image51" width="110" height="18" border="0" id="Image51"></a></td>');
	   }
	   else
	   {
	      document.write('<td><form name="form5" method="post" action="../cgi-bin/oraactl_psr.wrp">');
		  document.write('<input type="hidden" name="site" value="' + site + '">');
		  document.write('<input type="image" src="../images/main/menu_b_off.gif" align="right" border="0"></form></td>');
          document.write('<td><a href="http://s3a.' + linkToUse + '.com/pls/portal30/' + portalSchema + '.dyn_newa_rpt.show?p_arg_names=practid&p_arg_values=' + sid + '&p_arg_names=pointid&p_arg_values=999&p_arg_names=rewards&p_arg_values=T" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(' + image51bs + ',1)">');
          document.write('<img src="../images/main/menu_c_off.gif" alt="g-team Rewards" name="Image51" width="110" height="18" border="0" id="Image51"></a></td>');
	   }
    }
}



