// JavaScript Document
//form validation 
/*
NTIRA Corporation Copyright 2007
This is a form validation test. Any tag with key validation name will be checked. If one doesn't aquire then the submit button will not work.</p><br />
Each label group will be parsed. It will read the first node of each input to determine the validity
<ul>
<li>label name = validate_text</li>
<li>label name = validate_email</li>
<li>label name = validate_select</li>
<li>label name = validate_digit</li>
set submit button to id submitbtn
*/

function check_valid(){
		var valid = true;
		//document.getElementById('idname').getElementsByTagName('span')[0]
		// main id parent . child node [index] . property
		msg = '';
		
		var z = document.getElementsByTagName("label");
		for(i=0;i < z.length; i++){
		
			//////////////////////////////////////
			//	validate if text field is empty
			//////////////////////////////////////
			if(z[i].getAttribute("name") == "validate_text"){
			
				
				if(z[i].getElementsByTagName("input")[0].value == "" || z[i].getElementsByTagName("input")[0].value == null){
					
					notvalid_txt(z);
					valid = false;
					
				}else{
					isvalid_txt(z);
					
				}//end if
			}//end valid text
			
			//////////////////////////////////////
			//	validate if textarea is empty
			//////////////////////////////////////
			if(z[i].getAttribute("name") == "validate_textarea"){
			
				
				if(z[i].getElementsByTagName("textarea")[0].value == "" || z[i].getElementsByTagName("textarea")[0].value == null){
					
					notvalid_txtarea(z);
					valid = false;
					
				}else{
					isvalid_txtarea(z);
					
				}//end if
			}//end valid textarea
			
			//////////////////////////////////////
			//	validate digit
			//////////////////////////////////////
			if(z[i].getAttribute("name") == "validate_digit"){
			
					
				if(isDigit(z[i].getElementsByTagName("input")[0].value) ){
					isvalid_txt(z);
				}else{
					notvalid_txt(z);
					valid = false;
					
				}//end if
			}//end valid text
			
			
			
			//////////////////////////////////////
			//	validate select
			//////////////////////////////////////
			if(z[i].getAttribute("name") == "validate_select"){
				indexNumber = z[i].getElementsByTagName("select")[0].selectedIndex;
				
				if(z[i].getElementsByTagName("select")[0].options[indexNumber].text != "Select"){
	
					isvalid(z);
				}else{
					notvalid(z);
					valid = false;
				}
			}
			
			//////////////////////////////////////
			//	validate checkbox
			//  requires one at least to be checked withint the label field
			//////////////////////////////////////
			if(z[i].getAttribute("name") == "validate_checkbox"){
				checkboxValid = false;
				for(j = 0; j < z[i].getElementsByTagName("input").length; j++){
					if(z[i].getElementsByTagName("input")[j].checked == true)
					{
						checkboxValid = true;
					}
				}
				if(checkboxValid){
	
					isvalid(z);
				}else{
					notvalid(z);
					valid = false;
				}

				
			}
			
			//////////////////////////////////////
			//	validate email
			//////////////////////////////////////
			if(z[i].getAttribute("name") == "validate_email"){
				field = '';
				field = z[i].getElementsByTagName("input")[0].value;
				validEmail = false;
				temp = new Array();
				temp = field.split('@');
				if(temp.length > 1){
					temp = field.split('.');
					if(temp.length > 1){
						validEmail = true;
					}
				}
				
				if(validEmail){
					
					isvalid_txt(z);
					
				}else{
					
					notvalid_txt(z);
					valid = false;
					
				}//end if
			}//end valid email
			
		}//end for loop
			
		if(valid){
			document.getElementById("submitbtn").removeAttribute("disabled");
		}else{
			document.getElementById("submitbtn").setAttribute("disabled","false");
		}
		
		
}
function notvalid_txt(z){
	z[i].getElementsByTagName("input")[0].setAttribute("style","background-color:#FFB9C0");
	z[i].getElementsByTagName("span")[0].setAttribute("style","visibility:visible;");
	
					
	try{
		//ie
		z[i].getElementsByTagName("input")[0].style.setAttribute("cssText","background-color:#FFB9C0");
		z[i].getElementsByTagName("span")[0].style.setAttribute("visibility", "visible");
		//z[i].getElementsByTagName("span")[1].style.setAttribute("visibility", "hidden");
	}catch(e){}
}
function isvalid_txt(z){
	z[i].getElementsByTagName("input")[0].setAttribute("style","background-color:#FFFFFF");
	z[i].getElementsByTagName("span")[0].setAttribute("style","visibility:hidden;");
	//z[i].getElementsByTagName("span")[1].setAttribute("style","visibility:visible;");
					
	try{
		//ie
		z[i].getElementsByTagName("input")[0].style.setAttribute("cssText","background-color:#FFFFFF");
		z[i].getElementsByTagName("span")[0].style.setAttribute("visibility", "hidden");
		//z[i].getElementsByTagName("span")[1].style.setAttribute("visibility", "visible");
	}catch(e){}
}

//for text area styling
function notvalid_txtarea(z){
	z[i].getElementsByTagName("textarea")[0].setAttribute("style","background-color:#FFB9C0");
	z[i].getElementsByTagName("span")[0].setAttribute("style","visibility:visible;");
	
					
	try{
		//ie
		z[i].getElementsByTagName("textarea")[0].style.setAttribute("cssText","background-color:#FFB9C0");
		z[i].getElementsByTagName("span")[0].style.setAttribute("visibility", "visible");
		//z[i].getElementsByTagName("span")[1].style.setAttribute("visibility", "hidden");
	}catch(e){}
}
function isvalid_txtarea(z){
	z[i].getElementsByTagName("textarea")[0].setAttribute("style","background-color:#FFFFFF");
	z[i].getElementsByTagName("span")[0].setAttribute("style","visibility:hidden;");
	//z[i].getElementsByTagName("span")[1].setAttribute("style","visibility:visible;");
					
	try{
		//ie
		z[i].getElementsByTagName("textarea")[0].style.setAttribute("cssText","background-color:#FFFFFF");
		z[i].getElementsByTagName("span")[0].style.setAttribute("visibility", "hidden");
		//z[i].getElementsByTagName("span")[1].style.setAttribute("visibility", "visible");
	}catch(e){}
}

//hide span tag messages / show
function notvalid(z){
	
	z[i].getElementsByTagName("span")[0].setAttribute("style","visibility:visible;");
	
					
	try{
		//ie
		
		z[i].getElementsByTagName("span")[0].style.setAttribute("visibility", "visible");
		//z[i].getElementsByTagName("span")[1].style.setAttribute("visibility", "hidden");
	}catch(e){}
}
function isvalid(z){
	
	z[i].getElementsByTagName("span")[0].setAttribute("style","visibility:hidden;");
	//z[i].getElementsByTagName("span")[1].setAttribute("style","visibility:visible;");
					
	try{
		//ie
		
		z[i].getElementsByTagName("span")[0].style.setAttribute("visibility", "hidden");
		//z[i].getElementsByTagName("span")[1].style.setAttribute("visibility", "visible");
	}catch(e){}
}

function isDigit ( val )
{
  var strBuffer = new String(val);
  var nPos = 0;

  if ( strBuffer.length == 0 || strBuffer == null)
    return false;

  for (nPos = 0; nPos < strBuffer.length; nPos++)
    if (strBuffer.charAt (nPos) < '0' || strBuffer.charAt(nPos) > '9')
      return false;

  return true;
}