function verify(){
    var elem_email = document.getElementById('_txtEmailAddress');
    var elem_verifyemail = document.getElementById('_txtVerifiedEmailAddress');
    var elem_headertext = document.getElementById('_lblHeaderText');
    var elem_txtPassword = document.getElementById('_txtPassword');
    if( elem_email.value != elem_verifyemail.value || (isEmail(elem_email.value)==false) || (isEmail(elem_verifyemail.value)==false) ){
//        alert(elem_email.value);
        toggleForm(0);
        elem_headertext.innerHTML = 'The email addresses you entered do not match. Please enter and verify a valid email address and click Continue.';
    }
    else {
        try{ checkuser(elem_email.value); }
        catch(val){}
    }

}
function toggleForm(state){
    var elem_continue = document.getElementById('_tblContinue');   
    var elem_password = document.getElementById('_tblPassword');
    var elem_profile = document.getElementById('_tblProfileInfo');
    
//    switch( state ){
//        case 0:
//            elem_continue.style.display = 'block';
//            elem_password.style.display = 'none';
//            elem_profile.style.display = 'none';
//            break;
//        case -1:
//            elem_continue.style.display = 'block';
//            elem_password.style.display = 'none';
//            elem_profile.style.display = 'none';
//            showLogin(0);
//               break;
//        case 1:
//            elem_continue.style.display = 'none';
//            elem_password.style.display = 'block';
//            elem_profile.style.display = 'block';
//            break;
//    }
    if( state == 0 ){ //toggle form off but allow registration
        elem_continue.style.display = 'block';
        elem_password.style.display = 'none';
        elem_profile.style.display = 'none';
    }
    else if( state == 1 ){//toggle form on and allow registration
//        elem_continue.style.display = 'none';
//        elem_password.style.display = 'block';
//        elem_profile.style.display = 'block';
        showLogin(2);
    }
    else if( state == -1 ){//toggle form off and display login box
//        elem_continue.style.display = 'block';
//        elem_password.style.display = 'none';
//        elem_profile.style.display = 'none';
        showLogin(3);
    }
}
function ddlIndustry_Change(){
    var elem_ddlIndustry = document.getElementById('_ddlIndustry');
    var elem_trPriTherArea = document.getElementById('_trPriTherArea');
    var elem_ddlPriTherArea = document.getElementById('_ddlPriTherArea');
    var elem_trSecTherArea = document.getElementById('_trSecTherArea');
    var elem_ddlSecTherArea = document.getElementById('_ddlSecTherArea');

    var selidx = elem_ddlIndustry.selectedIndex;
    if( selidx > 0 && selidx < 4 ) {
//        alert(elem_ddlIndustry.selectedIndex);
        elem_trPriTherArea.style.display = '';
        elem_trSecTherArea.style.display = '';
    }
    else{
        elem_trPriTherArea.style.display = 'none';
        elem_trSecTherArea.style.display = 'none';
    }
 }
 function ddlCountries_Change(){
    elem_countries = document.getElementById('_ddlCountries');
    elem_ddlUS = document.getElementById('_ddlUS');
    elem_ddlCA = document.getElementById('_ddlCA');
    elem_txtState = document.getElementById('_txtState');
    
    switch( elem_countries.value ){
        case "US":
            elem_ddlUS.style.display = '';
            elem_ddlCA.style.display = 'none';
            elem_txtState.style.display = 'none';
            break;
        case "CA":
            elem_ddlUS.style.display = 'none';
            elem_ddlCA.style.display = '';
            elem_txtState.style.display = 'none';
            break;
        default:
            elem_ddlUS.style.display = 'none';
            elem_ddlCA.style.display = 'none';
            elem_txtState.style.display = '';
            break;
    }
 }
 
 //////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////////////////////
 
 function isEmail(string) 
{
	var iChars = "*|,\":<>[]{}`\';()&$#% ";
	var atPos;
	var pdPos;
	if (!string) return false;
	for (var i = 0; i < string.length; i++) 
		{
		if (iChars.indexOf(string.charAt(i)) != -1)
        return false;
         }
	atPos = string.indexOf ("@",1)
	if (atPos == -1) 
		{
		return false
		}
	if ( string.indexOf ("@", atPos+1) != -1) 
		{
		return false
		}
	pdPos = string.indexOf (".", atPos)
	if ( pdPos == -1) 
		{
		return false
		}
	if ( pdPos+3 > string.length)    
        {
		return false
        }
   return true;
}
                      
function isProper(string, iLength) 
{
	var iChars = "*|\":<>[]{}\;()@&$%~";
	if (!string) return false;

	for (var i = 0; i < string.length; i++) 
	{
		if (iChars.indexOf(string.charAt(i)) != -1)
		return false;
	}
	
	if (iLength.length != 0)
	{
		if (string.length > iLength || string.length < 1)
		{
			return false;
		}
	}
	return true;
}
 
function isValidPassword(string) 
{
	var iChars = "*|,\":<>[]{}`\;()@&$%~";
	var bNumber = false;
	var bAlpha = false;

	if (!string) return false;

	if (string.length < 8) return false;
	
	if (string.length > 15) return false;
	
	for (var i = 0; i < string.length; i++)
	{
		if (isNaN( string.charAt(i)))
			{//alert( string.charAt(i));
			bAlpha = true;}
		if (string.charAt(i) >= "0" && string.charAt(i) <= "9")
		{	//alert( string.charAt(i));
			bNumber = true;}
	}
	
	if (bNumber == false || bAlpha == false) 
		return false;
	
	return true;
}

function isValidPhone(string)
{
	if(string.length>20){return false;}
	if(string.length<5){return false;}
	for(var i=0;i<string.length;i++)
	{
		if(isNaN(string.charAt(i))){
			return false;
/*			if(string.charAt(i)!='-'||string.charAt(i)!='('||string.charAt(i)!=')'||string.charAt(i)!='.'||string.charAt(i)!=' '){return false;} */
		}
	}
	return true;	
} 

function isValidAreaCode(string)
{
	if(string.length>8){return false;}
	if(string.length<1){return false;}
	for(var i=0;i<string.length;i++)
	{
		if(isNaN(string.charAt(i))){return false;}
	}
	return true;
}

function isNumeric(string, iValue)
{
	if (!string) return false;
	for (var i=0;i<string.length;i++)
	{
		if (isNaN(string.charAt(i)))
		{
			return false;
		}
	}
	if (string.length > iValue || string.length < 1)
	{
		return false;
	}
	return true;
}

function isMatch( string1, string2 )
{
	if (string1 != string2 )
	{
		return false;
	}
	
	return true;
}
function emailIsVerified(){
	if (isEmail(document.getElementById('_txtEmailAddress').value) == false) {
		alert("Please enter a valid email address.");
		document.getElementById('_txtEmailAddress').focus();
		return false;
	}
	if (isMatch(document.getElementById('_txtEmailAddress').value, document.getElementById('_txtVerifiedEmailAddress').value) == false) {
		alert("The email addresses you entered do not match.\n\nPlease confirm your email address.\n\nNote:  Your email address will be used to receive confirmations and reminders from the system.");
		document.getElementById('_txtVerifiedEmailAddress').focus();
		return false;
	}
	document.RegisterUser.submit();
}
function isComplete() 
{
//    alert(document.getElementById('_txtEmailAddress').value);
//    return false;
    var valmsg = 'Please fix the following problems:<ul>';
    var arr = [];
	if (isEmail(document.getElementById('_txtEmailAddress').value) == false) {
		arr.include('Please enter a valid email address.');
//        alert("Please enter a valid email address.");
//		document.getElementById('_txtEmailAddress').focus();
//		document.getElementById('_txtEmailAddress').select();
//		return false;
	}
	if (isMatch(document.getElementById('_txtEmailAddress').value, document.getElementById('_txtVerifiedEmailAddress').value) == false) {
		arr.include('The email addresses you entered do not match.');
//		alert("The email addresses you entered do not match.\n\nPlease confirm your email address.\n\nNote:  Your email address will be used to receive confirmations and reminders from the system.");
//		document.getElementById('_txtVerifiedEmailAddress').focus();
//		document.getElementById('_txtVerifiedEmailAddress').select();
//		return false;
	}
    if (isValidPassword(document.getElementById('_txtPassword').value) == false) {
          arr.include('Please enter a valid password of 8-15 alpha-numeric characters (both alpha & numeric required).');
//        alert("Please enter a valid password of 8-15 alpha-numeric characters (both alpha & numeric required).");
//        document.getElementById('_txtPassword').value = "";
//        document.getElementById('_txtVerifiedPassword').value = "";
//        document.getElementById('_txtPassword').focus();
//        document.getElementById('_txtPassword').select();
//        return false;
    }
    if (isMatch(document.getElementById('_txtPassword').value, document.getElementById('_txtVerifiedPassword').value) == false) {
        arr.include('The passwords you have entered do not match.  Please ensure that the passwords match.');
//        alert("The passwords you have entered do not match.  Please re-enter the information.");
//        document.getElementById('_txtPassword').value = "";
//        document.getElementById('_txtVerifiedPassword').value = "";
//        document.getElementById('_txtPassword').focus();
//        document.getElementById('_txtPassword').select();
//        return false;
    }
    if($('_ddlIndustry').getSelected().get('value')=='-1'){
        arr.include('Please select an industry.');
    }
    if($('_ddlPriTherArea').getSelected().get('value')=='-1'){
        arr.include('Please specify the primary therapeutic area.');
    }
    if($('_ddlSecTherArea').getSelected().get('value')=='-1'){
        arr.include('Please specify the secondary therapeutic area.');
    }
	if (isProper(document.getElementById('_txtInstitution').value,100) == false) {
        arr.include('Please enter the institutional name / place of employment.');
//        alert("Please enter the name or your institution / place of employment.");
//        document.getElementById('_txtInstitution').focus();
//        document.getElementById('_txtInstitution').select();
//        return false;
    }
	if (isProper(document.getElementById('_txtJobTitle').value,100) == false) {
	    arr.include('Please enter the user\'s role.  (Manager, Technician, etc...)');
//        alert("Please enter your role within your organization.  (Manager, Technician, etc...)");
//        document.getElementById('_txtJobTitle').focus();
//        document.getElementById('_txtJobTitle').select();
//        return false;
    }
    if($('_ddlSalutation').getSelected().get('value')=='0'){
        arr.include('Please select a courtesy title.');
    }        
	if (isProper(document.getElementById('_txtFirstName').value,25) == false) {
	    arr.include('Please enter a valid first name.');
//        alert("Please enter a valid first name.");
//        document.getElementById('_txtFirstName').focus();
//        document.getElementById('_txtFirstName').select();
//        return false;
    }
	if (isProper(document.getElementById('_txtLastName').value,25) == false) {
	    arr.include('Please enter a valid last name.');
//        alert("Please enter a valid last name.");
//        document.getElementById('_txtLastName').focus();
//        document.getElementById('_txtLastName').select();
//        return false;
    }
    if($('_ddlEducationLevels').getSelected().get('value')=='0'){
        arr.include('Please select an education level.');
    }                 
    if (isProper(document.getElementById('_txtAddress1').value,50) == false) {
        arr.include('Please enter a valid street address.');
//        alert("Please enter a valid street address.");
//        document.getElementById('_txtAddress1').focus();
//        document.getElementById('_txtAddress1').select();
//        return false;
    }
    if( document.getElementById('_txtAddress2').length>0 ){
        if (isProper(document.getElementById('_txtAddress1').value,50) == false) {
            arr.include('The secondary address field contains invalid characters. The field is optional.');
    //        alert("Please enter a valid street address.");
    //        document.getElementById('_txtAddress1').focus();
    //        document.getElementById('_txtAddress1').select();
    //        return false;
        }
    }
    if($('_ddlCountries').getSelected().get('value')=='0'){
        arr.include('Please select the country of residence.');
    }
    else{
        var country=$('_ddlCountries').getSelected().get('value');
        if( country=='CA' ){
            if($('_ddlCA').getSelected().get('value')=='0'){
                arr.include('Please select the Canadian province / territory of residence.');
            }
        }
        else if( country=='US' ){
            if($('_ddlUS').getSelected().get('value')=='0'){
                arr.include('Please select the United States state of residence.');
            }
        }
        else if(isProper($('_txtState').get('value'),50)==false){
            arr.include('Please enter the state / area or residence.');
        }
    }
    if (isProper(document.getElementById('_txtCity').value,50) == false) {
        arr.include('Please enter a valid city.');
//		alert("Please enter a valid city.");
//        document.getElementById('_txtCity').focus();
//        document.getElementById('_txtCity').select();
//        return false;
    }        
	if (isProper(document.getElementById('_txtZipCode').value,20) == false) {
	    arr.include('Please enter a valid five (5) digit zip/postal code.');
        //alert("Please enter a valid five (5) digit zip/postal code.");
//        alert("Please enter a zip/postal code.");
//        document.getElementById('_txtZipCode').focus();
//        document.getElementById('_txtZipCode').select();
//        return false;
    }    
    if (isValidAreaCode(document.getElementById('_txtAreaCode').value) == false) {
        arr.include('Please enter the area/city code as a numeric string of characters: e.g. 455")');
//		alert("Please enter the area/city code as a numeric string of characters:\n\n"+
//			"Example: 455");
//		document.getElementById('_txtAreaCode').focus();
//		document.getElementById('_txtAreaCode').select();
//		return false;
    }    
    if (isValidPhone(document.getElementById('_txtPhoneNumber').value) == false) {
        arr.include('Please enter the phone number as a string of numeric characters: e.g. for 302-3113, you should enter 3023113');
//		alert("Please enter the phone number as a string of numeric characters:\n\n"+
//			"Example: For 302-3113, you should enter 3023113.");
//		document.getElementById('_txtPhoneNumber').focus();
//		document.getElementById('_txtPhoneNumber').select();
//		return false;
	}
	if(document.getElementById('_txtPhoneExtension').value.length>0){	
		if(isNumeric(document.getElementById('_txtPhoneExtension').value, 5)== false){
		    arr.include('Please enter the phone extension as a string of 5 or fewer numeric characters: e.g. 4458');
//			alert("Please enter the phone extension as a string of 5 or fewer numeric characters:\n\n"+
//				"Example: 4458.");
//			document.getElementById('_txtPhoneExtension').focus();
//			document.getElementById('_txtPhoneExtension').select();
//			return false;
		}
	}

    if( arr.length>0 ){
        $('regInstructions').setStyle('display','none');
        $('regValidatorBox').setStyle('display','block');
        $('regValidatorBox').set('text','');
        setvaltext(arr);

        return false;
    }
    else{
        $('regInstructions').setStyle('display','none');
        $('regValidatorBox').setStyle('display','block');
        $('regValidatorBox').set('text','');
//        var valhead = new Element('div', {id: 'valhead', html: 'This will be hooked in to the database after Thursday night\'s upload.','style': 'margin:0 10px;font-weight:normal;text-align:left;'});
//        valhead.inject($('regValidatorBox'));
    }

//	if(document.getElementById('reg_frm')!='undefined'){
//		document.getElementById('reg_frm').style.display = 'none';
//	}	
    return true;
}
function setvaltext(arr){
    try{
        var valhead = new Element('div', {id: 'valhead', html: 'Please fix the following problems:','style': 'margin:0 10px;font-weight:normal;font-size:0.9em;text-align:left;'});
        valhead.inject($('regValidatorBox'));
        var valUL  = new Element('ul', {id: 'valUL','style': 'margin:0 10px;color:red;text-align:left;'});
        valUL.inject($('regValidatorBox'));
        arr.each(function(item){
            var valLI  = new Element('li', {'html': item, 'style': 'margin:10px 10px;color:red;text-align:left;border-bottom:1px dotted #777;font-size:0.9em;'});
            valLI.inject(valUL);
            var hello = '';
        });
    }
    catch(err){alert(err);}
    //return msg;
}
