<!--
/*****************************************************************************
 * Copyright (c) 2001 Multex.com, Inc. All Rights Reserved.
 *
 * File:        validate
 * Author:		Jacob Metcalf ( jacob.metcalf@multex.com )
 * Description: 
 *	
 ****************************************************************************/

/*****************************************************************************
 * Function:		mxValidatePassword
 * Description:		User preferences form specific validation.
 *
 ****************************************************************************/
function mxValidatePassword(action,formName)
{
	var newPassword			= eval('document.'+formName+'.newPassword.value')
	var userPassword		= eval('document.'+formName+'.userPassword.value')
	var confirmPassword		= eval('document.'+formName+'.confirmPassword.value')
	var userLogin			= eval('document.'+formName+'.userLogin.value')

	
	//test for illegal chars
	var legalCharsReg = "[^a-zA-Z0-9_\\s]";
	var regexLegalChars = new RegExp(legalCharsReg);

	//user didnt confirm the new password correctly
	if ( newPassword != confirmPassword ){
		alert('Your new passwords do not match');
				
	//users new pass is too short or long.
	}else if ( newPassword.length < 6 || newPassword.length >= 30 ) {
		//alert(newPassword.length);
		alert('Your password must be between at least 6 but no more than 30 characters long');
	
	//user didnt enter legal chars
	}else if ( regexLegalChars.test(newPassword) ) {
		alert('Your password must contain valid charactors');
		
	//username and pass cant be the same
	}else if ( userLogin == newPassword ) {
		alert('Your password must not be your user ID');
	
	//user didnt enter orignal password	(only for change password page. pass expired page doesnt have userPassword)
	}else if ( action == 'setUserPasswordPage' && ( newPassword !='' && userPassword == '' || userPassword == '')) {
		alert('You must enter your original password if you wish to change it');
	
	//current and new passwords the same (only for change password page. pass expired page doesnt have userPassword)
	}else if ( action == 'setUserPasswordPage' && ( newPassword == userPassword )) {
		alert('You must enter a different password from your current one');
					
						
	//else if everthing is cool....
	}else{
		mxPostEvent(action,formName);
	}
}

/*****************************************************************************
 * Function:		mxValidateAdvSearch
 * Description:		Advanced search form specific validation.
 *
 ****************************************************************************/
function mxValidateAdvSearch( eventID, formName, dateFormat, tzOffset )
{
	var form = eval( "document." + formName );
	var errorMessage = "";
/**	alert(form.subjectsSUB.value);
	var PUB = form.subjectsPUB.options[form.subjectsPUB.selectedIndex].value;
	var SUB = form.subjectsSUB.value;
	var SPE = form.subjectsSPE.value;
	var EVE = form.subjectsEVE.value;
	alert(PUB + ',' + SUB + ',' + SPE + ',' + EVE);
	var subjects;**/
	//alert('mxValidateAdvSearch: dateFormat= ' + dateFormat + ';' + form.fromDay.value);
	if ( form == null ) 
	{
		alert( "Error: mxValidateAdvSearch() Could not find form '" + formName + "'."  ); 
	}
	else
	{
		if ( eval( "document." + formName + ".dateRange.value" ) == 'customDate' )
		{
			/**
			SJN 8 Jun 2002 - during dev it turned out that passing tzOffset here was actually messing up
			to and from dates in the dateRangeLib.  FTB, setting tzOffset to zero here to ensure that the
			to and from date aren't modified
			**/
			//if ( mxValidateOptionDate( formName, 'from', 'dateFrom', dateFormat, tzOffset, true, true ) == false )
			if ( mxValidateOptionDate( formName, 'from', 'dateFrom', dateFormat, '', true, true ) == false )
				{ 
					errorMessage += "'From Date' not a valid date\n"; 
				}
				
			if ( eval( "document." + formName + ".toDay" ) != null )
			{
			//	if ( mxValidateOptionDate( formName, 'to', 'dateTo', dateFormat, tzOffset, true, true ) == false )
				if ( mxValidateOptionDate( formName, 'to', 'dateTo', dateFormat, '', true, true ) == false )
					{ 
						errorMessage += "'To Date' not a valid date\n"; 
					}	
			}
			else
			{
				errorMessage += "'To Date' not a valid date\n"; 
			}
		}
		
		
		if ( errorMessage.length > 0 )
		{
			alert( errorMessage );
		}
		else
		{
			mxPostEvent( eventID ,formName, 'analysts','*selectAll*', 'companies','*selectAll*');
			return true;
		}
	}
}

/*****************************************************************************
 * Function:		mxValidateResultsSearchPost
 * Description:		Advanced search form specific validation.
 *
 ****************************************************************************/
function mxValidateResultsSearchPost( eventID, formName, dateFormat, tzOffset )
{
	var form = eval( "document." + formName );
	var errorMessage = "";

	//alert('mxValidateAdvSearch: dateFormat= ' + dateFormat + ';' + form.fromDay.value);
	if ( form == null ) 
	{
		alert( "Error: mxValidateAdvSearch() Could not find form '" + formName + "'."  ); 
	}
	else
	{
		if ( eval( "document." + formName + ".dateRange.value" ) == 'customDate' )
		{
			if ( mxValidateOptionDate( formName, 'from', 'dateFrom', dateFormat, tzOffset, true, true ) == false )
				{ 
					errorMessage += "'From Date' not a valid date\n"; 
				}
				
			if ( eval( "document." + formName + ".toDay" ) != null )
			{
				if ( mxValidateOptionDate( formName, 'to', 'dateTo', dateFormat, tzOffset, true, true ) == false )
					{ 
						errorMessage += "'To Date' not a valid date\n"; 
					}	
			}
			else
			{
				errorMessage += "'To Date' not a valid date\n"; 
			}
		}

		if ( eval( "document." + formName + ".pageNumber" ) != null ) 
		{	
			var pageNumber	= eval( "document." + formName + ".pageNumber" ); 
			pageNumber.value = "";
		}
		
		if ( errorMessage.length > 0 )
		{
			alert( errorMessage );
		}
		else
		{
			mxPostEvent( eventID ,formName);
			return true;
		}
	}
}

/*****************************************************************************
 * Function:		mxSetCustomDate
 * Description:		date validation and setting for Advanced Search
 *
 ****************************************************************************/
function mxSetCustomDate(formName, dateRange, datePart){
	
	var dateRangeObj = eval( "document." + formName + "." + dateRange );
	var datePartObj = eval ("document." + formName + "." + datePart )
	
	if ( dateRangeObj == null ){
		alert( "Error: mxSetCustomDate() Could not find field '" + dateRangeObj +"'."  ); 
	}
	else if (datePartObj == null){
		alert( "Error: mxSetCustomDate() Could not find field '" + datePartObj +"'."  ); 	
	}
	else{
		if (dateRangeObj.value != 'customDate'){
			if ( datePartObj.value != ''){
				for (i=0; i<dateRangeObj.options.length; i++){
					if (dateRangeObj.options[i].value == 'customDate'){
						dateRangeObj.options[i].selected = true
						break;
					}
				}
			}
		}
	}		

}

/*****************************************************************************
 * Function:		mxResetToFromDates
 * Description:		resets To and From Date sections to blank if the date range
 *					selector is set to non-Custom Date
 *
 ****************************************************************************/
function mxResetToFromDates(formName, dateRange){

	var dateRangeObj = eval( "document." + formName + "." + dateRange );
	var datePartObjStem = eval( "document." + formName );

	if ( dateRangeObj == null ){
		alert( "Error: mxSetCustomDate() Could not find field '" + dateRangeObj +"'."  ); 
	}
	else{
		if (dateRangeObj.value != 'customDate'){
			datePartObjStem.fromDay.options[0].selected = true;		
			datePartObjStem.fromMonth.options[0].selected = true;
			datePartObjStem.fromYear.options[0].selected = true;	
			datePartObjStem.toDay.options[0].selected = true;	
			datePartObjStem.toMonth.options[0].selected = true;	
			datePartObjStem.toYear.options[0].selected = true;	
			datePartObjStem.dateFrom.value = '';
			datePartObjStem.dateTo.value = '';
		}
	}	

}

/*****************************************************************************
 * Function:		mxCheckSavedSearchName
 * Description:		Validates the Saved Search name before submitting the search
 *					for saving.
 * Author:			Sarah Newton 14th May 2002
 *
 ****************************************************************************/
function mxCheckSavedSearchName( eventID, formName )
{
	var form = eval( "document." + formName );
	var profileName = form.profileName.value;
	if  ( profileName=='' )
	{
		alert('Please enter a name for your Saved Search');
	}
	else
	{
		var legalCharsReg = "[^a-zA-Z0-9_\\s]";
		var regexLegalChars = new RegExp(legalCharsReg);
		if (regexLegalChars.test(profileName))
		{
			alert('Your Saved Search name contains an invalid character');
		}	
		else
		{
			mxPostEvent('getProfileCreatePage',formName);
		}
	}
} 




/*****************************************************************************
 * Function:		validatePublicContactFormEmail
 * Description:		
 *					
 * Author:			
 *
 ****************************************************************************/
function validateContactFormEmail( eventID, formName )
{
	var form = eval( "document." + formName );
	var validationType = form.validation.value;
	
	if ( form.fullName ) {	var fullName	= form.fullName.value; }
	if ( form.userName ) {	var userName	= form.userName.value; }
	if ( form.company ) {	var company		= form.company.value; }
	if ( form.email ) {		var email		= form.email.value; }
	if ( form.contact ) {	var contact		= form.contact.value; }
	if ( form.phone ) {		var phone		= form.phone.value; }
	if ( form.query ) {		var query		= form.query.value; }
	if ( form.firstName ) { var firstName	= form.firstName.value; }
	if ( form.surname ) {	var lastName	= form.surname.value; }
	if ( form.country ) {	var country		= form.country.value; }
	
	if ( form.mInitials ) {	var mInitials	= form.mInitials.value; }
	if ( form.address1 ) {	var address1	= form.address1.value; }
	if ( form.address2 ) {	var address2	= form.address2.value; }
	if ( form.cityState ) {	var cityState	= form.cityState.value; }
	if ( form.postZip ) {	var postZip		= form.postZip.value; }
	

	var validation	= false;
	
	//test email for @ and .
	var legalCharsReg = "[/@]";
	var regexLegalChars = new RegExp(legalCharsReg);
	
	
	switch( validationType ){
		case 'existingUser':
			if ( fullName.length == 0 || fullName.length > 100){
				alert('Please enter your full name. It must be no longer than 100 characters.');
				
			}else if ( company.length == 0 || company.length > 100){
				alert('Please enter your company. It must be no longer than 100 characters.');
			
			}else if ( regexLegalChars.test(email) == false || ( email.length == 0 || email.length > 100 ) ) { //check that its got @
				alert('Please enter a valid email address. It must contain an \'@\' symbol and be no longer than 100 characters.');
					
			}else if ( contact.length == 0 || contact.length > 100 ){
				alert('Please enter your contact at ING. It must be no longer than 100 characters.');
			
			}else if ( query.length == 0 || query.length > 2000){
				alert('Please enter your query. It must be no longer than 2000 characters');
				
			}else{validation = true;}

			break;	
			
		case 'password':
			if ( fullName.length == 0 || fullName.length > 100){
				alert('Please enter your full name. It must be no longer than 100 characters.');
				
			} else if ( userName.length == 0 || userName.length > 100){
				alert('Please enter your userName. It must be no longer than 100 characters.');
				
			}else if ( company.length == 0 || company.length > 100){
				alert('Please enter your company. It must be no longer than 100 characters.');
			
			}else if ( regexLegalChars.test(email) == false || ( email.length == 0 || email.length > 100 ) ) { //check that its got @
				alert('Please enter a valid email address. It must contain an \'@\' symbol and be no longer than 100 characters.');
					
			}else if ( contact.length == 0 || contact.length > 100 ){
				alert('Please enter your contact at ING. It must be no longer than 100 characters.');
			
			}else{validation = true;}	

			break;			
				
		case 'apply': 
			if ( firstName.length == 0 || firstName.length > 50){
				alert('Please enter your first name. It must be no longer than 50 characters.');
				
			}else if ( mInitials.length > 10){
				alert('Please restrict your middle initials to be no longer than 10 characters.');
				
			} else if ( lastName.length == 0 || lastName.length > 50){
				alert('Please enter your surname. It must be no longer than 50 characters.');
				
			}else if ( company.length == 0 || company.length > 100){
				alert('Please enter your company. It must be no longer than 100 characters.');
				
			}else if ( address1.length > 50){
				alert('Please restrict the first line of your address to be no longer than 50 characters.');
				
			}else if ( address2.length > 50){
				alert('Please restrict the second line of your address to be no longer than 50 characters.');
				
			}else if ( cityState.length > 50){
				alert('Please restrict the city/state you live in to be no longer than 50 characters.');
				
			}else if ( country.length == 0 || country.length > 50){
				alert('Please enter the country you that you live in. It must be no longer than 50 characters.');
			
			}else if ( postZip.length > 15){
				alert('Please restrict your post or zip code to be no longer than 15 characters.');
					
			}else if ( phone.length == 0 || phone.length > 50 ){ //also check that its numeric
				alert('Please enter your phone number. It must be no longer than 50 characters.');
				
			}else if ( regexLegalChars.test(email) == false || ( email.length == 0 || email.length > 100 ) ) { //check that its got @
				alert('Please enter a valid email address. It must contain an \'@\' symbol and be no longer than 100 characters.');
				
			}else if ( contact.length == 0 || contact.length > 100 ){
				alert('Please enter your contact at ING. It must be no longer than 100 characters.');
				

			}else{validation = true;}
			
			break;		
				
		case 'enquiry': 
			if ( fullName.length == 0 || fullName.length > 100){
				alert('Please enter your full name. It must be no longer than 100 characters.');
					
			}else if ( phone.length == 0 || phone.length > 50 ){ //also check that its numeric
				alert('Please enter your phone number. It must be no longer than 50 characters.');
				
			}else if ( regexLegalChars.test(email) == false || ( email.length == 0 || email.length > 100 ) ) { //check that its got @
				alert('Please enter a valid email address. It must contain an \'@\' symbol and be no longer than 100 characters.');
				
			}else if ( query.length == 0 || query.length > 2000){
				alert('Please enter your query. It must be no longer than 2000 characters');
				
			}else if ( company.length > 100){
				alert('Please restrict the name of your company to be no longer than 100 characters.');
			
			}else{validation = true;}	
			
			break;	
			
		case 'anyother': 
			if ( fullName.length == 0 || fullName.length > 100){
				alert('Please enter your full name. It must be no longer than 100 characters.');
					
			}else if ( phone.length == 0 || phone.length > 50 ){ //also check that its numeric
				alert('Please enter your phone number. It must be no longer than 50 characters.');
				
			}else if ( regexLegalChars.test(email) == false || ( email.length == 0 || email.length > 100 ) ) { //check that its got @
				alert('Please enter a valid email address. It must contain an \'@\' symbol and be no longer than 100 characters.');
				
			}else if ( query.length == 0 || query.length > 2000){
				alert('Please enter your query. It must be no longer than 2000 characters');
				
			}else if ( company.length > 100){
				alert('Please restrict the name of your company to be no longer than 100 characters.');
			
			}else{validation = true;}	
			
			break;						
	}
	

	if ( validation ){
		mxPostEvent( eventID,formName)
	}
	
} 

//-->