// general purpose validation function
function ck_num(field)
{
	val = parseInt(field.value);
	if (isNaN(val)) 
	{
		alert("Invalid number format");
		field.focus();
		return false;
	}
	
	return true;
}

function ck_pos_num(field)
{
	val = parseInt(field.value);
	if (isNaN(val)) {
		alert("Invalid number format");
		field.focus();
		return false;
	}

	
	if (val>0) return true; 
		else 
	{	
		alert("Invalid number format");
		field.focus();
		return false;
	}
}

function ck_text(field, zval, str, errorclass)
{
	if (field.value == zval) 
	{
		if (str!="")
		{
			alert(str);
			if(errorclass) field.className = errorclass;
			field.focus();
		}
		return false;
	} else 
		return true;
}
	
function ck_select(field, zval, zindex, str, errorclass)
{
	if (field.options[field.selectedIndex].value==zval||field.selectedIndex==zindex)
	{
		if (str!="")
		{
			alert(str);
			if(errorclass) field.className = errorclass;
			field.focus();
		}
		return false;
	}
	else
		return true;
}
	

function ck_radio(field, str)
{
	for (i=0;i<field.length;i++)
		if (field[i].checked) return i+1;
		
	if (str!="")
	{
		alert(str);
		field[0].focus();
	}
	return false;
}
	
function validate_email(field, alert_str, errorclass)
{
	var str = Trim(field.value);
//	alert(str);
	if (window.RegExp) {
		var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
		var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
		var reg1 = new RegExp(reg1str);
		var reg2 = new RegExp(reg2str);
		if (!reg1.test(str) && reg2.test(str)) {
			return true;
		}
		alert(alert_str);
		field.focus();
		field.select();
		return false;
	} else {
		if(str.indexOf("@") >= 0)
		{
			return true;
		}
		alert(alert_str);
		if(errorclass) field.className = errorclass;
		field.focus();
		field.select();
		return false;
	}
}

// Regular expresion validator
function validate_regexp(field, expr, alert_str)
{
	var str = field.value;

	if (window.RegExp) 
	{
		var reg1str = expr;
		var reg1 = new RegExp(reg1str);
		if (field.value=="" || reg1.test(field.value)) 
		{
			return true;
		}
		alert(alert_str);
		field.focus();
		return false;
	}
}

// auto fill form with supplied values
function set_form(f, names, values)
{
	var i;

	for (i=0; i<names.length; i++)
	{
		if (e = f.elements[names[i]]) 
		{
	//		alert(i + " " + e.name +" "+ e.type+" "+ values[i]);
			switch(e.type) {
			case "hidden" :
			case "text" :
			case "textarea" : e.value = values[i]; break;
			case "select-one" : set_select(e, values[i]); break;
			case "checkbox" : set_checkbox(e, values[i]); break;	
			}

			if (e.length>0&&e[0].type=="radio") 
				set_radio(e, values[i]);
		}
	}
}

// functions used by set_form

function set_text(field, val)
{
	field.value = val;
	return true;
}

function set_select(field, val)
{
	var i;
	for (i=0;i<field.options.length;i++)
		if (field.options[i].value == val) 
		{
			field.selectedIndex = i;
			return true;
		}
	return false;
}

function set_checkbox(field, val)
{
	if (field.value != val) return false;
	field.checked = true;
	return true;
}

function set_radio(field, val)
{
	var i;
	var ok;
	ok = false;
	for (i=0; i<field.length;i++)
	{
//	alert(field[i].value);
		if (field[i].value == val)
		{
	//		alert("Found i = "+i);
			field[i].checked = true;
			ok = true;
		}
		else field[i].checked = false;
	}
	return ok;
}
//e-mail scrambler
function m_scr(part1, part2, part3, caption)
{
	str = part1+String.fromCharCode(eval(64))+part2+String.fromCharCode(eval(46))+part3;
	if (caption!="")
	{
	document.write ("<a href='ma"+"ilto:"+str+"'>"+caption+"</a>");
	}
	else
	{
	document.write ("<a href='ma"+"ilto:"+str+"'>"+str+"</a>");
	}
}
//buttons and stuff
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function init()
	{
		for(i=0; i<norm_butt.length; i++)
			{
				butt_arr[i]=norm_butt[i];

			}
		for(i=0; i<norm_butt.length; i++)
			{
				MM_preloadImages(high_butt[i]);
			}
	}
function showbutt()
	{

		for (i=0;i<document.images.length; i++)
			{
				document.images[i].src=butt_arr[i];
			}
	}

function color(s)
	{
		document.images[s].src=high_butt[s];
	}
function uncolor(s)
	{
		document.images[s].src=butt_arr[s];
	}
function press(s)
	{
		for(i=0; i<norm_butt.length; i++)
			{
				butt_arr[i]=norm_butt[i];
			}
		butt_arr[s]=high_butt[s];
		showbutt();
		color(s);
	}

function checkallboxes(field)
{
	
	if (typeof(field)=="object")
	{
	for (i=0; i<field.length; i++)
		{
			field[i].checked=true;
		}
	field.checked=true;
	}
}

function uncheckallboxes(field)
{
	if (typeof(field)=="object")
	{
	for (i=0; i<field.length; i++)
		{
			field[i].checked=false;
		}
	field.checked=false;
	}
}

function getradiovalue(field)
{
	for(i=0; i<field.length; i++)
		{
			if (field[i].checked) return field[i].value;
		}
	return false;
}


function Trim(s) {

//alert (s.substring(0,1));
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length-1);
//	alert(s);
  }

//alert(s.substring(s.length-1,s.length));	
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
//	alert(s);
  }
  return s;
}

//check field if is numeric and fixed digits
function check_num(field,strlen){
	var value=field.value;
	if(!isNaN(value) && value.length==Number(strlen)) return true; 
	else return false;	
}
//check +positive number
function check_positive(field){
	var value=field.value;
	if(Number(field.value)>0) return true; 
else { alert("Please enter a positive number!");return false;}	
}

var pageFormIsSubmitting = false;
function submitForm(action1, formName)
{
    if (formName == undefined) {
        if (document.forms.length > 0) {
            formName = document.forms[0].name;
        } else {
            return;
        }
    }
    var currentForm = document.forms[formName];

    // setez action-ul
    if (currentForm.elements["action"]) {
        currentForm.elements["action"].value = action1;
    }

    // fac submit
    doSubmit(formName);
}
/* }}} form related functions */


function doSubmit(form1)
{
    if (pageFormIsSubmitting || ! document.forms[form1]) {
        return false;
    }

    pageFormIsSubmitting  = true;
    document.forms[form1].submit();
}


function checkdate(objName) {
	var datefield = objName;
	if (chkdate(objName) == false) {
		datefield.select();
		alert("That date is invalid.  Please try again.");
		datefield.focus();
		return false;
	}
	else {
		return true;
	}
}

function chkdate(objName) {
	var strDatestyle = "US"; //United States date style
	//var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield.value;
	if (strDate.length < 1) {
		return true;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) {
				err = 1;
				return false;
			}
			else {
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			booFound = true;
	   }
	}
	if (booFound == false) {
		if (strDate.length>5) {
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
	   }
	}
	if (strYear.length == 2) {
		strYear = '20' + strYear;
	}
	// US style
	if (strDatestyle == "US") {
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
		err = 2;
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
		for (i = 0;i<12;i++) {
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
		   }
		}
		if (isNaN(intMonth)) {
			err = 3;
			return false;
		}
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
		err = 4;
		return false;
	}
	if (intMonth>12 || intMonth<1) {
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
		err = 7;
		return false;
	}
	if (intMonth == 2) {
		if (intday < 1) {
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true) {
			if (intday > 29) {
				err = 9;
				return false;
			}
		}
		else {
			if (intday > 28) {
				err = 10;
				return false;
			}
		}
	}
	if (strDatestyle == "US") {
		datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
	}
	else {
		datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
	}
	return true;
}

function LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}
else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}

function doDateCheck(from, to) {
	alert(from.value);
	if (Date.parse(from.value) <= Date.parse(to.value)) {
		alert("The dates are valid.");
	}
	else {
		if (from.value == "" || to.value == "") 
			alert("Both dates must be entered.");
		else 
			alert("End date must occur after the start date.");
   }
}
