
//-- FORM FUNCTIONS --

var errors = 0;
var formName = 'input';

function clearForm(curForm){
	var fieldType = '';
	for(x = 0; x < curForm.length; x++){
		fieldType = curForm.elements[x].type;
		if((fieldType == 'text')||(fieldType == 'textarea')||(fieldType == 'password')){
			curForm.elements[x].value = '';
		}
		if((fieldType == 'select-one')||(fieldType == 'select-multiple')){
			curForm.elements[x].selectedIndex = 0;
		}
		if((fieldType == 'checkbox')||(fieldType == 'radio')){
			curForm.elements[x].checked = false;
		}
	}
}

function set_select(field,value){
	for(x = 0; x < eval('document.'+formName+'.'+field+'.length'); x++){
		if(eval('document.'+formName+'.'+field+'['+x+'].value == '+ value)){
			eval('document.'+formName+'.'+field+'.selectedIndex = '+x)
		}
	}
}

function set_field(name,value){eval('document.'+formName+'.'+name+'.value = "'+ value +'"');}


//-- FORM VALIDATION --

function new_test(){errors = 0;}

function send_form(){
	if(errors == 0){eval('document.'+formName+'.submit()');}
}

function raiseError(field,errorTxt,type){
	errors++;
	if(type == 1){ alert(Lang.Error + ": " + Lang.ErrInvalidValue + " " + errorTxt + "."); }
	if(type == 2){ alert(Lang.Error + ": " + Lang.ErrSelectValue + " " + errorTxt + "."); }
	if(type == 3){ alert(Lang.Error + ": " + errorTxt + " " + Lang.ErrInvalidDate + "."); }
	eval('document.'+formName+'.'+field+'.focus()');
}

function check_text(field,errorTxt){
	if(errors == 0){
		if(eval('document.'+formName+'.'+field+'.value == ""')){raiseError(field,errorTxt,1);}
	}
}

function check_select(field,errorTxt){
	if(errors == 0){
		if(eval('document.'+formName+'.'+field+'.selectedIndex == 0')){raiseError(field,errorTxt,2);}
	}
}

function check_box(field,errorTxt){
	if(errors == 0){
		if(! eval('document.'+formName+'.'+field+'.checked')){raiseError(field,errorTxt,2);}
	}
}

function check_len(field,errorTxt,operator,length){
	if(errors == 0){
		if(eval('document.'+formName+'.'+field+'.value.length '+operator+' '+length)){raiseError(field,errorTxt,1);}
	}
}

function check_str(field,errorTxt,target){
	if(errors == 0){
		var cStr = eval('document.'+formName+'.'+field+'.value');
		if(cStr.indexOf(target) == -1){raiseError(field,errorTxt,1);}
	}
}

function check_chr(field,errorTxt,target){
	if(errors == 0){
		var cStr = eval('document.'+formName+'.'+field+'.value');
		if(cStr.indexOf(target) != -1){raiseError(field,errorTxt,1);}
	}
}

function check_value(field,errorTxt,operator,target){
	if(errors == 0){
		if(eval('document.'+formName+'.'+field+'.value '+ operator +' '+ target)){raiseError(field,errorTxt,1);}
	}
}

function check_email(field){
	if(errors == 0){
		var eStr = eval('document.'+formName+'.'+field+'.value');
		check_str(field, Lang.LblEmail, "@");
		check_str(field, Lang.LblEmail, ".");
	}
}

function check_num(field,errorTxt){
	if(errors == 0){
		var cStr = eval('document.'+formName+'.'+field+'.value');
		if(cStr - cStr != 0){raiseError(field,errorTxt,1);}
	}
}

var m_days = new Array(31,29,31,30,31,30,31,31,30,31,30,31);

function check_date(field,errorTxt){
	if(errors == 0){
		var strDate = eval('document.'+formName+'.'+field+'.value');
		var inDate = strDate.split("/");
		if (inDate[1] > m_days[inDate[0] - 1]){raiseError(field,strDate,3);}
	}
}

function check_isodate(field,errorTxt){
	if(errors == 0){
		var strDate = eval('document.'+formName+'.'+field+'.value');
		if (strDate.search(/^\d+-\d\d-\d\d$/) != 0) {
			raiseError(field,strDate,3);
		} else {
			var inDate = strDate.split("-");
			if (inDate[2] > m_days[inDate[1] - 1]){raiseError(field,strDate,3);}
		}
	}
}

function check_time(field,errorTxt){
	if(errors == 0){
		var strTime = eval('document.'+formName+'.'+field+'.value');
		if (strTime.search(/^\d\d:\d\d\s(AM|PM)$/) != 0) {
			raiseError(field,strTime,3);
		} else {
			strTime = strTime.substring(0,5);
			inTime = strTime.split(":");
			if (inTime[0] > 12 || inTime[1] > 59){raiseError(field,strDate,3);}
		}
	}
}

function replace_str(field,target,result){
	eval('document.'+formName+'.'+field+'.value = document.'+formName+'.'+field+'.value.replace(/'+target+'/g, "'+result+'")');
}
