// JavaScript Document
function validateInput(input,errorElement,msg){
	if(input){
		if(input.value.length <= 0 || input.value == null){
			input.style.border = '#C00 solid 3px';
			input.focus();
			errorElement.innerHTML = msg;
			errorElement.style.display = 'block';
			return false;			
		}
	}
	
	return true;
}


function validateCheck(checkbox,errorElement,msg){
	if(checkbox){
		if(checkbox.checked==false){
			checkbox.style.border = '#C00 solid 3px';
			checkbox.focus();
			errorElement.innerHTML = msg;
			errorElement.style.display = 'block';
			return false;
		
		}
	}
	
	return true;
	
}


function validateCheckGroup(check,errorElement,msg){
	
	if(check){
		
		//loop through radio/checkbox group and if an element is checked then return true
		for(i=0;i<check.length;i++){
			if(check[i].checked){
				return true;
			}		
		}
	
		check[0].focus();
		errorElement.innerHTML = msg;
		errorElement.style.display = 'block';
		return false;
	}
	
	return true;
	
}



//Use this function when you want to require a field...but it should only be required if a different element is checked
function validateCheckDependent(check,dependent,errorElement,msg,checked){
	//alert(checked);
	if(check && dependent){
		if(checked==true){
			if(check.checked && dependent.value.length <= 0){	
				dependent.style.border = '#C00 solid 3px';
				dependent.focus();
				errorElement.innerHTML = msg;
				errorElement.style.display = 'block';
				return false;
			}
		}
		else if(checked == false){
			if(check.checked == false && dependent.value.length <= 0){	
				dependent.style.border = '#C00 solid 3px';
				dependent.focus();
				errorElement.innerHTML = msg;
				errorElement.style.display = 'block';
				return false;
			}
		}
	}
	
	return true;
		
		
}

function validateDate(date,errorElement,msg){
	
	if(date){
		
		/* 
		
		Valid Date Formats
		---------------------------
		NOTE: formats are acceptable with a slash(/) or dash(-)
		> mm/dd/yyyy
		> mm/dd/yy
		> mm/d/yyyy
		> mm/d/yy
		> m/dd/yyyy
		> m/dd/yy
		> m/d/yyyy
		> m/d/yy
		
		*/
		
		if(date.value.match(/^((0?[1-9])|(1[0-2]))[-\/](([0-2]?[0-9])|(3[0-1]))[-\/](([0-9]{2})|([0-9]{4}))$/)){
			return true;			  
		}
		else{
			date.style.border = '#C00 solid 3px';
			date.focus();
			errorElement.innerHTML = msg;
			errorElement.style.display = 'block';
			return false;
		
		}
	}	
	
	
}


function validatePhone(phone,errorElement,msg){
	
	if(phone){
		
		/*
		
		Valid Phone Number Formats
		---------------------------------
		NOTE: numbers other 5 are acceptable, I just used 5 for an example number
		> 555-555-5555
		> 5555555555
		> 555 - 555 - 5555
		> 555 - 555- 5555
		> 555 - 555 -5555
		> 555 -555 - 5555
		> 555 -555 -5555
		> 555 -555- 5555
		> 555- 555 - 5555
		> 555- 555- 5555
		> 555- 555 -5555
		
		*/
		
		if(phone.value.match(/^[0-9]{10}$|[0-9]{3}\s?-\s?[0-9]{3}\s?-\s?[0-9]{4}$/)){
			return true;	
		}
		else{
			phone.style.border = '#C00 solid 3px';
			phone.focus();
			errorElement.innerHTML = msg;
			errorElement.style.display = 'block';
			return false;			
		}		
	}	
}


function validateZip(postal,errorElement,msg){
	
	if(postal){
		
		/*
		
		Valid Zip Code Formats
		-------------------------------
		NOTE: Numbers other than 5 are acceptable I just used 5 for an example
		> 55555
		> 55555-5555
		> 55555 -5555
		> 55555- 5555
		> 55555 - 5555
		> 55555 5555
		
		*/
		
		if(postal.value.match(/^[0-9]{5}$|[0-9]{5}(\s)?-(\s)?[0-9]{4}$/)){
			return true;
		}
		else{
			postal.style.border = '#C00 solid 3px';
			postal.focus();
			errorElement.innerHTML = msg;
			errorElement.style.display = 'block';
			return false;
		}
	}
}


function validateSocial(social,errorElement,msg){
	
	if(social){
		
		/*
		
		Valid Social Security Number Formats
		--------------------------------------
		NOTE: Numbers other than 5 are acceptable, I justed used 5 for an example number
		> 555-55-5555
		> 555555555
		> 55555-5555
		> 555-555555
		> 555 - 55 - 5555
		> 555 - 55- 5555
		> 555 - 55 -5555
		> 555 -55 - 5555
		> 555 -55 -5555
		> 555 -55- 5555
		> 555- 55 - 5555
		> 555- 55- 5555
		> 555- 55 -5555
		
		*/
		
		if(social.value.match(/^[0-9]{9}$|[0-9]{3}\s?-\s?[0-9]{2}\s?-\s?[0-9]{4}$/)){
			return true;	
		}
		else{
			social.style.border = '#C00 solid 3px';
			social.focus();
			errorElement.innerHTML = msg;
			errorElement.style.display = 'block';
			return false;			
		}		
	}	
}