//set field bg error color here
var errColor = "#FFE18F";
var validColor = "";

function NoneWithCheck(ss) {
	for(var k = 0; k < ss.length; k++) {
		if(ss[k].checked) {
			return false; 
		} //end if
		
	} //end for
	
	return true;

} //end NoneWithChecks function

/********************************************************
FUNCTION:		valPub

AUTHOR:			efeldman

DESCRIPTION: 	This function validates the internal add/edit awards

PARAMETERS: 	form		the form object

**********************************************************/
function valPub(form) {
	var msgArray = new Array();
	var msgIndex = 0;
	var showMsg = false;
	var emptyField = true;
	var radioCheck = true;
	var valid = true;
	m = 0;
	var total = 0;
	var brochures =true;
	var brochureValid = true;
	var negative = true;
	var ok = true;
	
	result = true;

	for(i=0; i<form.length-4; i++) {
	
	//Do not check hidden
		if(form.elements[i].type != 'hidden' ){//&& form.elements[i].type != 'submit' && form.elements[i].type != 'reset'
			
			//valid= to color current field and not submit (or not)
			valid = true;
		//check for a valid value in one of the publications
		
		//CHECK 4 BLANKS Validate, not for non-required fields		
			if(form.elements[i].type != 'radio' && form.elements[i].id.substring(0,6) != 'notReq' && form.elements[i].id.substring(0,3) != 'pub' && form.elements[i].type != 'textarea'){
				if(trimAll(form.elements[i].value) ==''){
					emptyField = false;
					valid=false;
				}
			}//end weed out not required fields

		//RADIO BUTTONS Validate		
			if(form.elements[i].type == 'radio'){
				objRadioCheck = document.getElementsByName(form.elements[i].name);
				if(NoneWithCheck(objRadioCheck))
				{ 
					valid=false;
					emptyField = false;
				}
				//call on getCheckedValue to get the value of the selected radio button
				radioValue = getCheckedValue(document.forms['form'].elements[i]);
				//if(radioValue == 'other' && trimAll(document.form.notReqotherInput.value) == ''){
					//valid = false;
					//form.notReqotherInput.style.backgroundColor = errColor;
					//msgArray[m++] = "Please input Other Reason for 'I am ordering for'.";
				//}
			}// end if radio
			
		//Not Null
			if(trimAll(form.elements[i].value) != ''){

			//ZIP CODE Validate
				if(form.elements[i].id == 'zip'){
					if (!validateZIP(form.elements[i])){
						valid=false;
						msgArray[m++] = "Zip Code is invalid. Must be 5 digits (#####) or 9 digits (#####-####).";
					}
				}//end zip

			} //end if !=blank
			
			//create label id of field Label	
			var label;
			
			//USE form[i].name FOR RADIO, NOT ID TO HIGHLIGHT THE SINGLE LABEL
			if(form.elements[i].type == 'radio') {
				label = "lbl" + form.elements[i].name;
			} else {
				label = "lbl" + form.elements[i].id;
			}
		
		//IF current loop field not Valid
			if (!valid && form.elements[i].type != 'radio'){
				form.elements[i].style.backgroundColor = errColor;
				document.getElementById(label).style.backgroundColor = errColor;
				result = false;
			}
			else{
				if(form.elements[i].type != 'radio' || form.elements[i].id == 'notReqQuestionComment'){
				form.elements[i].style.backgroundColor = validColor;
				document.getElementById(label).style.backgroundColor = validColor;
				}
			} //end if not valid
			
		} //end if !=hidden
	} //end for form loop i

	//checking for one value at least in brochures
	for(i=0; i<form.length-4; i++) {
			if(form.elements[i].id.substring(0,3) == 'pub' && form.elements[i].value != ''){
				if(isNaN(form.elements[i].value)){
					ok = false;
					form.elements[i].style.backgroundColor = errColor;
					document.getElementById("lbl"+form.elements[i].id).style.backgroundColor = errColor;
				}
				else{
				total = total + parseFloat(form.elements[i].value);
				}
			}
	}//ends total counting loop
	
	if(total <= 0 || total != /^[0-9]\d$/){//checking if total is invalid // 
		for(i=0; i<form.length-4; i++) {
			if(form.elements[i].id.substring(0,3) == 'pub') {
				form.elements[i].style.backgroundColor = errColor;
				document.getElementById("lbl"+form.elements[i].id).style.backgroundColor = errColor;
			}
			if (!valNumber(form.elements[i]) && isNaN(form.elements[i].value)){
				negative = false;
				form.elements[i].style.backgroundColor = errColor;
				document.getElementById("lbl"+form.elements[i].id).style.backgroundColor = errColor;
			}
			brochures = false;
		}
		ok = false;
	}//end checking if total is invalid}
	
	if(total > 0){// checking if total is valid
		for(i=0; i<form.length-4; i++) {
			if(form.elements[i].id.substring(0,3) == 'pub') {
				if (!valNumber(form.elements[i]) || isNaN(form.elements[i].value) || form.elements[i].value % 1 != 0){
					negative = false;
					form.elements[i].style.backgroundColor = errColor;
				}
				else{
					if(valNumber(form.elements[i])){
					form.elements[i].style.backgroundColor = validColor;
					document.getElementById("lbl"+form.elements[i].id).style.backgroundColor = validColor;
					ok = true;
					}
				}
			}
			
		}
		brochures = true;
	}// end checking if total is valid

	
	if(!emptyField){
		msgArray[m++] = "Please fill in all required fields.";
	}	
	if(!brochures){
		msgArray[m++] = "You must order at least 1 brochure or flier. (Alpha Characters are not allowed.)";
	}	
	
	if(!negative){
		msgArray[m++] = "You cannot order a negative or decimal value or use alpha characters.";
		ok = false;
	}	
	
	if(!ok){
		result = false;
	}
	
	if(radioValue == 'other' && trimAll(document.form.notReqotherInput.value) == ''){
		//verifying that there is a value in the other textbox if "other" is selected
			result = false;
			form.notReqotherInput.style.backgroundColor = errColor;
			lblnotReqotherInput.style.backgroundColor = errColor;
			msgArray[m++] = "Please input Other Reason for 'I am ordering for'.";
	}
	else{
		var newVal;
		if(radioValue != 'other' && document.form.notReqotherInput.value != ''){
				newVal ='';
				(document.form.notReqotherInput).value=newVal;
		}
		}//end get radio value
	
	if(msgArray.length > 0){
		showMsg = true;
		msgBuilder(msgArray, showMsg, "msg");	
	}
	else {
		//hide the messages.
		msgBuilder(msgArray, showMsg, "msg");
	}
	
	return result;
} //end validate function

/********************************************************

Function: valNumber

purpose: validate a zero or a positive number for the 
number of brochures

********************************************************/
function valNumber(field){
	var value = parseFloat(field.value);
	if((value) < 0){
		return false;
	}
	return true;
}
/********************************************************

Function: valOtherInput

********************************************************/
function valOtherInput(field){
	if(field.value == ''){
		return false;
	}
	return true;
}
/********************************************************

Function: getRadioValue

********************************************************/

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var x = 0; x < radioLength; x++) {
		if(radioObj[x].checked) {
			return radioObj[x].value;
		}
	}
	return "";
}




/********************************************************
FUNCTION:		msgBuilder

AUTHOR:			(adapted from) dherold

DESCRIPTION: 	This function builds a list of messages
				inside a div which name is passsed in.  
				Used when validating forms.

PARAMETERS: 	msgArray 	an array of messages.
				blnShow 	should the messages be displayed
							or hidden.
				msgDivName	name of the div to display.

**********************************************************/
function msgBuilder(msgArray, blnShow, divName)
{

	var objDiv = document.getElementById(divName);
	
	if(blnShow)
	{
		strInnerHTML = "<ol>";
		
		//place each message inside an html li tag
		for(i=0; i<msgArray.length; i++){
		
			strInnerHTML = strInnerHTML + "<li class='err'>" + msgArray[i] + "</li>";
		}
		
		strInnerHTML = strInnerHTML + "</ol>";
		
		objDiv.innerHTML = strInnerHTML;
		objDiv.style.visibility = 'visible';
	}
	else{
		objDiv.style.visibility = 'hidden';
	}
	
}

/********************************************************
FUNCTION:		trimAll

AUTHOR:			(adapted from) dherold

DESCRIPTION: 	This function strips whitespace from a value

PARAMETERS: 	strValue 	value to strip whitespace

**********************************************************/
function trimAll( strValue ) {
	var objRegExp = /^(\s*)$/;
	//check for all spaces
	if(objRegExp.test(strValue)){
	   strValue = strValue.replace(objRegExp, '');
	   if( strValue.length == 0)
		  return strValue;
	}

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
	   //remove leading and trailing whitespace characters
	   strValue = strValue.replace(objRegExp, '$2');
	}
  return strValue;
}
	
function padout(number) { 
	return (number < 10) ? '0' + number : number; 
}

/**************************************************************

Function : validateZip

**************************************************************/
function validateZIP(field) {
	var valid = "0123456789-";
	var hyphencount = 0;
	
	if (field.value.length!=5 && field.value.length!=10 && trimAll(field.value) != '') {
		//field.style.backgroundColor = errColor;
		return false;
	}
	
	for (var i=0; i < field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (temp == "-") {
			hyphencount++;
		}
		if (valid.indexOf(temp) == "-1") {
			//field.style.backgroundColor = errColor;
			return false;
					}
		if ((hyphencount > 1) || ((field.value.length==10) && ""+field.value.charAt(5)!="-")) {
			//field.style.backgroundColor = errColor;
			return false;
		}
	}
	if(field.value.match("-") != null){
		arrStr = field.value.split("-");
		if((arrStr[1] == 0000) || (arrStr[1] == 9999)){
			//field.style.backgroundColor = errColor;
			return false;
			
		}
	}
	return true;
}