// used for form field checking
function setElementValues(c_ele)
{
	if(!c_ele) return;
		
	// now the text output
	error_div = document.getElementById(c_ele.name+"_error");
	if(error_div)
		error_div.className = "error";
	
	c_ele.focus();
	c_ele.scrollIntoView("true");
	
	gen_error_div = document.getElementById("general_error_info");
	if(gen_error_div) {
		gen_error_div.style.visibility = "visible";
		gen_error_div.style.display = "block";
	}
}

function resetElementValues(c_ele)
{
	if(!c_ele) return;

	error_div = document.getElementById(c_ele.name+"_error");
	if(error_div)
		error_div.className = "";
	
	gen_error_div = document.getElementById("general_error_info");
	if(gen_error_div) {
		gen_error_div.style.visibility = "hidden";
		gen_error_div.style.display = "none";
	}
}

function setFocus() {
  if (document.forms.length > 0) {
    var field = document.forms[0];
    for (i=0; i<field.length; i++) {
      if ( (field.elements[i].type != "image") &&
           (field.elements[i].type != "hidden") &&
           (field.elements[i].type != "reset") &&
           (field.elements[i].type != "submit") ) {

        document.forms[0].elements[i].focus();

        if ( (field.elements[i].type == "text") ||
             (field.elements[i].type == "password") )
          document.forms[0].elements[i].select();

        break;
      }
    }
  }
}

function rowOverEffect(object) {
	if(object.className == 'dataTableRow')
		object.className = 'dataTableRowOver';
}

function rowOutEffect(object) {
	if(object.className == 'dataTableRowOver')
		object.className = 'dataTableRow';
}

// check if value is numeric (exception for not available string: na)
function is_numeric(num_string)
{
	valid_chars = new Array("0","1","2","3","4","5","6","7","8","9",".","n","a");
	
	for(i=0; i < num_string.length; i++) {
		var x=0;
		for (; x < valid_chars.length; x++) {
			if(num_string.charAt(i) == valid_chars[x])
				break;
		}
		if(x >= valid_chars.length)
			return false;
	}
	return true;
}
// check if value is alphanumeric
function isAlphaNumeric(str, extended)
{
	chars = "[^A-Za-z0-9]";
	if(extended)
		chars = "[^A-Za-z0-9@._-]";
		
	var charpos = str.search(chars); 
	if(str.length > 0 && charpos >= 0)
		return false; 

	return true;
}

// non-digit characters which are allowed in phone numbers
var phoneDelimiters = "()- +";

function isInteger(s)
{
    for(i = 0; i < s.length; i++) {   
        // Check that current character is number.
        var c = s.charAt(i);
        if((c < "0") || (c > "9"))
			return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for(i = 0; i < s.length; i++) {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if(bag.indexOf(c) == -1)
			returnString += c;
    }
    return returnString;
}

function checkPhoneNumber(strPhone)
{
	s = stripCharsInBag(strPhone, phoneDelimiters);
	return (isInteger(s) && s.length > 5);
}

function checkEmail(strEmail)
{
	if(!strEmail || strEmail.length < 8)
		return false;
		
	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(strEmail))
		return true;
	else return false;
}

function checkLogin(strLogin)
{
	if(strLogin.length < 8)
		return false;
		
	return isAlphaNumeric(strLogin, true);	
}

function checkPassword(strPassword)
{
	if(strPassword.length < 8)
		return false;
		
	return isAlphaNumeric(strPassword, false);	
}