/* ************************************************************
	The Conference Desk : www.theconferencedesk.com
	Developed by:	The MayaTech Corporation
					http://www.mayatech.com
					301-587-1600
	Filename:		account.cfm
	Description:	login page w/ info about becoming a member
	Revisions:		KJ 8/22/05 initial version
****************************************************************

/*variables to use with numeric/alphabetic functions below */
var numb = '0123456789';
var lwr = 'abcdefghijklmnopqrstuvwxyz';
var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

/*	this function gets called by the 5 validation functions below it*/
function isValid(parm,val) {
	if (parm == "") return true;
	for (i=0; i<parm.length; i++) {
		if (val.indexOf(parm.charAt(i),0) == -1){ 
			return false;
		}
	}
	return true;
}

/*	these 5 functions check for 
		-all numeric, 
		-all lowercase alpha, 
		-all uppercase alpha, 
		-all alpha, 
		-combo of all 3; 
	these all call the isValid function above;
	you must pass the actual value to the function*/

function isNum(parm) {
	if(!isValid(parm.value,numb)){
		alert("Value must be a whole number");
		parm.focus();
	}
}

function isLower(parm) {
	if(!isValid(parm.value,lwr)){
		alert("Value must be lowercase characters only");
		parm.focus();
	}
}

function isUpper(parm) {
	if(!isValid(parm,upr)){
		alert("Value must be uppercase characters only");
		parm.focus();
	}
}

function isAlpha(parm) {
	if(!isValid(parm,lwr+upr)){
		alert("Value must be alphabetic characters only");
		parm.focus();
	}
}

function isAlphanum(parm) {
	if(!isValid(parm,lwr+upr+numb)){
		alert("Value must be alphanumeric characters only");
	}
} 

function isAlphanumeric(box) {
   var unacceptedChars = /[\W_]/; // allow only letters and numbers

   if (unacceptedChars.test(box.value)) {
	//alert("Please use only alphanumerical characters for your Login/E-Mail or password.");
	alert("Please use only alphanumerical characters for your password.");
	box.select();
	return false
	}	
return true 
}

/*	check date type of entry;
	send the form field as parameter ;
	returns true/false to indicate if the value in form field is a valid date;
	pops an alert if invalid*/
function isDate(strFieldName){
	var objFormField = strFieldName;
	//strDate = objFormField.value;
	if(objFormField.value.length>0){
		var dateregex=/^[ ]*[0]?(\d{1,2})\/(\d{1,2})\/(\d{4,})[ ]*$/;
		var match=objFormField.value.match(dateregex);
		if (match){
			var tmpdate=new Date(match[3],parseInt(match[1],10)-1,match[2]);
			if (tmpdate.getDate()==parseInt(match[2],10) && tmpdate.getFullYear()==parseInt(match[3],10) && (tmpdate.getMonth()+1)==parseInt(match[1],10) && (tmpdate.getFullYear() > 1920)) { 
				return true; 
			}
		}
		alert("Enter a valid date. Example: 01/31/2003");
		objFormField.select();
		return false;
	}
	else {
		return true;
	}
}

/*	function to check email structure validity 
	pass form field as parameter*/
function ValidateEmail(frm)
{
    var strEmail = frm.value;

	var re = /^\w+[^\s@]*[^\.@][@]{1}\w+[^\s@]*\.{1}\w{2,5}$/; //Regular Expression for Email address  
	var towdot = /\.\./;

	if(strEmail != "")
	{
		if (!(re.test(strEmail))) {
			alert("Invalid Email Address!")
			frm.select();
		}
		else {
		  if (strEmail.search(towdot) != -1) {
			alert("Invalid Email Address!")
			frm.select();
		  }
		  else {
	        return true;
		  }
	  }
   }	
}


/*this date validation routine checks for valid date, format, and, optionally, dates prior to today
  returns: true for a valid date or false for an invalid date; 
  		   does NOT pop any alert
  usage: isvaliddate(datevalue, boolean indication of whether to check for prior dates)
         isvaliddate('1/1/1990',true) would return false because it is before today
		 isvaliddate('bb',false) would return false because 'bb' is not a valid date construct
		 isvaliddate('1/1/90',false) returns false because the date doesn't use a 4-digit year*/
function isvaliddate(dt,dofuture){
	//check validity of date fields
	var gooddate=true;
	if(dt.length>0){
    	var dateregex=/^[ ]*[0]?(\d{1,2})\/(\d{1,2})\/(\d{4,})[ ]*$/;
		var match=dt.match(dateregex);
		
		//if the format is bad return false
		if(!match){
			gooddate=false;
		//if the format is good, go ahead and make sure the actual numbers are okay
		}else{
			var tmpdate=new Date(match[3],parseInt(match[1],10)-1,match[2]);
			if (!(tmpdate.getDate()==parseInt(match[2],10) && tmpdate.getFullYear()==parseInt(match[3],10) && (tmpdate.getMonth()+1)==parseInt(match[1],10))) { 
				gooddate=false;
			}else{
				if(dofuture){
					//check that date is in the future
					var nowdate=new Date();
					//year can't be less than this year
					if(tmpdate.getFullYear()<nowdate.getFullYear()){
						gooddate=false;
					//if year is this year, month can't be less than this month
					}else{
						if(tmpdate.getFullYear()==nowdate.getFullYear()){
							if(tmpdate.getMonth()<nowdate.getMonth()){
								gooddate=false;
							//if year and month are this year and month, day can't be less than or equal to today
							}else{
								if(tmpdate.getMonth()==nowdate.getMonth()){
									if(tmpdate.getDate()<=nowdate.getDate()){
										gooddate=false;
									}
								}
							}
						}
					}
				}
			}
		}
	}
	return gooddate;
}

/* these 6 functions act exactly like their counterparts above;
	returns true/false depending on whether the parameter is valid;
	do NOT pop any alerts */
function isvalidnum(parm) {
	return isValid(parm.value,numb);
}

function isvalidalpha(parm) {
	return isValid(parm.value,lwr+upr);
}

function isvalidlower(parm) {
	return isValid(parm.value,lwr);
}

function isvalidupper(parm) {
	return isValid(parm.value,upr);
}

function isvalidalphanum(parm) {
	return isValid(parm,lwr+upr+numb);
} 

function isvalidemail(parm)
{
    var strEmail = parm.value;

	var re = /^\w+[^\s@]*[^\.@][@]{1}\w+[^\s@]*\.{1}\w{2,5}$/; //Regular Expression for Email address  
	var towdot = /\.\./;

	if(strEmail != ""){
		if (!(re.test(strEmail))){
			return false;
		}
		else {
			if (strEmail.search(towdot) != -1){
			  return false;
			}
			else{
	        	return true;
			}
		}
 	}
}

/* create the prototype trim function on the String object; 
   this allows you to trim javascript strings*/
String.prototype.trim = function() {
	// skip leading and trailing whitespace and return everything in between
	var x=this;
		x=x.replace(/^\s*(.*)/, '$1');
		x=x.replace(/(.*)\s*$/, '$1');
	return x;
}

/*checks password business rules: 
password cannot contain #`'" or space-band 
and must be 6-15 chars long*/
function checkpwd(box){  
	if (box.value.indexOf('#') != -1 ||
		box.value.indexOf('`') != -1 ||
		box.value.indexOf(" ") != -1 || 
		box.value.indexOf("'") != -1 || 
		box.value.indexOf('"') != -1 || 
		box.length() < 6 || box.length() > 15){
			return false
	}
	return true 
}
