// JavaScript Document
/*
	Taufeeq ur Rehman
	PHP,MySQL,Javascript
	
	File Description: This file is a utility to validate the form fields
			This function automatically validates the password and emails
	Usage: just call validateForm function and it will return you ture or fasle depending of validation
	i.e:
		validateForm("first_name,last_name,email","error",6,regForm);
		where first arg is the fields to validate (if you have email box then name it to "email")
		Second is the css class which you want to apply on error message
		Third one is the password length to validate if password is less then provided length it will generate error
		Fourth one is the form name just put your form name here
		
	Reconmendation:
		Please use field name with under score and please use full name as the error message is got from the name
		for example for first name you have to use first_name as field name so that the error will display as
		"Please Enter First name"
	
	This code is open to modify and customize and free to use for personal or commercial usage
*/
function validateForm(ctrls,errclass,passlen,frm)
{
	var firstCtrl = null;
	var retVal = true;
	var glue = "Enter ";
	var br = "<br>";
	var tc ;
	var ctrlArray = ctrls.split(",");
	for(ctrl=0; ctrl<ctrlArray.length; ctrl++)
	{
		
		c = document.forms[frm].elements[ctrlArray[ctrl]];
		//if(!getParentForm(c,dv)) continue;
		s = ctrlArray[ctrl]+"_div";
		
		if(c.type == 'select-one' || c.type == 'file'){
			
			glue = "Select ";
			if(ctrlArray[ctrl] == "dd" ||ctrlArray[ctrl] == "mm" ||ctrlArray[ctrl] == "yy"){
				
				ctrlName = " Date of Birth";
			}else{
				ctrlName = ctrlArray[ctrl];
			}
		}else{
			glue = "Enter ";
			ctrlName = ctrlArray[ctrl];
		}
		
		prnt = c.parentNode;
		elm = document.getElementById("div_"+c.id);
		if(elm && elm != "undefined"){
			try{
				prnt.removeChild(elm);
			}catch(e){}
		}
		elm = document.createElement("div");
		elm.id = "div_"+c.id;
		
		
		prnt.insertBefore(elm,c);
		
		elm.className = "";
		elm.innerHTML = "";
		if(c.value.trim() == ""){
			if(!firstCtrl){
				firstCtrl = c;
			}
//			alert(ctrlArray[ctrl]);
			retVal = false;
			elm.className = errclass;
			ctrlName = ctrlName.replace("_"," ");
			char = ctrlName.charAt(0).toUpperCase();
			elm.innerHTML = glue+char+ctrlName.substr(1,ctrlName.length);
			
		}
		
	}
	if(!retVal){
		firstCtrl.focus();
		return false;
	}
	if(document.forms[frm].elements["email"] && document.forms[frm].elements["email"] != "undefined"){
			c = document.forms[frm].elements["email"];
			prnt = c.parentNode;
			elm = document.getElementById("div_"+c.id);
			if(elm && elm != "undefined"){
				try{
					prnt.removeChild(elm);
				}catch(e){}
			}
			elm = document.createElement("div");
			elm.id = "div_"+c.id;
			elm.innerHTML = "";
			elm.ClassName = null;
			prnt.insertBefore(elm,c);
		
		if(!validateEmail(document.forms[frm].elements["email"].value)){
			elm.className = errclass;
			elm.innerHTML = "Invalid Email";
			retVal = false;
			firstCtrl = c;
		}
	}
	if(!retVal){
		firstCtrl.focus();
		return false;
	}
	
	if(passlen == 0) return retVal;
	passArray = getElementsByContainer('password',frm);
	/*for(i=0; i<passArray.length; i++){
		alert(passArray[i].id);
	}*/
	if(passArray != ""){
	//alert(cntrl + " >> "+document.getElementById(cntrl).src);
		if(passArray[1]){
			c = passArray[0];
			prnt = c.parentNode;
			elm = document.getElementById("div_"+c.id);
			if(elm && elm != "undefined"){
				try{
					prnt.removeChild(elm);
				}catch(e){}
			}
			elm = document.createElement("div");
			elm.id = "div_"+c.id;
			elm.innerHTML = "";
			elm.ClassName = null;
			prnt.insertBefore(elm,c);
			if(passArray[0].value.length < passlen){
				elm.className = errclass;
				elm.innerHTML = "Password should be minimum "+passlen+" characters";
				retVal = false;
				firstCtrl = c;
			}
			
			if(retVal){
				elm.className = null;
				elm.innerHTML = "";
				if((passArray[0].value.trim() != "") && passArray[0].value != passArray[1].value){
					passArray[0].value = "";
					passArray[1].value = "";
					elm.className = errclass;
					elm.innerHTML = "Password and confirm password dosen't match"
					retVal = false;
					firstCtrl = c;
				}
			}
		}
	}
	if(!retVal){
		firstCtrl.focus();
	}
//	return false;
	return retVal;
}
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
function validateEmail(val){
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(val)))
	return false;
	return true;
}
function getElementsByContainer(ctype,d){
	j = 0;
	p = Array();
	var dv = document.getElementById(d);
	if(!dv){ alert(d+" not found");
		return false;
	}
	var ctrlsArray = dv.getElementsByTagName("input");
	
	for (i=0; i<ctrlsArray.length; i++){
		if(ctrlsArray[i].type == ctype){
			p[j++] = ctrlsArray[i];
		}
	}
	return p;
}