/**
 *
 *	by tGDA, t_gda$AT$hotmail.com,20080313
 *
 */
 


function AutoForm(name, cssClassesObj) {
	this.name=name;
	this.reqd_fields=new Array();
	this.identicalFieldSuffix='_re';
	this.fieldsRegexCheck=new Array();
	this.cssClassesObj=cssClassesObj;
	this.fieldOptions=new Array();
	
	this.extracheckingcallback=false;
}

function in_array(arr, val) {
	for(i in arr) if (arr[i]==val) return true;
	return false;
}

AutoForm.prototype.validate=function() {
	
	for(var i in this.reqd_fields) {
		if (isNaN(parseInt(i)))  continue;
		var targetInputID='form_'+this.name+'_'+this.reqd_fields[i];
		var targetInput=document.getElementById(targetInputID);
		if(!targetInput.value || targetInput.value=='') {
			BlinkBgById(targetInputID, this.cssClassesObj.BLINK_REQD, this.cssClassesObj.REQD);
			targetInput.focus();
			//alert('Fill All the Required fields!');
			return false;
		}
		
		//checking if there's a special regex it should follow
		if (this.fieldsRegexCheck[this.reqd_fields[i]]) {
			if (targetInput.value.search(this.fieldsRegexCheck[this.reqd_fields[i]])==-1) {
				BlinkBgById(targetInputID, this.cssClassesObj.BLINK_REGX, this.cssClassesObj.REQD);
				return false;
			}
		}
		
		//checking if there's any field that should be identical with this
		var targetIdenticalInput;
		if (targetIdenticalInput=document.getElementById(targetInputID+this.identicalFieldSuffix)) {
			if((targetIdenticalInput.value!='') && targetInput.value!=targetIdenticalInput.value) {
				//TODO:check whether it's really reqd
				BlinkBgById(targetInputID, this.cssClassesObj.BLINK_REQD, this.cssClassesObj.REQD);
				BlinkBgById(targetInputID+this.identicalFieldSuffix, this.cssClassesObj.BLINK_REQD, this.cssClassesObj.REQD);
				return false;
			}
		}
		
		//if is a file
		if (targetInput.type=='file') {
			var allowed_exts=this.fieldOptions[this.reqd_fields[i]]['exts'];
			var file_ext=targetInput.value.substr(targetInput.value.lastIndexOf('.')+1).toLowerCase();
			if(!in_array(allowed_exts, file_ext)) {
				BlinkBgById(targetInputID, this.cssClassesObj.BLINK_REQD, this.cssClassesObj.REQD);
				return false;
			}
			
		}
	}
	if(this.extracheckingcallback) {
		return this.extracheckingcallback();
	}

	return true;
}

function BlinkBgById(targetElemID, blinkClassName, origClassName) {
	//alert(blinkClassName+' '+origClassName);
	//alert(document.getElementById(targetElemID).className);
	appendClassName(targetElemID, blinkClassName);
	//alert("appendClassName('"+targetElemID+"', '"+origClassName+"');")
	setTimeout("replaceClassName('"+targetElemID+"', '"+blinkClassName+"', '"+origClassName+"');",555);
	setTimeout("replaceClassName('"+targetElemID+"', '"+origClassName+"', '"+blinkClassName+"');",555*2);
	setTimeout("replaceClassName('"+targetElemID+"', '"+blinkClassName+"', '"+origClassName+"');",555*3);
}

function appendClassName(elemID, newClassName) {
	var elem=document.getElementById(elemID);
	elem.className=elem.className.replace(newClassName, '').replace('  ', ' ')+' '+newClassName;
	//alert(elem.className);
}



function replaceClassName(elemID, oldClassName, newClassName) {
	var elem=document.getElementById(elemID);
	elem.className=elem.className.replace(oldClassName, '').replace('  ', ' ')+' '+newClassName;
	//alert(elem.className);
}

function GetRes (arr,blinkClassName,className)
{
	//alert (arr.length);
	$error=false;
	for(var i=0 ; i<arr.length ; i++)
	{
		//alert (arr[i]);
		var field=document.getElementById(arr[i]).value;
		//alert (field);
		if( !(hasWhiteSpace(field)) || field == '')
		{
			//alert ("hi");
			BlinkBgById(arr[i],blinkClassName,className);
			$error=true;
			break;
		}
		else if (arr[i] == 'email')
		{
			if(!(validate(field)) )
			{
				BlinkBgById(arr[i],blinkClassName,className);
				$error=true;
				break;
			}
		}
		
	}
	if( !$error ) 
	{
		//alert('hii');
		document.form.submit();
	}
}


function validate(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = email;
   if(reg.test(address) == false) {
      return false;
   }
   else
   		return true;
}

function hasWhiteSpace(s) 
{
     reWhiteSpace = new RegExp(/^\s+$/);

     // Check for white space
     if (reWhiteSpace.test(s)) {
          return false;
     }
	 else
	 {
    	return true;
	 }
}



