// Generic Form Validation
// Mutual Exchange

var checkObjects	= new Array();
var errors		= "";  
var returnVal		= "";
var language		= new Array();
language["header"]	= "The following error(s) occured:"
language["start"]	= "->";
language["field"]	= " Field ";
language["require"]	= " is required";
language["min"]		= " and must consist of at least ";
language["max"]		= " and must not contain more than ";
language["minmax"]	= " and no more than ";
language["chars"]	= " characters";
language["num"]		= " and must contain a number";
language["email"]	= " must contain a valid e-mail address";

// -----------------------------------------------------------------------------
// define - Call this function in the beginning of the page. I.e. onLoad.
// n = name of the input field (Required)
// type= string, num, email (Required)
// min = the value must have at least [min] characters (Optional)
// max = the value must have maximum [max] characters (Optional)
// d = (Optional)
// -----------------------------------------------------------------------------
function define(n, type, HTMLname, min, max, d) {
	var p;
	var i;
	var x;
	if (!d)
	d = document;
	if ((p=n.indexOf("?"))>0&&parent.frames.length) {
		d = parent.frames[n.substring(p+1)].document;
		n = n.substring(0,p);
	}
	if (!(x = d[n]) && d.all) 
		x = d.all[n];
	for (i = 0; !x && i < d.forms.length; i++) {
		x = d.forms[i][n];
	}
	for (i = 0; !x && d.layers && i < d.layers.length; i++) {
		x = define(n, type, HTMLname, min, max, d.layers[i].document);
		return x;       
	}
	eval("V_"+n+" = new formResult(x, type, HTMLname, min, max);");
	checkObjects[eval(checkObjects.length)] = eval("V_"+n);
}


function formResult(form, type, HTMLname, min, max) {
	this.form = form;
	this.type = type;
	this.HTMLname = HTMLname;
	this.min  = min;
	this.max  = max;
}

function validate() {
	
	if (checkObjects.length > 0) {
		errorObject = "";
		for (i = 0; i < checkObjects.length; i++) {
			validateObject = new Object();
			validateObject.form = checkObjects[i].form;
			validateObject.HTMLname = checkObjects[i].HTMLname;
			validateObject.val = checkObjects[i].form.value;
			validateObject.len = checkObjects[i].form.value.length;
			validateObject.min = checkObjects[i].min;
			validateObject.max = checkObjects[i].max;
			validateObject.type = checkObjects[i].type;
			       
			if (validateObject.type == "num" || validateObject.type == "string" ) {
				
				 if ((validateObject.type == "num" && validateObject.len <= 0) || (validateObject.type == "num" && isNaN(validateObject.val))) { 
						
						errors += validateObject.HTMLname +",";

				} else if (validateObject.min && validateObject.max && (validateObject.len < validateObject.min || validateObject.len > validateObject.max)) { 
		
						errors += validateObject.HTMLname + ","	;
				} else if (validateObject.min && !validateObject.max && (validateObject.len < validateObject.min)) { 
		
						errors += validateObject.HTMLname + ",";

				} else if (validateObject.max && !validateObject.min &&(validateObject.len > validateObject.max)) { 
		
						errors += validateObject.HTMLname + ",";

				} else if (!validateObject.min && !validateObject.max && validateObject.len <= 0) { 
		
						errors += validateObject.HTMLname + "," ;

				}
			} else if(validateObject.type == "email") {
			// Checking existense of "@" and ".". 
			// Length of must >= 5 and the "." must 
			// not directly precede or follow the "@"
				if (validateObject.val.length != 0  ){
					if ((validateObject.val.indexOf("@") == -1) || (validateObject.val.charAt(0) == ".") || (validateObject.val.charAt(0) == "@") || (validateObject.len < 6) || (validateObject.val.indexOf(".") == -1) || (validateObject.val.charAt(validateObject.val.indexOf("@")+1) == ".") || (validateObject.val.charAt(validateObject.val.indexOf("@")-1) == ".")
						|| (validateObject.val.indexOf("@") != validateObject.val.lastIndexOf("@")) || (validateObject.val.indexOf("@") > validateObject.val.lastIndexOf(".")) ) { 
				
						errors += validateObject.HTMLname +","; 
					}

				}
			}
			else if (validateObject.type == "nullnum"){
				if(isNaN(validateObject.val) && validateObject.len >0 ) { 
					errors += validateObject.HTMLname +",";

				}
			}
			else if (validateObject.type == "int"){
				if((validateObject.len <= 0) || isNaN(validateObject.val) || (validateObject.val.indexOf(".") != -1)) { 
					
						errors += validateObject.HTMLname +",";

				}
			}

			 else if (validateObject.type == "equal"){
				if(validateObject.val != checkObjects[i-1].form.value){
				      errors += validateObject.HTMLname +",";
				}
			}

			/* For currency validations upto 2 decimal places*/
			else if (validateObject.type == "numAmount"){
				if( (validateObject.len >0 && (isNaN(validateObject.val)  || (validateObject.len - (validateObject.val.indexOf(".")+1) >2) ))) { 
					
						errors += validateObject.HTMLname +",";

				}
			}
			else if (validateObject.type == "intExceptZero"){
				if((validateObject.len <= 0) || isNaN(validateObject.val) || (validateObject.val.indexOf(".") != -1) ||
					validateObject.val <=0) { 
					
						errors += validateObject.HTMLname +",";

				}
			}
			
		}
	}
		
	if (errors) {
			returnVal = errors;
			errors = "";
	}
	else {
			returnVal = "";
	}
		
   return returnVal;
}



function cancelUser(url){
	document.forms[0].action=url+"/Login";
	document.forms[0].submit();
}

/* show hide errro div
errorString -->Error string returned by validate method
*/
function showHideError(errorString){
	var errorDiv ="";
	var errorArray = new Array();
	errorArray= errorString.split(",");
	if(errorArray.length > 0){
		for(j=0; j<checkObjects.length; j++){
			errorDiv = checkObjects[j].HTMLname.toString()+"Error";
			for(i=0; i<(errorArray.length-1); i++){
				if(checkObjects[j].HTMLname.toString() == errorArray[i]){
					document.getElementById(errorDiv).style.display= "block";	
					break;
				}
				else if(checkObjects[j].HTMLname.toString()!= errorArray[i]){
					document.getElementById(errorDiv).style.display= "none";	
				}
			}
		}
	}
}


/*
Check null string 
*/
function checkNull(txt){
	if(Trim(txt)==""){
		txt="";
		return false;
	}
	return true;
}

/*
Special character check
*/
function specialCharacterCheck(str){
	var iChars = "!@#$%^&*()+=-[]';,{}|\"<>?";              
	 for (var i = 0; i < str.length; i++) {
  		if (iChars.indexOf(str.charAt(i)) != -1) {
  	  		return false;
  		}
	}
}

function Trim(trimvalue){
	if(trimvalue.length < 1){
		return"";
	}
	trimvalue = RTrim(trimvalue);
	trimvalue = LTrim(trimvalue);
	if(trimvalue==""){
		return "";
	}
	else{
		return trimvalue;
	}
}

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function