function showErrorMessages(fieldname,errorno)
{
	if(errorno==0)
		alert('Please enter your ' + fieldname);
	else
		if(errorno==1)
			alert('Value entered for ' + fieldname + 'is not valid , You can only use alphabeats A-z,a-z, digits 0-9 or underscore _');
		else
			if(errorno==2)
				alert('Please enter numeric value (Only Numbers) for ' + fieldname);
			else
				if(errorno==3)
					alert(fieldname + ' must have at least 6 characters.');
				else
					if(errorno==4)
						alert(fieldname + ' must have atleast 4 characters.');
}

function customErrorMessages(errorMessage)
{
	alert ( errorMessage );
}

function checkBlank(input)
{
	if(input == "" || input == null)
		return (false)

	return (true)
}

function positiveInteger(input,fnm)
{
	if ( checkBlank(input) )
	{
		for(var i=0; i < input.length ; i++)
		{
			if ( input.charAt(i) < "0" || input.charAt(i) > "9" )
			{
				showErrorMessages(fnm,2)
				return (false)
			}
		}
		return (true)
	}
	showErrorMessages(fnm,0)
	return (false)
}

function validateEmail(input)
{
	checkFlag = checkBlank(input)

	if (checkFlag)
	{	
		emailString = input

		emailArrayAt = emailString.split("@")
		lenAt = emailArrayAt.length

		emailArrayDot = emailString.split(".")
		lenDot = emailArrayDot.length

		if (checkFlag)
			if (lenAt==2)
				checkFlag = ( checkBlank(emailArrayAt[1]) && checkBlank(emailArrayAt[0]) )
			else
				checkFlag = false

		if (checkFlag)
			if (lenDot >= 2)
				checkFlag = checkBlank(emailArrayDot[1])
			else
				checkFlag = false

		if (checkFlag)
			checkFlag = checkBlank(emailArrayDot[lenDot-1])
	}

	if ( checkFlag )
		return (true)
	else
	{
		alert('Please enter valid Email address');
		return(false)
	}

/*
		var invalid = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
		var valid = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid

		if(  ( !(invalid.test(input)) && valid.test(input) ) )
*/

}

function checkAlphaNumeric_password(input)
{
	var nre = /\W/g
	r = nre.test(input)
	return(!r)

/*	note !r is important as nre checks for chars other than alphanums  */
}

function checkAlphaNumeric_user_details(input)
{

	var a = /[*-+,.><?';":}{`~!@#$%^&)(\[\]\/=\|\\]/
	r = a.test(input)

	var b = /\w/
	rb = b.test(input)

	return( !r && rb)
}

function checkUserName(input,fieldname)
{
        if ( checkBlank(input) )
                if( checkAlphaNumeric_password(input) )
                        if (input.length >= 3)
                                return(true)
                        else
                                customErrorMessages(fieldname + ' must have at least 3 characters.')
                else
                                customErrorMessages('Your ' + fieldname + ' can only use letters A-Z, a-z, numbers 0-9 or underscore and no spaces')
        else
                showErrorMessages(fieldname,0)

        return(false)
}

function checkAll_password(input,fieldname)
{
        if ( checkBlank(input) )
                if( checkAlphaNumeric_password(input) )
                        if (input.length >= 6)
                                return(true)
                        else
                                showErrorMessages(fieldname,3)
                else
                        showErrorMessages(fieldname,1)
        else
                showErrorMessages(fieldname,0)

        return(false)
}

function checkAll_password1(input,fieldname,emsg)
{

        if ( checkBlank(input) )
                if( checkAlphaNumeric_password(input) )
                        if (input.length >= 6)
                                return(true)
                        else
				if(emsg='')
				{
                                	showErrorMessages(fieldname,3)
				}else
				{	
					customErrorMessages(emsg)		
				}
                else
			if(emsg='')
			{
                            showErrorMessages(fieldname,1)
			}else
			{	
				customErrorMessages(emsg)		
			}
        else
		if(emsg='')
		{
                    showErrorMessages(fieldname,0)
		}else
		{	
			customErrorMessages(emsg)		
		}
        return(false)
}

function checkAll_cdkeys(input,fieldname)
{
        if ( checkBlank(input) )
                if( checkAlphaNumeric_password(input) )
                        if (input.length >= 4)
                                return(true)
                        else
		                customErrorMessages('Please enter ' + fieldname + ' set of four characters of the registration number.')
                else
				customErrorMessages('Invalid Value entered for ' + fieldname + ' four characters of the registration number.')
        else
                customErrorMessages('Please enter ' + fieldname + ' four characters of the registration number.')

        return(false)
}



function checkAll_user_details(input,fieldname)
{
        if ( checkBlank(input) )
                if( checkAlphaNumeric_user_details(input) )
                        return(true)
                else
                        showErrorMessages(fieldname,1)
        else
                showErrorMessages(fieldname,0)

        return(false)
}

function openWindow_pp()
{
	
	pwdwindow = window.open('privacy_policy','privacy_policy','resizable=0,width=400,height=200,scrollbars=1')
	pwdwindow.moveTo(0,0);
}

function openWindow_cp()
{
	pwdwindow = window.open('change_password','change_password','resizable=0,width=550,height=250,scrollbars=1');
	pwdwindow.moveTo(0,0);
}

// for change in password

function ValidateForm_change_password(FormRef)
{

	checkFlag = true

	var FieldNames = new Array()
	var valueIds = new Array()

	FieldNames[0]  = 'Password '
	valueIds[0] = FormRef.newpassword.value

	FieldNames[1]  = 'Confirm Password'
	valueIds[1] = FormRef.confirmnew.value

	if (checkFlag)
		for(var i=0;i<=1;i++)
			if( !( checkAll_password( valueIds[i],FieldNames[i]) ) )
			{
				checkFlag = false		
				break;
			}

	if(checkFlag)
	{
		if ( valueIds[0]!=valueIds[1])
			alert('Password and re-entered Password are not matching');
		else
			FormRef.submit();
	}
}

// user name

function ValidateForm_username(FormRef)
{

	checkFlag = true

	var FieldNames = new Array()
	var valueIds = new Array()

	FieldNames[0]  = 'Username'
	valueIds[0] = FormRef.username.value

	if (checkFlag)
		if( !( checkUserName( valueIds[0],FieldNames[0]) ) )
			checkFlag = false
	if (checkFlag)
		FormRef.submit();
}

// cdkey

function ValidateForm_CdKey(FormRef)
{

	checkFlag = true

	var FieldNames = new Array()
	var valueIds = new Array()

	FieldNames[0]  = '1st'
	valueIds[0] = FormRef.cdkey1.value

	FieldNames[1]  = '2nd'
	valueIds[1] = FormRef.cdkey2.value

	FieldNames[2]  = '3rd'
	valueIds[2] = FormRef.cdkey3.value

	FieldNames[3]  = '4th'
	valueIds[3] = FormRef.cdkey4.value

	if (checkFlag)
		for (i=0;i<4;i++)
			if( !( checkAll_cdkeys( valueIds[i],FieldNames[i]) ) )
			{
				checkFlag = false		
				break;
			}
	if (checkFlag)
		FormRef.submit()
}

// password 

function ValidateForm_password(FormRef)
{

	checkFlag = true

	var FieldNames = new Array()
	var valueIds = new Array()

	FieldNames[0]  = 'Password '
	valueIds[0] = FormRef.password.value

	FieldNames[1]  = 're-Entered Password '
	valueIds[1] = FormRef.confirm.value

	if (checkFlag)
		for(var i=0;i<=1;i++)
			if( !( checkAll_password( valueIds[i],FieldNames[i]) ) )
			{
				checkFlag = false		
				break;
			}

	if(checkFlag)
	{
		if ( valueIds[0]!=valueIds[1])
			alert('Password and re-entered Password are not matching');
		else
			FormRef.submit();
	}
}

// email

function checkExternalEmail(input,fieldname)
{

	var a = /[*+,><?';":}{`~!#@$%^&)(\[\]\/=\|\\]/
	var b = /\w/

        if ( checkBlank(input) )
	{
		if ( input.indexOf(' ')== -1 )
		{
			r = a.test(input)
			rb = b.test(input)
			if ( !r && rb )
				return (true)
			else
				customErrorMessages('Invalid Email (104)')
		}
		else
			customErrorMessages('Invalid Email (109)')
	}
        else
		//showErrorMessages(fieldname,0)
		customErrorMessages('Please enter your E-mail address.')

        return(false)
}

function checkExternalEmail1(input,fieldname)
{

	var a = /[*+,><?';":}{`~!#@$%^&)(\[\]\/=\|\\]/
	var b = /\w/

        if ( checkBlank(input) )
	{
		if ( input.indexOf(' ')== -1 )
		{
			r = a.test(input)
			rb = b.test(input)
			if ( !r && rb )
				return (true)
			else
				customErrorMessages('Invalid Email (123)')
		}
		else
			customErrorMessages('Invalid Email (146)')
	}
        else
		//showErrorMessages(fieldname,0)
		customErrorMessages('Please enter your E-mail address again.')
        return(false)
}

function ValidateForm_externalemail(FormRef)
{
	checkFlag = true

	var FieldNames = new Array()
	var valueIds = new Array()

	FieldNames[0]  = 'Email '
	valueIds[0] = FormRef.emailp1.value

	FieldNames[1]  = 'Email '
	valueIds[1] = FormRef.emailp2.value

	FieldNames[2]  = 'Confirm Email '
	valueIds[2] = FormRef.confirmemailp1.value

	FieldNames[3]  = 'Confirm Email '
	valueIds[3] = FormRef.confirmemailp2.value

	if (checkFlag)
		if(checkExternalEmail(valueIds[0],FieldNames[0],''))
		{
			if(!checkExternalEmail1(valueIds[0],FieldNames[0] ))
			{	
				checkFlag=false
			}
		}else
		{
			checkFlag=false

		}
	email = valueIds[0] + valueIds[1]
	confirmEmail = valueIds[2] + valueIds[3]

	Uemail = email.toUpperCase()
	UconfirmEmail = confirmEmail.toUpperCase()

	if ( email != confirmEmail )
	{
		alert (' The two E-mail address you entered do not match. ');
		checkFlag = false
	}

	if (checkFlag)
		FormRef.submit()
}

// username_cdkey_password

function ValidateForm_username_cdkey_password(FormRef)
{

	checkFlag = true

	var FieldNames = new Array()
	var valueIds = new Array()

	FieldNames[0]  = 'Username '
	valueIds[0] = FormRef.username.value

	FieldNames[1]  = 'CDKEY '
	valueIds[1] = FormRef.cdkey.value

	FieldNames[2]  = 'Password '
	valueIds[2] = FormRef.password.value

	FieldNames[3]  = 're-Entered Password '
	valueIds[3] = FormRef.confirm.value

	if (checkFlag)
		for(var i=0;i<=3;i++)
			if( !( checkAll_password( valueIds[i],FieldNames[i]) ) )
			{
				checkFlag = false		
				break;
			}

	if(checkFlag)
	{
		if ( valueIds[2]!=valueIds[3])
			alert('Password and re-Entered Password are not matching');
		else
			FormRef.submit();
	}
}

// mail username & password to user

function validate_mail_password(FormRef)
{

	input = FormRef.email.value

	if ( validateEmail(input) )
		FormRef.submit();
}

// edit user details and user details

function ValidateForm(FormRef)
{

	checkFlag = true

	var FieldNames = new Array()
	var valueIds = new Array()

	FieldNames[0]  = 'First name '
	valueIds[0] = FormRef.firstname.value

	FieldNames[1]  = 'Last name '
	valueIds[1] = FormRef.lastname.value

	FieldNames[2]  = 'Course-Name '
	valueIds[2] = FormRef.coursename.value

	FieldNames[3]  = 'Town/City '
	valueIds[3] = FormRef.towncity.value

	FieldNames[4]  = 'County/Provinance/State '
	valueIds[4]  = FormRef.provinancestate.value

	FieldNames[5]  = 'Country '
	valueIds[5] = FormRef.country.value

	FieldNames[6]  = 'Address line 1 '
	valueIds[6] = FormRef.address1.value

	FieldNames[8]  = 'Address line 2 '
	valueIds[8] = FormRef.address2.value

	FieldNames[7]  = 'Postal/Zip code '
	valueIds[7] = FormRef.postalzip.value

	FieldNames[9]  = 'email '
	valueIds[9] = FormRef.externalemail1.value

	FieldNames[10]  = 'email '
	valueIds[10] = FormRef.externalemail2.value

	if (checkFlag)
		for(var i=0;i<=2;i++)
		{
			if (i<2)
			{
				if( !( checkAll_user_details( valueIds[i],FieldNames[i] ) ) )
				{
					checkFlag = false		
					break;
				}
			}
			if (i==2)
			{
				if ( ! ( checkBlank(valueIds[i] ) ) )
				{
				   checkFlag = false
				   customErrorMessages("Please enter the Name that the website calls you.")
                                   //showErrorMessages( FieldNames[i],0)
				   break;
				}
			}
		}

	if (checkFlag)
		for(var i=3;i<8;i++)
			if( !( checkBlank(valueIds[i]) ) )
			{
				//showErrorMessages(FieldNames[i],0)
				customErrorMessages("Please enter " + FieldNames[i] + "." )
				checkFlag = false		
				break;
			}
/*
	if (checkFlag )
		checkFlag = checkAll_user_details( valueIds[8],FieldNames[8] )

//  checkBlank 			showErrorMessages( FieldNames[9],0)

*/
	if (checkFlag)
		if ( ! (  checkExternalEmail( valueIds[9],'Email' ) ) )
		{
			checkFlag = false
		}

	if (checkFlag)
		if ( ! ( checkExternalEmail( valueIds[10],'Email' ) ) )
		{
			checkFlag = false
		}

	return(checkFlag)

/*	if (checkFlag )
		checkFlag = positiveInteger( valueIds[8],FieldNames[8] ) */

/*	if (checkFlag)
		checkFlag = validateEmail( valueIds[9] )  */

}

function execute(value,url,FRef)
{
	var browserName = navigator.appName;
	var title = document.title;
	//alert(title);

	if(!value)
	{
		alert('Error while processing, select an option ');
		return 0;
	}
			
	if(browserName == 'Microsoft Internet Explorer')
	{
		if(value=='favourites')
		{
			//window.external.AddFavorite(url,title);
			window.external.AddFavorite(url,'Foundations21 homepage')
		}
		if(value=='homepage')		
		{
			BRef = FRef.buttonUpdate
			BRef.style.behavior='url(#default#homepage)';
			BRef.setHomePage(url);
		}
	}
	else
	{
	        if (value=='homepage')
		{  featureText='The ability to make Foundations 21 your home page '
		}
		else
		{  featureText='The ability to add Foundations 21 to your favourites '
		}
		alert(featureText+' is currently not available for your browser (' + browserName + ').  We recommend using Internet Explorer with Foundations 21');	
	}
}

function setOtherDetails(FormRef)
{
	favourites = FormRef.favourites.checked
	favouritesurl = document.forms[0].favourites.value
	if( favourites )       
		
		execute('favourites',favouritesurl,FormRef)

	homepage = FormRef.homepage.checked
	homepageurl = document.forms[0].homepage.value	
	if( homepage )
		execute('homepage',homepageurl,FormRef)

	FormRef.submit();

}

function setPreferenceAdd(FormRef)
{
	if (ValidateForm(FormRef))
	{
		FormRef.submit();
	}
}

function setPreference(FormRef)
{
	if (ValidateForm(FormRef))
	{
		setOtherDetails(FormRef)
	}
}


// password recovery

function ValidateForm_lost_password(FormRef)
{

	checkFlag = true

	var FieldNames = new Array()
	var valueIds = new Array()

	FieldNames[0]  = 'Username '
	valueIds[0] = FormRef.username.value

	FieldNames[1]  = 'CDKEY '
	valueIds[1] = FormRef.cdkey.value
     

 
                       if( (!(checkUserName(valueIds[0],FieldNames[0]))) && checkFlag)
                         {
                          checkFlag=false
                          
                          }
        checkFlag1=false;
        if (( checkBlank(valueIds[1]) )  &&  checkFlag)        
 
                if( checkAlphaNumeric_password(valueIds[1]) )
                        if (valueIds[1].length >= 16)
                                checkFlag1=true;
                        else
                                alert('Registration number must have at least 16 characters.');
                             
                              
                else
                        alert('Value entered for Registration number is not valid , You can only use alphabeats A-z,a-z, digits 0-9 or underscore _');
                         
                
        else
                alert('Please enter your Registration number');
        
       

 if (checkFlag1)      
 {
 	if(checkFlag)
	{
		FormRef.submit();
	}
 }
}

