/**
 *
 *	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);
}