/*View/hide forms*/
/*function toggleLayer(whichLayer)
{
	if (document.getElementById)
	{
		// Current Web Standards
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "":"block";
	}
	else if (document.all)
	{
		// This is for old ID versions
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
	else if (document.layers)
	{
		// this is for Netscape 4
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
}*/

/*****
 * View/hide forms
 *****/
 //field == the checkbox that determines the outcome
 //whichLayer == the ID to be shown/hidden by this function
function toggleHidden(field, whichLayer){
	var theCheckBox = document.getElementById(field);
	var layerStyle = document.getElementById(whichLayer).style;
	var theRadio = document.getElementsByName(field);
	if(theRadio.length > 1){	//if it's a radio
		for(var r = 0; r < theRadio.length; r++){	// Cycle through the radio buttons to find the value
			if(theRadio[0].checked == true){
				layerStyle.display = "block";
			} else if(theRadio[0].checked == false){
				layerStyle.display = "none";
			}
			//alert(theRadio.length);
		}
	} else if (theRadio.length == undefined){	//if it's a checkbox, not a radio
		if(theCheckBox.checked == true){// && (theCheckBox.value == "Yes" || theCheckBox.value == "yes" || theCheckBox.value == "YES")){
			layerStyle.display = "block";
		} else{
			layerStyle.display = "none";
		}
	}
}

/*****
 * Switch field descriptions
 *****/
 //id == the ID to be displayed by this function
 //display == how to show the display CSS property (e.g. "none", "block")
 function displayDescription(id, display){
 	var displayMe = document.getElementById(id).style;
	displayMe.display = display;
 }

/*****
 * Change submit button to continue
 *****/
 //id == the ID to affect this function
 //toValue == the string that the button changes to
function submitToContinue(id, toValue){
	var theCheckBox = document.getElementById(id);
	var submitBtn = document.getElementById("submit");
	if(theCheckBox.checked == true){
		submitBtn.value = "Continue...";
	} else {
		submitBtn.value = toValue;
	}
}

/*Disable the submit button until all required fields are filled out*/
/*This function has not yet been written*/

//Disable the submit button upon submission
//id == the id of the button to disable
function disableSubmit(id){
	if(id == ""){
		var submitBtn = document.getElementById("submit");
	} else {
		var submitBtn = document.getElementById(id);
	}
	submitBtn.value = "One moment...";
	submitBtn.disabled = true;
}

function disableConfirm(confirmBtn){
	var submitBtn = document.getElementById(confirmBtn);
	var confirmBox = document.getElementById("confirmDetails");
	if(confirmBox.checked == true) submitBtn.disabled = false;
	else submitBtn.disabled = true;
}

function enableSubmit(id){
	if(id == ""){
		var submitBtn = document.getElementById("submit");
	} else {
		var submitBtn = document.getElementById(id);
	}
	submitBtn.disabled = false;
}

/*****
 * Make sure numbers are entered as numbers
 * (e.g. "# of Unicr0ns: 1" is better than  "# of Unicr0ns: meh"
 *****/
 //id == the field being affected by this function
 //value == the data in the form field
 //type == the type of data being sent
function validateType(id, value, type){
	var theFieldStyle = document.getElementById(id).style;
	//integer or optional integer (can be left blank)
	if(type == "number" || type == "opNumber"){
		//alert(parseInt(value));
		if(value == parseInt(value) || value == ""){
			theFieldStyle.background = "#fff";
			return(false);
		} else {
			theFieldStyle.background = "#ff3";
			return(true);
		}
	}
	//decimal number
	else if(type == "float"){
		if(value == parseFloat(value)){
			theFieldStyle.background = "#fff";
			return(false);
		} else{
			return(true);
		}
	}
	//required field
	else if(type == "requiredText"){
		if(value == ""){
			theFieldStyle.background = "#ff3";
		} else {
			theFieldStyle.background = "#fff";
		}
	}
}

/*****
 * Make sure numbers are within a specified limit
 * (e.g. within 0 and 5, or greater than 0)
 *****/
 //id == the field being affected by this function
 //value == the data in the form field
 //bottomLimit == the point at which small numbers will no longer be accepted
 //topLimit == the point at which large numbers will no longer be accepted
function validateLimit(id, value, type, bottomLimit, topLimit){
	var theFieldStyle = document.getElementById(id).style;
	var bottomError = false;
	var topError = false;
	//integer or optional integer (can be left blank)
	if(type == "number" || type == "opNumber" || type == "float"){
		//Check if the value is greater than the bottom limit
		if(bottomLimit !== ""){
			if(value > bottomLimit){
				theFieldStyle.background = "#fff";
				bottomError = false;
			} else{
				theFieldStyle.background = "#ff3";
				bottomError = true;
			}
		}
		//Check if the value is less than the top limit
		if(topLimit !== ""){
			if(value < topLimit){
				theFieldStyle.background = "#fff";
				topError = false;
			} else{
				theFieldStyle.background = "#ff3";
				topError = true;
			}
		}
		if(bottomError == true || topError == true){
			return(true);
		} else{
			return(false);
		}
	}
}

/*****
 * Validate Field
 * Performs the validateType and validateLimit functions
 * to avoid conflicting color changes.
 *****/
 //id == the field being affected by this function
 //value == the data in the form field
 //bottomLimit == the point at which small numbers will no longer be accepted
 //topLimit == the point at which large numbers will no longer be accepted
function validateField(id, value, type, bottomLimit, topLimit){
	var theFieldStyle = document.getElementById(id).style;
	//integer, optional integer (can be left blank), or floating (decimal allowed)
	if(type == "number" || type == "opNumber" || type == "float"){
		//perform two functions
		var typeError =	validateType(id, value, type);
		var limitError = validateLimit(id, value, type, bottomLimit, topLimit);
		//if either function comes back error, change the field color
		if(typeError == true || limitError == true){
			theFieldStyle.background = "#ff3";
		} else{
			theFieldStyle.background = "#fff";
		}
		if(type == "opNumber" && value == ''){
			theFieldStyle.background = "#fff";
		}
	}
}

/*****
 * Validate Textarea
 * 
 * Puts a limit on the number of characters that can be entered in a textarea
 *****/
  //id == the field being affected by this function
  //bottomLimit == the minimum # of characters required for the field
  //topLimit == the maximum # of characters allowed in the field
 function validateTextarea(id, bottomLimit, topLimit){
 	var textarea = document.getElementById(id);
	var theFieldStyle = textarea.style;
	if(textarea.value.length < bottomLimit || textarea.value.length > topLimit){
		theFieldStyle.background = "#ff3";
	} /*else if(textarea.value.length > topLimit){
		textarea.value = textarea.value.substring(0,topLimit);
	}*/ else{
		theFieldStyle.background = "#fff";
	}
 }

/*****
* Validate Strong Password
*
* Makes sure a password is at least six characters long and has a number in it.
*****/
 //password == the password entered by the user
function validatePassword(id, password){
	var n = "";
	var theFieldStyle = document.getElementById(id).style;
	var confirmed = false;
	//Check password length
	if(password.length < 1){
		theFieldStyle.background = "#ff3";
	} else {
		for(i=0;i<password.length;i++){
			n = password[i].match(/\d+/,password[i]);
			if(n != null){
				//theFieldStyle.background = "#fff";
				var confirmed = true;
			}
		}
		if(confirmed == true){
			theFieldStyle.background = "#fff";
		} else {
			theFieldStyle.background = "#ff3";
		}
	}
}


 
 
/*****
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 *****/
// Declaring valid date character, minimum year and maximum year
/*var dtCh= "/";
var currentDate=new Date();
var minYear = currentDate.getFullYear();
var currentMonth = currentDate.getMonth()+1;
var currentDay = currentDate.getDate();
var maxYear=2100;*/

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var dtCh = ""
	
	if(dtStr.indexOf("/") > -1){
		dtCh = "/";
	} else if(dtStr.indexOf("-") > -1){
		dtCh = "-";
	} else if(dtStr.indexOf(".") > -1){
		dtCh = ".";
	}
	//alert(dtCh);
	var currentDate=new Date();
	var minYear = currentDate.getFullYear();
	var currentMonth = currentDate.getMonth()+1;
	var currentDay = currentDate.getDate();
	var maxYear=2100;
	
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	//if a 2-digit date was entered, convert it to 4
	if(strYr.length==2){
		//strYr = parseInt(strYr)+2000;
		strYr = "20" + strYr;
		strYear = strYr;
	}
	//strYr.toString();
	//for the first nine months or days (01-09) look at the second character (1-9)
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false;
	}
	/*if (strYear.length != 2 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 2 digit year between "+minYear+" and "+maxYear)
		return false
	}*/
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false;
	}
	//If the date is earlier than today's date, throw an error.
	/*if (year < minYear || (year == minYear && month < currentMonth) || (year == minYear && month == currentMonth && day <= currentDay)){
		return false;
	}*/
return true
}

function validateDate(theDate, id){
	//var dt=document.frmSample.txtDate
	var theField = document.getElementById(id).style;
	if (isDate(theDate)==false){
		//document.getElementById(theDate).focus()
		theField.background = "#ff3";
		return false;
	}
	theField.background = "#fff";
    return true
 }
 
function count(targetField, counterField, type){
	if(type == "word"){
		/*var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
		var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
		var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
		var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
		var splitString = cleanedStr.split(" ");
		var word_count = splitString.length -1;*/
		//var target = document.application[targetField].value;
		var target = document.getElementById(targetField).value;
		var wordCount = target.split(" ");
		document.getElementById(counterField).value = wordCount.length;
	}
}
