var JS_UTIL = {
	
	sumbit_form: function(id) {
		if (this.validate()) {
			var obj = document.getElementById(id);
			if (obj)
				obj.submit();
		}
		
	},
	validate: function() {
		
		var obj = document.getElementById("username");
		if (obj)
			if (isEmpty(obj.value)) {
				alert("Username is empty.");
				obj.select();
				return false;
			}
		obj = document.getElementById("password");
		if (obj)
			if (isEmpty(obj.value)) {
				alert("Password is empty.");
				obj.select();
				return false;
			}
		return true;
		
	}
}


function isEmpty(objVal){
	objVal = Trim(objVal);
	if ((objVal=='')||(objVal=='@')) 
		return true;
	return false;
}
function Trim(TRIM_VALUE){
		if(TRIM_VALUE.length < 1){
			return"";
		}
		TRIM_VALUE = RTrim(TRIM_VALUE);
		TRIM_VALUE = LTrim(TRIM_VALUE);
		if(TRIM_VALUE==""){
			return "";
		}else{
			return TRIM_VALUE;
		}
	} 

function RTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;

	while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){
		}else{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
	iTemp = iTemp-1;
	} 
	return strTemp;
} 

function LTrim(VALUE){
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space){
		}else{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
	iTemp = iTemp + 1;
	} 
	return strTemp;
}

function isValidEmail(email) {
	
	AtPos = email.indexOf("@")
	StopPos = email.lastIndexOf(".")
	Message = ""
	
	if (email == "") {
		return false
	}
	
	if (AtPos == -1 || StopPos == -1) {
		return false;
	}
	
	if (StopPos < AtPos) {
		return false;
	}
	
	if (StopPos - AtPos == 1) {
		return false;
	} 
	return true;
}
////////////////////////////////////////////////
// validate multiple email separated by comma //
//               by: ricci                    //
////////////////////////////////////////////////
function validate(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   return reg.test(email);
}

function trim(str){
	var	str = str.replace(/^\s\s*/,''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}

function checkAllEmail(email_val){
	var i = 0;
	var stringEmail = email_val.value;
	if(stringEmail.search(',') != -1){
		var emails = email_val.value.split(",");
	}
	else
		var emails = email_val.value.split("/");
	
	
	if(stringEmail != ""){
	
	for(i=0; i<emails.length; i++){
	  if(emails[i]!=""){	
		if(!validate(trim(emails[i]))){			
			alert("Incorrect format: "+emails[i]);
			email_val.value.focus();
			return false;
			break;
		}
	  }
	}
	}
	return true;
}
//////////////////////////////////////////////////////
//	
//	Start updates by Gerby - 20090319
//  Setting and resetting of date select dropdown
//  	to the correct number of days for a given month.
//
//////////////////////////////////////////////////////
function daysInFebruary(year) {
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function daysInNonFebruary(month) {
	// 30 days is September(9) , April(4) , June(6) and November(11)
    // 31 days is January, March, May, July ,August, October and December
    return (((month == 4) || (month == 6) || (month == 9) || (month == 11)) ? 30 : 31);
}

function getCalendarDaysCount(month,year) {
	
	if (month == 2)
		return daysInFebruary(year);
	else		
		return daysInNonFebruary(month);

}

function resetDateSelect(selectyear_id,selectmonth_id,bday_td_id) {
	
	var obj = document.getElementById(selectyear_id);
	if (obj)
		var year = obj.value;
	var obj = document.getElementById(selectmonth_id);
	if (obj)
		var month = obj.value;
	
	day_counter = getCalendarDaysCount(month,year);
		
	var str_bday_day = "<select id='bday_day' name='bday_day' style='width:40px;'>";
		for (var i=1; i <= day_counter; i++)
		str_bday_day += "<option value=\"" + i + "\">" + i + "</option>";
	str_bday_day += "</select>";
	
	obj = document.getElementById(bday_td_id);
	if (obj)
		obj.innerHTML = str_bday_day;
	
}
//////////////////////////////////////////////////////
//	
//	End of updates by Gerby - 20090319
//////////////////////////////////////////////////////
