// JavaScript Document
//Changes done by Sapna
//Updated Bhadra's Regular Expression for name validation with /^([A-Za-z]+\'?[A-Za-z]*)[\d]{0,10}$/

var W3CDOM = (document.getElementsByTagName && document.createElement);

var fldsarr;
var msgsarr;
var msgtype;
var errors;

function DomValidate(flds, msgs){

	this.fldsarr = flds;
	this.msgsarr = msgs;
	this.txtClass = "";
	this.validateForm = validateForm;
	this.validatetype = validatetype;
	this.writeError = writeError;
	this.removeError = removeError;
	this.name = name;
	this.trim = trim; //AG
	this.nsname = nsname;
	this.phno = phno;
	this.required = required;
	this.errors = errors;
	this.msgtype = msgtype;
	this.writeErrors = writeErrors;
	this.email = email;
	this.multi_email = multi_email;
	this.usphone = usphone;
	this.url = url;
	this.ip = ip;
	this.mobile = mobile;
	this.postcode = postcode;
	this.number = number;
	this.namelen = namelen;
	this.ccv_3 = ccv_3;
	this.visa_num = visa_num;
	this.master_num = master_num;
	this.street_num = street_num;
	this.address = address;
	this.intlen = intlen;
	this.passwordlen = passwordlen;
	this.radioval = radioval;
}

function validateForm(frmname){

	var status;
	
	validForm = true;
	firstError = null;
	errorstring = '';
	this.errors = '';
	status = true;
	
	for (var i = 0; i < this.fldsarr.length; i++) {
		var x = eval("document." + frmname).elements[this.fldsarr[i][0]];
		this.fldsarr[i][2] = Array();
		x.statusArr = Array();
		
		for (var k = 0; k < this.fldsarr[i][1].length; k++) {
			//alert(x+' '+this.fldsarr[i][1][k]+' '+this.msgsarr[i][1][k]);
			if (this.validatetype(x, this.fldsarr[i][1][k], this.msgsarr[i][1][k])) {
				this.fldsarr[i][2][k] = 't';
				
				if (!inArray(this.fldsarr[i][2], 'f')) {
					this.removeError(x);
				}
			}
			else {
				status = false;
				this.fldsarr[i][2][k] = 'f';
			}
			x.statusArr = this.fldsarr[i][2];
			//alert(status+'----'+x.name);	
		}
	}
	if (this.msgtype == 'all') {
		this.writeErrors(this.errors);
	}
	//alert(this.errors);
	return status;
	
}

function validatetype(obj, type, msg){
	
	
	
	switch (type) {
		case 'is_set':
			return this.is_set(obj, msg);
			break;
			
		case 'comments':
			return this.comments(obj, msg);
			break;
			
		case 'required':
			return this.required(obj, msg);
			break;
			
		case 'email':
			return this.email(obj, msg);
			break;

		case 'multi_email':
			return this.multi_email(obj, msg);
			break;
			
		case 'phno':
			return this.phno(obj, msg);
			break;
			
		case 'name':
			return this.name(obj, msg);
			
			break;
			
		//AG			 
		case 'trim':
			return this.trim(obj, msg);
			
			break;
			
		case 'nsname':
			return this.nsname(obj, msg);
			
			break;
			
		case 'usphone':
			return this.usphone(obj, msg);
			break;
			
		case 'url':
			return this.url(obj, msg);
			break;
			
		case 'ip':
			return this.ip(obj, msg);
			break;
			
		case 'mobile':
			return this.mobile(obj, msg);
			break;
			
		case 'postcode':
			
			return this.postcode(obj, msg);
			
			break;
			
		case 'number':
			return this.number(obj, msg);
			break;
			
		case 'checkfile':
			return this.checkfile(obj, msg);
			break;
			
		case 'namelen':
			return this.namelen(obj, msg);
			break;
			
       case 'passwordlen':
			return this.passwordlen(obj, msg);
			break;	
			
		case 'intlen':
			return this.intlen(obj, msg);
			break;	
			
		case 'ccv_3':
			return this.ccv_3(obj, msg);
			break;
			
		case 'visa_num':
			return this.visa_num(obj, msg);
			break;
			
		case 'master_num':
			return this.master_num(obj, msg);
			break;
		case 'street_num':
			return this.street_num(obj, msg);
			break;
			
		case 'address':
			return this.address(obj, msg);
			break;
		
		case 'radioval':
			return this.radioval(obj, msg);
			break;
		
			
	}
	
}

function required(obj, msg){

	switch (obj.type) {
		case 'checkbox':
			if (!check_boxes(obj, msg)) {
				this.writeError(obj, msg);
			}
			break;
			
		case 'radio':
			if (!check_boxes(obj, msg)) {
				this.writeError(obj, msg);
			}
			break;
			
		case 'select':
			if (obj.selected == false) {
				if (!msg) {
					this.writeError(obj, 'This field is required');
				}
				else {
					this.writeError(obj, msg);
				}
				return false;
			}
			break;
			
		case 'select-multiple':
			var isSelected = false;
			for (var j = 0; j < obj.options.length; j++) {
				if (obj.options[j].selected) {
					isSelected = true;
				}
			}
			if (!isSelected) {
				if (!msg) {
					this.writeError(obj, 'This field is required');
				}
				else {
					this.writeError(obj, msg);
				}
				return isSelected;
			}
			break;
			
		default:
			if (!obj.value) {
				if (!msg) {
					this.writeError(obj, 'This field is required');
				}
				else {
					this.writeError(obj, msg);
				}
				return false;
			}
			
			
	}
	
	return true;
}


function check_boxes(obj, msg){

	//if(!isArray(obj))
	//{
	//alert("true");
	//alert(obj.value);
	if (obj.checked == false) {
		//	alert(obj.name);
		if (!msg) {
			this.writeError(obj, 'This field is required');
		}
		else {
			//alert(msg);
			this.writeError(obj, msg);
		}
		return false;
	}
	/*}else{
	 alert("else");
	 for(var i=0;i<=obj.length;i++)
	 {
	 //	alert(obj[i].name);
	 if(obj[i].checked==false)
	 {
	 if(!msg)
	 {
	 this.writeError(obj[i],'This field is required');
	 }else{
	 this.writeError(obj[i],msg);
	 }
	 return false;
	 }
	 }
	 
	 
	 }*/
	return true;
}





function writeError(obj, message){
	validForm = false;
	
	if (W3CDOM) {
		//	alert(obj.name);
		if(obj.length > 1){
			if(obj[0].type == 'radio'){
				errorstring += obj.name + ': ' + message + '\n';
				obj.hasError = true;
			}else{
				obj.style.border = "1px solid #FF0000";
			}
		}else{
			obj.style.border = "1px solid #FF0000";
		}
		

		
		obj.title = message.replace("&raquo;", "");
		obj.alt = message.replace("&raquo;", "");
		//obj.setAttribute('alt',message);
		//obj.setAttribute('title',message);
		//obj.onchange = DOMVALremoveError(obj,'formfield');
		if (obj.hasError) {
		
			if (!inArray(obj.statusArr, 'f')) {
				this.errors += "<li>" + message + "</li>";
			}
			//alert(this.errors);
			return false;
		}
		else {
			//alert(this.errors);
			if (!inArray(obj.statusArr, 'f')) {
				this.errors += "<li>" + message + "</li>";
			}
		}
		
		if (obj.type == "checkbox" || obj.type == "radio") {
			//alert(obj.statusArr);
			//alert(this.errors);
			if (document.getElementById(obj.name + '_' + obj.type + '_span')) {
				var sp = document.getElementById(obj.name + '_' + obj.type + '_span');
			}
			else {
				var sp = document.createElement('span');
			}
			
			
			sp.id = obj.name + '_' + obj.type + '_span';
			//sp.className = 'error';
			sp.innerHTML = "&laquo;";
			obj.parentNode.appendChild(sp);
		}
		obj.hasError = sp;
	}
	else {
		errorstring += obj.name + ': ' + message + '\n';
		obj.hasError = true;
	}
	
}

function writeErrors(message){
	var oElement;
	if (document.getElementById("div_error")) {
		oElement = document.getElementById("div_error");
		mElement = document.getElementById("message_error");
		if (message != "") {
			oElement.style.display = "block";
			//Error(s) occured during form submission.<br>
			mElement.innerHTML = "" + message;
			//alert(oElement.innerHTML);
		}
	}
	else {
		alert('Please create a div with "div_error" as id');
	}
	var iElement;
	//iElement=document.getElementById("showerrimg");
	//iElement.innerHTML="<img src='../images/wrong.gif'>";
}


function DOMVALremoveError(obj, clsname){
	//alert(obj.className);

	//obj.className = clsname;
}

function removeError(obj){
	//obj.className = "formfield";
	
	// if(isArray(obj))
	//  {
	//   var k = document.getElementById(obj[obj.length -1].name+'_'+obj[obj.length -1].type+'_span');
	//    k.innerHTML = "";
	//    k.style.border="";
	//    
	//  }else{
	//alert("inside remove error");
	//obj.style.border = this.txtClass;
	if(obj.length > 1){
			if(obj[0].type == 'radio'){
				obj.hasError = false;
			}else{
				obj.style.border = "1px solid #41BE26";
			}
		}else{
			obj.style.border = "1px solid #41BE26";
		}
	
	
	if (document.getElementById(obj.name + '_' + obj.type + '_span')) {
		var k = document.getElementById(obj.name + '_' + obj.type + '_span');
		k.innerHTML = "";
	}
	// }
	//  
	obj.hasError = false;
	
}


/* Validation functions start here..... */
// @ added for multiple email address validation from the single textfield - SBS 20/April/2009 
function multi_email(obj, msg){
	if(obj.value.indexOf(',') > 0){
		var email_arr = obj.value.split(',');
		var l = email_arr.length;
		while(l--){
			if(!email_val(trimAll(email_arr[l]))){
				if (!msg) 
					this.writeError(obj, 'Please enter a valid email address');
				else 
					this.writeError(obj, msg);
				return false;
			}
		}
	}
	return true;
}
// @ added for checking email with only values as parameter and not by passing the DOM object - SBS 20/April/2009 
function email_val(email_value){
	if (email_value) {
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email_value)) {
			return true;
		}else {
			return false;
		}
	}
	return true;
}

function email(obj, msg){
	if (obj.value) {
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(obj.value)) {
			return true;
		}
		else {
			if (!msg) 
				this.writeError(obj, 'Please enter a valid email address');
			else 
				this.writeError(obj, msg);
			return false;
		}
	}
	return true;
}

function phno(obj, msg){
	if (obj.value) {
		var stripped = obj.value.replace(/[\(\)\.\]\[\-\+\ ]/g, '');
		if (isNaN(parseInt(stripped))) {
			if (!msg) 
				this.writeError(obj, 'Please enter a valid Phone Number');
			else 
				this.writeError(obj, msg);
			return false;
		}
		
		
		if (stripped.length < 7) {
			if (!msg) 
				this.writeError(obj, 'Please enter a valid Phone Number');
			else 
				this.writeError(obj, msg);
			return false;
		}
	}
	return true;
}

function street_num(obj, msg){
	var street = trimAll(obj.value);
	if (street != "") {
	
	
		if (/^((\[|\]|\-|\(|\))*|\w|\s)+$/.test(obj.value)) {
			return true;
		}
		else {
			if (!msg) 
				this.writeError(obj, 'Please enter a valid Street Number');
			else 
				this.writeError(obj, msg);
			return false;
		}
		
	}
	else {
		return true;
	}
	
}

function address(obj, msg){
	var addr = trimAll(obj.value);
	if (addr != "") {
	
	
		if (/^((\[|\]|\-|\(|\)|\,|'|&|\.)*|\w|\s)+$/.test(obj.value)) {
			return true;
		}
		else {
			if (!msg) 
				this.writeError(obj, 'Please enter a valid Address');
			else 
				this.writeError(obj, msg);
			return false;
		}
		
		if (addr.length <= 5) {
			if (!msg) 
				this.writeError(obj, 'Please enter a valid Address');
			else 
				this.writeError(obj, msg);
			return false;
		}
		
	}
	else {
		return true;
	}
	
	
}
/*
@modified for checking the single element validation for the radio group
when there is only single radio button it has no length
but when here ae many radio button with same name they form a nodeGroup which has length and have to loop to check for each radio button for validation
- SBS 21/April/2009 
*/
function radioval(obj, msg){

	var checked = false;
	if(obj.length > 1){
		var i = obj.length;
		while(i--){
			if (obj[i].checked) {
				checked = true;
				break;
			}
		}
	}else if(obj.childNodes.length == 0){
		if(obj.checked){
			checked = true;
		}
	}
	if(!checked)
		this.writeError(obj, msg);
	return checked; 
}

function name(obj, msg){
	if (obj.value) {
		if (/^(([A-Za-z]+\s?\'?[A-Za-z]*)\s?)+[\d]{0,10}$/.test(obj.value)) {
			return true;
		}
		else {
			if (!msg) 
				this.writeError(obj, 'Please enter a valid name');
			else 
				this.writeError(obj, msg);
			return false;
		}
	}
	return true;
}

/* to check whether name is atleast nore than 2 chars */
function namelen(obj, msg){
	//		alert(obj.value+","+msg);
	var lenval = trimAll(obj.value);
	if (lenval) {
		if (lenval.length >= 2) {
			return true;
		}
		else {
			if (!msg) 
				this.writeError(obj, 'Please enter a valid name');
			else 
				this.writeError(obj, msg);
			return false;
		}
	}
	else {
		if (!msg) 
			this.writeError(obj, 'Please enter a valid name');
		else 
			this.writeError(obj, msg);
		return false;
	}
	return true;
}

/* to check whether name is atleast nore than 60 */
function intlen(obj, msg){
	var lenval = trimAll(obj.value);
	if(lenval == "" || !(lenval))
	{
		lenval = 0;
	}
	if (lenval || lenval == "" || !(lenval)) {
		if (lenval < 60 || lenval == "" || !(lenval)) {
			return true;
		}
		else {
		
			if (!msg) 
				this.writeError(obj, 'Please enter a valid number');
			else 
				this.writeError(obj, msg);
			return false;
		}
	}
	else {
			alert("hi");
		if (!msg) 
			this.writeError(obj, 'Please enter a valid name');
		else 
			this.writeError(obj, msg);
		return false;
	}
	return true;
}

function passwordlen(obj, msg){
	//		alert(obj.value+","+msg);
	var lenval = trimAll(obj.value);
	if (lenval) {
		if (lenval.length >= 5) {
			return true;
		}
		else {
			if (!msg) 
				this.writeError(obj, 'Please enter a valid number');
			else 
				this.writeError(obj, msg);
			return false;
		}
	}
	else {
		if (!msg) 
			this.writeError(obj, 'Please enter a valid name');
		else 
			this.writeError(obj, msg);
		return false;
	}
	return true;
}


function nsname(obj, msg){
	if (obj.value) {
	
		if (/^[a-z\']+$/i.test(obj.value)) {
		
			return true;
		}
		else {
			if (!msg) 
				this.writeError(obj, 'Please enter a valid name');
			else 
				this.writeError(obj, msg);
			return false;
		}
	}
	
	return true;
}

function usphone(obj, msg){
	if (obj.value) {
		if (/^(\+?\d{2}\s?)?\([1-9]\d{2}\)\s?\d{3}\-?\d{4}$/.test(obj.value)) {
			return true;
		}
		else {
			if (/^(\+?\d{2}\s?)?\[[1-9]\d{2}\]\s?\d{3}\-?\d{4}$/.test(obj.value)) {
				return true;
			}
			else {
				if (/^(\+?\d{2}\s?)?[1-9]\d{2}\s?\d{3}\-?\d{4}$/.test(obj.value)) {
					return true;
				}
				else {
					if (!msg) 
						this.writeError(obj, 'Please enter a valid US Phone Number');
					else 
						this.writeError(obj, msg);
					return false;
				}
			}
		}
	}
	return true;
}

function url(obj, msg){
	if (obj.value) {
		if (/^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([\w]+)(.[\w]+){1,2}$/.test(obj.value)) {
			return true;
		}
		else {
			if (!msg) 
				this.writeError(obj, 'Please enter a valid URL');
			else 
				this.writeError(obj, msg);
			return false;
		}
	}
	return true;
}

function ip(obj, msg){
	if (obj.value) {
		if ((obj.value != "0.0.0.0") && (obj.value != "255.255.255.255")) {
			if (/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.test(obj.value)) {
				return true;
				
			}
			else {
			
				if (!msg) {
					this.writeError(obj, 'Please enter a valid IP Address');
				}
				else {
					this.writeError(obj, msg);
				}
				return false;
			}
			
			
		}
		else {
			if (!msg) 
				this.writeError(obj, 'Please enter a valid IP Address');
			else 
				this.writeError(obj, msg);
			return false;
		}
	}
	return true;
}

function mobile(obj, msg){
	if (obj.value) {
		var stripped = obj.value.replace(/[\(\)\.\-\ ]/g, '');
		if (!(isNaN(stripped)) && (stripped.length > 9)) {
			return true;
		}
		else {
			if (!msg) 
				this.writeError(obj, 'Please enter a valid mobile number');
			else 
				this.writeError(obj, msg);
			return false;
		}
	}
	return true;
}

function is_int(val){
	var str = new String(val);
	arr = str.split(".");
	
	if (arr.length > 1) {
		return false;
	}
	else {
		return true;
	}
	
}

function ccv_3(obj, msg){
	if (!isNaN(obj.value) && is_int(obj.value) && obj.value.length == 3) {
		return true;
	}
	else {
		if (!msg) 
			this.writeError(obj, 'Please enter a valid ccv number');
		else 
			this.writeError(obj, msg);
		return false;
	}
}

function visa_num(obj, msg){
	if (!isNaN(obj.value) && is_int(obj.value) && ((obj.value.length == 13) || (obj.value.length == 16)) && (get_initial_digit(obj.value) == "4")) {
		return true;
	}
	else {
		if (!msg) 
			this.writeError(obj, 'Please enter a valid credit card number');
		else 
			this.writeError(obj, msg);
		return false;
	}
}

function master_num(obj, msg){
	if (!isNaN(obj.value) && is_int(obj.value) && ((obj.value.length == 16)) && (get_initial_digit(obj.value) == "5")) {
		return true;
	}
	else {
		if (!msg) 
			this.writeError(obj, 'Please enter a valid credit card number');
		else 
			this.writeError(obj, msg);
		return false;
	}
}

function get_initial_digit(val){
	var str = new String(val);
	return str.substring(0, 1)
}

function postcode(obj, msg){

	if (obj.value) {
		if (!isNaN(obj.value) && obj.value.length > 3) {
			return true;
		}
		else {
			if (!msg) 
				this.writeError(obj, 'Please enter a valid postcode');
			else 
				this.writeError(obj, msg);
			return false;
		}
	}
	return true;
}

function number(obj, msg){
	if (obj.value) {
		if (!isNaN(obj.value)) {
			return true;
		}
		else {
			if (!msg) 
				this.writeError(obj, 'Please enter a valid Number');
			else 
				this.writeError(obj, msg);
			return false;
		}
	}
	return true;
}

function checkfile(obj, msg){
	if (obj.value) {
		if (/^[\w+\s*]+.[pdf|doc|txt|jpg|gif]+/i.test(obj.value)) {
			return true;
		}
		else {
			if (!msg) 
				this.writeError(obj, 'Invalid File Extension');
			else 
				this.writeError(obj, msg);
			return false;
		}
	}
	return true;
}

//function to trim the spaces ->AG
function trimAll(sString){
	while (sString.substring(0, 1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length - 1, sString.length) == ' ') {
		sString = sString.substring(0, sString.length - 1);
	}
	return sString;
}

// Removes leading and ending whitespaces ->AG
function trim(obj, msg){
	if (obj.value) {
		if (trimAll(obj.value) != "") {
			return true;
		}
		else {
			if (!msg) 
				this.writeError(obj, 'Please enter your name');
			else 
				this.writeError(obj, msg);
			return false;
		}
	}
	return true;
}


/* google toolbar fix [no way related to the class] --ny */


if (window.attachEvent) 
	window.attachEvent("onload", setListeners);

function setListeners(){
	///hide the show errors div 
	if (document.getElementById("showallerrors")) {
		oElement = document.getElementById("showallerrors");
		//			alert(			oElement.style.display);
		oElement.style.display = "none";
	}
	///End hiding
	
	
	
	inputList = document.getElementsByTagName("INPUT");
	for (i = 0; i < inputList.length; i++) {
		inputList[i].attachEvent("onpropertychange", restoreStyles);
		inputList[i].style.backgroundColor = "";
	}
	selectList = document.getElementsByTagName("SELECT");
	for (i = 0; i < selectList.length; i++) {
		selectList[i].attachEvent("onpropertychange", restoreStyles);
		selectList[i].style.backgroundColor = "";
	}
}

function restoreStyles(){
	if (event.srcElement.style.backgroundColor != "") 
		event.srcElement.style.backgroundColor = "";
}

function isArray(obj){
	if (obj.constructor.toString().indexOf("Array") == -1) 
		return false;
	else 
		return true;
}

function inArray(arr, pstr){
	var str;
	str = "  " + arr.toString();
	//alert(str+"---------"+pstr);
	if (str.indexOf(pstr) > 0) 
		return true;
	else 
		return false;
}

/* google toolbar fix [no way related to the class] --ny */

function comments(obj, msg){
	if (obj.value) {
		if (/^(([A-Za-z]+\s?\'?[A-Za-z]*)\s?)+[\d]{0,10}$/.test(obj.value)) {
			return true;
		}
		else {
			if (!msg) 
				this.writeError(obj, 'Please enter a valid comments');
			else 
				this.writeError(obj, msg);
			return false;
		}
	}
	return true;
}
