// Validate Global Variables in Forms
// Added 01/16/02

/* USAGE GUIDE: Validate Object

PURPOSE:	Put common validation functions in one place to avoid duplication.

USAGE:		A global instance of this class has been declared in <cf_header>
			for use by all applications called gValidate.  To use this object
			write something like this: if( gValidate.isValidFileType() )...
			
			You can declare your own local instances of the class by writing:
			var myValidationObject = new Validate();

METHODS:	isValidFileType(filename, validFileTypeArray)
			This method is generally used to limit what types of files a user
			can upload.  Pass the filename string to check and an array of types 
			that can be uploaded. Returns true if filename is a valid type.
			NOTE: the array of file types should not include a '.' in front of them
			(ie. pass 'doc' to allow word files, NOT '.doc').
			
			isValidEmail(email)
			Pass the email address string you want to check.  Returns true if
			it's a properly formatted email address.  NOTE: This method maintains
			an array of valid domains (ie. .com, .tv, .gov, etc.).  To add new 
			domains to the validation system just add them to this array.
			
			isLeapYear(year)
			Returns true if year is a leap year else it returns false
			
			optionsSelected(collection)
			pass this function a mult. select box or a checkbox array to find out how many of the
			options are selected.  Returns the number of options selected in the specified 
			multiple select box or checkbox array.
			
*/

//constructor method
function Validate(){
	//assign functions defined below to these methods
	this.isValidFileType = validate_checkFileType;	
	this.isValidEmail = validate_checkEmail;
	this.isLeapYear = validate_checkLeapYear;
	this.optionsSelected = validate_optionsSelected;
}

//use weird function name to avoid conflicts
function validate_checkFileType(filename, validTypeArray){
	filename = filename.toLowerCase();
	//parse file extension out of filename without . at beginning
	var filetype = filename.substring( filename.lastIndexOf(".") + 1, filename.length );
	
	//don't allow empty fields
	if( filename == "" )
		return false;
	
	//search for valid extensions at the end of filename 
	for( var i = 0; i < validTypeArray.length; i++ )
		if( validTypeArray[i] == filetype )
			return true;
	
	return false;	//didn't find extension in filename so return false
}


//this function called if browser doesn't support RegExp object
function validate_simpleCheckEmail(email){
	//add new domains to this array when they are available
	var validDomainArray = new Array("com", "edu", "org", "net", "gov", "tv","info");
	//parse domain out of email without . at beginning
	var domain = email.substring( email.lastIndexOf(".") + 1, email.length );
	
	//don't allow empty fields
	if( email == "" )
		return false;
	
	//basic format checking
   	if (email.indexOf("@") <= 0 || 	// @ symbol found at least once and not first char
		email.indexOf(".") <= 0 ||	// . symbol found at least once and not first char
       	email.indexOf(' ') != -1 	// no spaces found in address
	)
		return false;
		
		
	//check for a valid domain at end of address
	for( var i = 0; i < validDomainArray.length; i++ )
		if( validDomainArray[i] == domain )
			return true;

	return false;	//invalid address if processing gets here

}

function validate_checkEmail(email) {
 // code from: http://webreference.com/js/column5/workaround.html
  if (window.RegExp) {
    var invalidPattern = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    var validPattern = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
    
	var reg1 = new RegExp(invalidPattern);
    var reg2 = new RegExp(validPattern);
	
   	//if email does not match the invalid pattern and does match the valid pattern then return true
    if (!reg1.test(email) && reg2.test(email))
      return true;
    
    return false;
  } else {
  		//browser doesn't support RegExp object so call simple email validation script
  		return validate_simpleCheckEmail(email);
  }
}

function validate_checkLeapYear(year) {
	year = parseInt(year);	//cast year to an integer
	return ( ((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0) );
}

//returns number of selected options in the passed in multiple select box or checkbox array
function validate_optionsSelected(collection){
	var numSelected = 0;
		
	if( collection.type == "select-multiple" ){					// multiple select box passed to function
															   //loop through options to find selected indexes
		for( var i = 0; i < collection.options.length; i++ )
			if( collection.options[i].selected )
				numSelected++;
	} else{																// checkbox array passed to function
		for( var i = 0; i < collection.length; i++ )
			if( collection[i].checked )
				numSelected++;
	}
	
	return numSelected;
}

// End Validate Global Variables in Forms

// Location Top of Screen
// Added 01/16/02

// if (top !=self) {
//       top.location = location
// }

// End Location Top of Screen

// Check Deletion

function areYouSure( message ) {
	return confirm( message );
}

// End Check Deletion

function checkAffiliate(form) {
	var validExtension = new Array("gif", "GIF", "jpg", "JPG", "jpeg", "JPEG");
	//var myURL = (form.affiliate_url.value);
	//var myRegExp =/^(http:\/\/)/gi;
	//check for valid Affiliate URL/web address
	//if ((form.affiliate_url.value != '') && (myURL.match(myRegExp))) {
		//newURLarray = myURL.match(myRegExp);
		 //if (newURLarray.length == 0) {
		//	alert("Please enter a valid Web Address, including the http:// at the beginning.");
		//	form.affiliate_url.focus();
		//	return false;
		 //}
	//}
	//else {
	//	  alert("Please enter a valid Web Address, including the http:// at the beginning.");
	 // 	  form.affiliate_url.focus();
	// 	  return false;
	//}	
	//use global Validate object to check file type
	if ((form.affiliate_image) && (form.affiliate_image.length > 0)) {
		if(!gValidate.isValidFileType(form.affiliate_image.value, validExtension) ){
			alert("Please select a GIF or JPEG file.");
			return false;
		}
		return true;
	}
}
function checkEzineForm(form) {
	var v = new Validate();
	if ((form.ezine_email) && (!v.isValidEmail(form.ezine_email.value))) {
		alert("Please enter a valid EMAIL ADDRESS.");
		form.ezine_email.focus();
		return false;
	}	
}
function checkUserForm(form) {
	var v = new Validate();
	if ((form.username) && (form.username.value == "")) {
		alert("Please enter an USERNAME.")	
		form.username.focus ()
		return false;
	}
	if (form.current_password) {
		if (form.current_password.value == "") {
			alert("Please enter your CURRENT PASSWORD.")	
			form.current_password.focus ()
			return false;			
		}
		else {
			if ((form.confirm_password) && (form.confirm_password.value == "")) {
				alert("Please CONFIRM your CURRENT PASSWORD.")	
				form.confirm_password.focus ()
				return false;			
			}
			else {
				if (form.current_password.value != form.confirm_password.value) {
					alert("Your CURRENT and CONFIRMATION PASSWORD do not match.")	
					form.confirm_password.focus ()
					return false;			
				}	
			}
		}
	}
	else {
		if ((form.password) && (form.password.value == "")) {
			alert("Please enter a PASSWORD.")	
			form.password.focus ()
			return false;
		}
	}	
	if ((form.gender) && (form.gender.options[form.gender.selectedIndex].value == '')) {
		alert("Please select a GENDER.");
		form.gender.focus();
		return false;
	}	
	if ((form.email) && (!v.isValidEmail(form.email.value))) {
		alert("Please enter a valid EMAIL ADDRESS.");
		form.email.focus();
		return false;
	}	
	if ((form.birthMonth) && (form.birthMonth.options[form.birthMonth.selectedIndex].value == '')) {
		alert("Please select a MONTH.");
		form.birthMonth.focus();
		return false;
	}		
	if ((form.birthDay) && (form.birthDay.options[form.birthDay.selectedIndex].value == '')) {
		alert("Please select a DAY.");
		form.birthDay.focus();
		return false;
	}			
	if ((form.birthYear) && (form.birthYear.options[form.birthYear.selectedIndex].value == '')) {
		alert("Please select a YEAR.");
		form.birthYear.focus();
		return false;
	}				
return true;	
}
// Form checkbox utilities
// Primarily used for Search Profiles Form
function uncheckAll(obj,field,sChkBox) {	
	var aCheckBoxes = document.forms[0].elements[field];
	if (aCheckBoxes.length!=-1) {
		 for (i = 0; i < aCheckBoxes.length; i++)
		     aCheckBoxes[i].checked = false ;
	}
    checkAnyState(obj,sChkBox);
}
function uncheckSingle(formname,field) {
    var aCheckBoxes = formname.elements[field];
	if(aCheckBoxes.length!=-1) {
	   for (i = 0; i < aCheckBoxes.length; i++){
	        if(aCheckBoxes[i].value != 50){
				aCheckBoxes[i].checked = false ;
			}
		}
    }
}
function checkAnyState(obj,sChkBox)	{
	var sInput = eval("document.forms[0]."+ sChkBox);
	if(obj.checked == true)
		sInput.checked = false;
}
		