/* $Id: common.js,v 1.1 2002/10/02 18:49:42 shaggy Exp $ */

/*
Copyright (c) 2001, 2002 by Martin Tsachev. All rights reserved.
http://www.mtweb.org

Redistribution and use in source and binary forms,
with or without modification, are permitted provided
that the conditions available at
http://www.opensource.org/licenses/bsd-license.html
are met.
*/

function validateRequired(str, warning, min, max) {
//for (i=0;i<document.rjform.elements.length;i++)	{
//	name = document.rjform.elements[i].id;
//	alert(document.rjform.elements[name].name + '\n' + document.rjform.elements[name].id + '\n' + document.rjform.elements[name].value + '\n' + document.rjform.elements[name].id + '\n' + document.rjform.elements[i].type);
//	alert(document.rjform.elements[i].name + '\n' + document.rjform.elements[i].id + '\n' + document.rjform.elements[i].value + '\n' + document.rjform.elements[i].id + '\n' + document.rjform.elements[i].type);
//}
//alert(str + '\n' + str.name + '\n' + str.type + '\n' + unescape(warning));
	if (str.type == 'text' || str.type == 'textarea' || str.type == 'file' || str.type == 'password')	{
		return validateRequiredText(str, warning, min, max);
	}	else if (str.type == 'select-one')	{
		return validateRequiredSelectOne(str, warning);
	}	else if (str.type == 'select-multiple')	{
		return validateRequiredSelectMultiple(str, warning);
	}	else if (str.type == 'radio' || str.type == 'checkbox' || str[0].type == 'radio' || str[0].type == 'checkbox')	{
		return validateRequiredChecked(str, warning);
	}
	return true;
}

function validateRequiredText(str, warning, min, max) {
	if (!min) { min = 1; }
	if (!max) {	max = 65535; }

	if (!str.value || str.value.length < min || str.value.length > max) {
		alert(unescape(warning));
		str.focus();
		str.select();
		return false;
	}
	return true;
}

function validateRequiredSelectOne(str, warning) {
	var si = str.selectedIndex;
	if (str.value != '' && si >= 0) {
		return true;
	}
	alert(unescape(warning));
	return false;
}

function validateRequiredSelectMultiple(str, warning) {
	var numOptions = str.options.length;
	lastSelected=-1;
	for(loop=numOptions-1;loop>=0;loop--) {
		if(str.options[loop].selected) {
			lastSelected = loop;
			value = str.options[loop].value;
			break;
		}
	}
	if(lastSelected < 0 || trim(value).length == 0) {
		alert(unescape(warning));
		return false;
	}
	return true;
}

function validateRequiredChecked(str, warning)	{
	isChecked=-1;
	if (!isArray(str))	{
//alert(str.name + '\n' + str.type);
		if (str.checked) {
			isChecked=1;
		}
	}	else	{
		for (loop=0;loop < str.length;loop++) {
			alert(str[loop].checked);
			if (str[loop].checked) {
				isChecked=loop;
				break; // only one needs to be checked
			}
		}
	}
	if (isChecked < 0) {
		alert(unescape(warning));
		return false;
	}
	return true;
}



function validateEmail(email) {
	if (!email.value) {
		return true;
	}

	var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
	if (!re_mail.test(email.value)) {
		alert('Incorrect email address');
		email.focus();
		email.select();
		return false;
	}
	return true;
}

function validateMatch(var1, var2, msg) {
	if (var1.value != var2.value) {
		alert(unescape(msg));
		var1.focus();
		var1.select();
		return false;
	}
	return true;
}


function validateNumeric(str,  warning)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;
   var strString = str.value;

   if (strString.length == 0) {
		blnResult = false;
   }

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   if (blnResult)	{
		return true;   
   }	else	{
		alert(unescape(warning));
		str.focus();
		str.select();
		return false;
   }
}


//chk if an object is an array or not.
function isArray(obj) {
	//returns true is it is an array
	if (obj.constructor.toString().indexOf('Array') == -1)	{
		return false;
	}	else	{
		return true;
	}
}