// general purpose validation function
function foc(field)
{
	field.select();
	field.focus();
}

function ck_num(field,str)
{
	val = parseInt(field.value,10);//returns bogus value
	if (isNaN(val))
	{
		foc(field);
		alert(str);
		return false;
	}

	return true;
}

function ck_pos_num(field)
{
	val = parseInt(field.value,10);//returns bogus value
	if (isNaN(val)) {
		foc(field);
		alert("Please enter a valid number");
		return false;
	}

	if (val>=0) return true;
	else
	{
		foc(field);
		alert("Please enter a positive number !");
		return false;
	}
}




function compare_num(field1,field2,str)
//checks if filed1<field2 and that both fields are positive numbers
{
	var return_value = true;
	num1 = parseInt(field1.value,10);
	num2 = parseInt(field2.value,10);
	if (num1>num2)
	{
		if (str!="")
		{
			foc(field1);
			alert(str);
		}
		return false;
	}
	else
	{
		return true;
	}
}

function ck_float_pos_num(field)
{
	if (!Number(field.value)) {
		foc(field);
		return false;
	}
	val = parseFloat(field.value);
	if (val>0) return true;
		else
	{
		foc(field);
		return false;
	}
}

function ck_text(field, zval, str)
{

	if (field.value == zval)
	{
		if (str!="")
		{
			foc(field);
			alert(str);
		}
		return false;
	}
	else return true;
}

function ck_identical(field_1, field_2, str)
{
	if (field_1.value != field_2.value)
	{
		if (str!="")
		{
			foc(field_2);
			alert(str);
		}
		return false;
	} else
		return true;
}

function ck_length(field, len, str)
{
	if (field.value.length < len)
	{
		if (str!="")
		{
			foc(field);
			alert(str);
		}
		return false;
	} else
		return true;
}
//comparator = -1 ... field<len comparator=0 field=len comparator = 1 field>len
function ck_length_ext(field,len,str,comparator)
{
	switch (comparator)
	{
		case -1:
		if (field.value.length < len)
		{
			if (str!="")
			{
				foc(field);
				alert(str);
			}
			return false;
		}
		else return true;
		break;

		case 0:
		if (field.value.length != len)
		{
			if (str!="")
			{
				foc(field);
				alert(str);
			}
			return false;
		}
		else return true;
		break;

		case 1:
		if (field.value.length > len)
		{
			if (str!="")
			{
				foc(field);
				alert(str);
			}
			return false;
		}
		else return true;
		break;
	}
}

function ck_select(field, zval, zindex, str)
{
	if (field.options[field.selectedIndex].value==zval || field.selectedIndex==zindex)
	{
		if (str!="")
		{
			field.focus();
			alert(str);
		}
		return false;
	}
	else
		return true;
}

function ck_checkbox(field, str)
{
	if (field.checked==false)
	{
		if (str!="")
		{
			field.focus();
			alert(str);
		}
		return false;
	}
	return true;
}
//to be used with the CCheckBoxArray class - check-box names must be name_0, name_1, name_2 etc. and name_0 deselects the other chk
function deselect_checkbox(field, num_fields)
{
	any_chk = eval(field+"_0");
	if (any_chk.checked == true)
	{
		for (i=1;i<num_fields;i++)
		{
			other_chk = eval((field+"_")+i);
			other_chk.checked=false;
		}
	}
}
// deselects the any chk if this chk gets pressed
function deselect_any_checkbox(field,field_index)
{
	current_chk = eval(field+"_"+field_index);
	any_chk = eval(field+"_0");
	if (current_chk.checked==true)
	{
		any_chk.checked = false;
	}
}

function reset_chk(field,num_fields)
{
	for (i=1;i<num_fields;i++)
	{
		chk = eval((field+"_")+i);
		chk.checked=false;
	}
	any_chk = eval((field+"_")+0);
	any_chk.checked = true;
}

function ck_radio(field, str)
{
	for (i=0;i<field.length;i++)
		if (field[i].checked) return i+1;

	if (str!="")
	{
		alert(str);
		foc(field[0]);
	}
	return false;
}

function validate_email(field, alert_str)
{
	var str = field.value;
//	alert(str);
	if (window.RegExp) {
		var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
		var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[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.select();
		return false;
	} else {
		if(str.indexOf("@") >= 0)
		{
			return true;
		}
		alert(alert_str);
		field.select();
		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;
}


function isEmpty(obj)
{
	obj_type = obj.type;
	if (obj_type == "text" || obj_type == "password" || obj_type == "textarea" || obj_type == "file")	{
		var objValue;

		objValue = obj.value.replace(/\s+$/,"");

		if (objValue.length == 0) {
			obj.focus();
			return true;
		} else {
			return false;
		}
	} else if (obj_type == "select") {
		for (i=0; i < obj.length; i++) {
			if (obj.options[i].selected) {
				if(obj.options[i].value == "") {
					obj.focus();
					return true;
				} else {
					return false;
				}
			}

		}
		return true;
	} else if (obj_type == "radio" || obj_type == "checkbox") {
		if (!obj[0] && obj) {
			if (obj.checked) {
				return false;
			} else {
				obj.focus();
				return true;
			}
		} else {
			for (i=0; i < obj.length; i++) {
				if (obj[i].checked) {
					return false;
				}
			}
			obj[0].focus();
			return true;
		}
	} else {
		return false;
	}
}

function ck_date_format(year, month, day)	//checks the date
{

	if(year==-1 && month==-1 && day==-1)
		return true;

	if(year==-1 || month==-1 || day==-1)
		return false;

	start_date = new Date(year, month-1, day);
	date_year=start_date.getYear();

	if(date_year<2000) date_year=date_year + 1900;

	//alert("Start_date:"+start_date.toString());

	if((start_date.getMonth() != month-1) || (start_date.getDate() != day)  || (date_year != year))
		return false;
	else
		return true;
}

function popUp(URL,width,height)
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+width+",height="+height+"');");
}
function CheckAll()
{
	for (var i=0;i<document.frmMessageList.elements.length;i++) {
		var e = document.frmMessageList.elements[i];
		if (e.name != 'allbox')  e.checked = document.frmMessageList.allbox.checked;
	}
}
function submitMyForm(action,message){
	if(message == 'none'){
		document.forms[0].form_action.value = action;
		document.forms[0].submit();
	}else{
		if(confirm(message)){
			document.forms[0].form_action.value = action;
			document.forms[0].submit();
		}
	}
}

function submitMyForm1(action,message){
	if(message == 'none'){
		document.forms[0].action = action + ".php";
		document.forms[0].submit();
	}else{
		if(confirm(message)){
			document.forms[0].action = action + ".php";
			document.forms[0].submit();
		}
	}
}




function open_window(location, width, height, name) 
{
	var w = width ;
	var h = height;
	var winl = 200;
	var wint = 200;
	
	if (name==null)
		name='popup';

	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl
	myWin = window.open(location, name,'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,'+winprops);
	myWin.focus();
}


function getElementsByClassName(classname, node)  
{
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}