// JavaScript Document
String.prototype.empty=function(){
var textstr=this; 
if(textstr==''){
return true;
}
return false;
}


String.prototype.trim=function(){
return this.replace(/^\s+/,'').replace(/\s+$/,'');
/*this.replace(/\s+$/,'');
return this;*/
}

function toggleCollapseMenu(menucatid)
{ //$("#"+menugroupid).slideToggle("slow");
	if($("#menugroup_"+menucatid).css('display')=='none'){
		//alert('3');
		$("#menucat_back_"+menucatid).addClass("menuitemactive");
	}else{
		//alert('4');
		$("#menucat_back_"+menucatid).removeClass("menuitemactive");
	}
	//alert('t');
	/*$("#menucat_back_"+menucatid).toggle(function(){
			$(this).addClass("menuitemactive");
			}, function () {
			$(this).removeClass("menuitemactive");
		});*/
	$("#menugroup_"+menucatid).slideToggle("normal", updateCollapseStatus);
	
  /*	$("#"+menugroupid).animate({
      "height": "toggle", "opacity": "toggle"
    }, "slow"); */
	//updateCollapseStatus(menucatid);
}

function updateCollapseStatus(){
	var submenus_container_div_id=$(this).attr('id');
	//var submenus_container_div_id='menugroup_'+menucatid;
	var menucatid=submenus_container_div_id.split('_');
	var menucat_back_div_id='menucat_back_'+menucatid[1];
	
	$.ajax({
	type: "POST",
  	data:"mode=updateMenuCollapsedStatus&mcid="+menucatid[1],
	url: "?",
  	async: false,
	success: function(resp, status){collapseStatusUpdated(resp, menucatid[1]);},
	error:function(){}
	});

}

function collapseStatusUpdated(resp,mcid){
	resp=handleSessionExpiration(resp);
	resp=resp.split('[||]');
	//alert(resp);
	//alert(mcid);
	if(resp[1]==1){ // collapsed
		//$("#menucat_back_"+mcid).removeClass("menuitemactive");
		//alert('Running tests.');
	}else{ // expanded
		//$("#menucat_back_"+mcid).addClass("menuitemactive");
		//alert('Error!');
	}
}

function handleSessionExpiration(resp)
{
		
	/************* Code for checking whether the session has expired **************************************/
	resp=resp.split('[!!||||||!!]');
	if(resp[0]=='SESS_EXPIRED'){ // check if the session has expired
		alert('Due to inactivity the present session has expired.\nPlease login again.');
		location.replace(resp[1]);
		return;	
	}

	return resp[0];
	/******************************************************************************************************/
}

function changeImage(id, img)
{
	$('#'+id).attr('src', 'images/'+img);
}

var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
var keyCode = (isNN) ? e.which : e.keyCode; 
var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
if(input.value.length >= len && !containsElement(filter,keyCode)) {
input.value = input.value.slice(0, len);
input.form[(getIndex(input)+1) % input.form.length].focus();
}

function containsElement(arr, ele) {
var found = false, index = 0;
while(!found && index < arr.length)
if(arr[index] == ele)
found = true;
else
index++;
return found;
}

function getIndex(input) {
var index = -1, i = 0, found = false;
while (i < input.form.length && index == -1)
if (input.form[i] == input)index = i;
else i++;
return index;
}
return true;
}

function isNum(txt)
{
	return txt.match(/^[0-9]+$/);
}

function validateDate(month, day, year)
{
	var month=parseInt(month,10);
	var day=parseInt(day,10);
	var months=new Array('', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); 
	if(year<=0)
		return false
	else if (month<1 || month>12)
		return false
	else if (day < 1)
		return false
	else
	{
		if(month==2)
		{
			if(year % 4 == 0 && year % 100 !=0 || year % 400 == 0)
			{
				if(day>29)
					return false
			}
			else
			{
				if(day>28)
					return false		
			}
		}
		else if ((month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) && day >31)
			return false
		else if(day>30)
			return false		
	}
	return true
}

