/* ************************************************************
Standard cliend side data validation and interface manipulation
function

Developer:  Philip Schmitz
Date Updated:  1/3/2009
************************************************************ */



/* ************************************************************
this function Will take a value and determine if the 
element is emptry
************************************************************ */
function isValueEmpty(value){
	if(value==null||value==""||value==" "||value=="  "||value=="   "||value=="    "||value=="     ")
		return 1;
	else
		return 0;
}

/* ************************************************************
this function Will take a form field name and determine if the 
element is emptry
************************************************************ */
function isEmpty(element){
	if(element.value==null||element.value==""||element.value==" "||element.value=="  "||element.value=="   "||element.value=="    "||element.value=="     ")
		return 1;
	else
		return 0;	
}

/* ************************************************************
this function will take a value and determine if it is a 
POSITIVE Integer (NO DECIMALS)
************************************************************ */
function isPosInt(element){
	elementString=element.toString();
	for(var i=0;i<elementString.length;i++){
		var elementChar=elementString.charAt(i);
		if(elementChar<"0" || elementChar>"9"){
			return 0;
		}
	}
	return 1;
}

/* ************************************************************
this function will take an a value and determine if it is a
valid zipcode format

12345
12345-1234

NOTE:  does not check for length reqquirements
************************************************************ */
function isZipCode(element){
	elementString=element.toString();
	for(var i=0;i<elementString.length;i++){
		var elementChar=elementString.charAt(i);
		if(!(elementChar == "-" || elementChar == " ") && elementChar<"0" || elementChar>"9"){
			return 0;
		}
	}
	return 1;
}

/* ************************************************************
this function will take a value and determine if its is a 
POSITIVE number.  It allows for decimals.
************************************************************ */
function isPosNumCh(element){
	if(isNaN(element)){
		return 0;
	}
	else{
		if(element <0){
			return 0;
		}
	}
	return 1;
}

/* ************************************************************
this function will take a string value and determine if it is
a POSITIIVE number.  it allows for decimals
************************************************************ */
function isPosNum(element){
	if(isNaN(element)){
		return 0;
	}
	else{
		elementString=element.toString();
		if (elementString.indexOf('.') == -1) {
			for(var i=0;i<elementString.length;i++){
				var elementChar=elementString.charAt(i);
				if(elementChar<"0" || elementChar>"9"){
					return 0;
				}
			}
		}
		else{
			elementString=elementString.substring(0,elementString.indexOf('.')-1)
			for(var i=0;i<elementString.length;i++){
				var elementChar=elementString.charAt(i);
				if(elementChar<"0" || elementChar>"9"){
					return 0;
				}
			}
		}
	}
	return 1;
}

/* ************************************************************
this function will take an array of fields and will determine
if each field passed in the array has a value in it and will
generate a javascript error for each field that is not populated

This function also auto submits form if all fields are filled
out which differentiates it from isAllFilled
************************************************************ */
function checkPreSubmit(fields,formObj){
	var carryOn = 1;
	if(isArray(fields)){
		for(var i = 0;i<fields.length;i++){
			if(isEmpty(fields[i][0])){
				alert("Please enter a value for "+fields[i][1]+".");
				carryOn = 0;
			}
		}
	}
	if(carryOn){
		formObj.submit();
	}
}

/* ************************************************************
this function will take an array of fields and will determine
if each field passed in the array has a value in it and will
generate a javascript error for each field that is not populated
************************************************************ */
function isAllFilled(fields){
	var carryOn = 1;
	if(isArray(fields)){
		for(var i = 0;i<fields.length;i++){
			if(isEmpty(fields[i][0])){
				alert("Please enter a value for "+fields[i][1]+".");
				carryOn = 0;
			}
		}
	}
	if(carryOn){
		return 1;
	}
} 

/* ************************************************************
Password Strength Factors and Weightings

password length:
level 0 (3 point): less than 4 characters
level 1 (6 points): between 5 and 7 characters
level 2 (12 points): between 8 and 15 characters
level 3 (18 points): 16 or more characters

letters:
level 0 (0 points): no letters
level 1 (5 points): all letters are lower case
level 2 (7 points): letters are mixed case

numbers:
level 0 (0 points): no numbers exist
level 1 (5 points): one number exists
level 1 (7 points): 3 or more numbers exists

special characters:
level 0 (0 points): no special characters
level 1 (5 points): one special character exists
level 2 (10 points): more than one special character exists

combinatons:
level 0 (1 points): letters and numbers exist
level 1 (1 points): mixed case letters
level 1 (2 points): letters, numbers and special characters 
					exist
level 1 (2 points): mixed case letters, numbers and special 
					characters exist

************************************************************ */
function testPassword(passwd)
{
		var intScore   = 0
		var strVerdict = "weak"
		var strLog     = ""
		
		// PASSWORD LENGTH
		if (passwd.length<5)                         // length 4 or less
		{
			intScore = (intScore+3)
			strLog   = strLog + "3 points for length (" + passwd.length + ")\n"
		}
		else if (passwd.length>4 && passwd.length<8) // length between 5 and 7
		{
			intScore = (intScore+6)
			strLog   = strLog + "6 points for length (" + passwd.length + ")\n"
		}
		else if (passwd.length>7 && passwd.length<16)// length between 8 and 15
		{
			intScore = (intScore+12)
			strLog   = strLog + "12 points for length (" + passwd.length + ")\n"
		}
		else if (passwd.length>15)                    // length 16 or more
		{
			intScore = (intScore+18)
			strLog   = strLog + "18 point for length (" + passwd.length + ")\n"
		}
		
		
		// LETTERS (Not exactly implemented as dictacted above because of my limited understanding of Regex)
		if (passwd.match(/[a-z]/))                              // [verified] at least one lower case letter
		{
			intScore = (intScore+1)
			strLog   = strLog + "1 point for at least one lower case char\n"
		}
		
		if (passwd.match(/[A-Z]/))                              // [verified] at least one upper case letter
		{
			intScore = (intScore+5)
			strLog   = strLog + "5 points for at least one upper case char\n"
		}
		
		// NUMBERS
		if (passwd.match(/\d+/))                                 // [verified] at least one number
		{
			intScore = (intScore+5)
			strLog   = strLog + "5 points for at least one number\n"
		}
		
		if (passwd.match(/(.*[0-9].*[0-9].*[0-9])/))             // [verified] at least three numbers
		{
			intScore = (intScore+5)
			strLog   = strLog + "5 points for at least three numbers\n"
		}
		
		
		// SPECIAL CHAR
		if (passwd.match(/.[!,@,#,$,%,^,&,*,?,_,~]/))            // [verified] at least one special character
		{
			intScore = (intScore+5)
			strLog   = strLog + "5 points for at least one special char\n"
		}
		
									 // [verified] at least two special characters
		if (passwd.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/))
		{
			intScore = (intScore+5)
			strLog   = strLog + "5 points for at least two special chars\n"
		}
	
		
		// COMBOS
		if (passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))        // [verified] both upper and lower case
		{
			intScore = (intScore+2)
			strLog   = strLog + "2 combo points for upper and lower letters\n"
		}

		if (passwd.match(/([a-zA-Z])/) && passwd.match(/([0-9])/)) // [verified] both letters and numbers
		{
			intScore = (intScore+2)
			strLog   = strLog + "2 combo points for letters and numbers\n"
		}
 
									// [verified] letters, numbers, and special characters
		if (passwd.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/))
		{
			intScore = (intScore+2)
			strLog   = strLog + "2 combo points for letters, numbers and special chars\n"
		}
	
	
		if(intScore < 16)
		{
		   strVerdict = "very weak"
		}
		else if (intScore > 15 && intScore < 25)
		{
		   strVerdict = "weak"
		}
		else if (intScore > 24 && intScore < 35)
		{
		   strVerdict = "mediocre"
		}
		else if (intScore > 34 && intScore < 45)
		{
		   strVerdict = "strong"
		}
		else
		{
		   strVerdict = "stronger"
		}
	
	//document.forms.passwordForm.score.value = (intScore)
	//document.forms.passwordForm.verdict.value = (strVerdict)
	//document.forms.passwordForm.matchlog.value = (strLog)
	
	return intScore;
}

/* ************************************************************
showPasswordStrenght Function shows visual queue to user
using color coded and numeric strenght values
************************************************************ */
function showPWStrength(passValue){
	//alert(testPassword(passValue));
	if(testPassword(passValue) < 25){
	//weak
		document.getElementById('idSMT0').style.display='none';
		document.getElementById('idSMT1').style.display='block';
		document.getElementById('idSMT1').style.color='#000';
		document.getElementById('idSMT2').style.display='none';
		document.getElementById('idSMT3').style.display='none';
		document.getElementById('idSMT4').style.display='none';
		document.getElementById('idSM1').bgColor='#FF0000';
		document.getElementById('idSM2').bgColor='#FF0000';
		document.getElementById('idSM3').bgColor='#FF0000';
		document.getElementById('idSM4').bgColor='#FF0000';
	}
	if(testPassword(passValue) > 24 && testPassword(passValue) < 35){
	//medium
		document.getElementById('idSMT0').style.display='none';
		document.getElementById('idSMT1').style.display='none';
		document.getElementById('idSMT2').style.display='block';
		document.getElementById('idSMT2').style.color='#000';
		document.getElementById('idSMT3').style.display='none';
		document.getElementById('idSMT4').style.display='none';
		document.getElementById('idSM1').bgColor='#FFCC00';
		document.getElementById('idSM2').bgColor='#FFCC00';
		document.getElementById('idSM3').bgColor='#FFCC00';
		document.getElementById('idSM4').bgColor='#FFCC00';
	}
	if(testPassword(passValue) > 34 && testPassword(passValue) < 45){
	//strong
		document.getElementById('idSMT0').style.display='none';
		document.getElementById('idSMT1').style.display='none';
		document.getElementById('idSMT2').style.display='none';
		document.getElementById('idSMT3').style.display='block';
		document.getElementById('idSMT3').style.color='#000';
		document.getElementById('idSMT4').style.display='none';
		document.getElementById('idSM1').bgColor='#00FF00';
		document.getElementById('idSM2').bgColor='#00FF00';
		document.getElementById('idSM3').bgColor='#00FF00';
		document.getElementById('idSM4').bgColor='#00FF00';
	}
	if(testPassword(passValue) > 44){
	//best
		document.getElementById('idSMT0').style.display='none';
		document.getElementById('idSMT1').style.display='none';
		document.getElementById('idSMT2').style.display='none';
		document.getElementById('idSMT3').style.display='none';
		document.getElementById('idSMT4').style.display='block';
		document.getElementById('idSMT4').style.color='#000';
		document.getElementById('idSM1').bgColor='#00FF00';
		document.getElementById('idSM2').bgColor='#00FF00';
		document.getElementById('idSM3').bgColor='#00FF00';
		document.getElementById('idSM4').bgColor='#00FF00';
	}
}

/* ************************************************************
this function is used to determine if a value (year,month,day)
is a valid date according to the calendar year
************************************************************ */
function isDate(year,month,day){
	// month argument must be in the range 1 - 12
	month = month - 1; // javascript month range : 0- 11
	//var tempDate = new Date(year,month,day);
	var tempDate = new Date();
	tempDate.setFullYear(year,month,day);
	//alert(tempDate);
	//alert(tempDate.getYear());
	//year-1900 is work arround for firefox which displays years after 2000 as 100 (eg 2008 is 108)
	if((year == tempDate.getYear()||(year-1900) == tempDate.getYear()) && (month == tempDate.getMonth()) && (day == tempDate.getDate())){
		return true;
	}
	else{
		return false;
	}
}

/* ************************************************************
this function returns true if the typeof function is not
equal to undefined and fale if it is undefined
************************************************************ */
function defined(o){
	return typeof(o) != "undefined";
}

/* ************************************************************
this function is used to determine if an object is an array
this is particularly useful for checking if cleint side
collections (eg sets of checkboxes) are a collection or
a single instance
************************************************************ */
function isArray(o){

	// If these conditions aren't met, it certainly isn't an Array
	if(o == null || typeof(o) != "object" || typeof(o.length)!= "number"){
		return false;
	}
	
	// Check to see if the object is an instance of the window's Array object
	if(defined(Array) && defined(o.constructor) && o.constructor==Array){
		return true;
	}
	
	// It might be an array defined from another window object - check to see if it has an Array's methods
	if(typeof(o.join) == "function" && typeof(o.sort) == "function" && typeof(o.reverse) == "function"){
		return true;
	}
	
	// As a last resort, let's see if index [0] is defined
	return (o.length==0 || defined(o[0]));
	
}