/********************************************************************
Function Name : onlynumbers()
Description   : Check for Only Numeric value entry
Created By    : Prakash Ranjan Satpathy
Date Created  : 16th Feb 2010(16/02/2010)
*********************************************************************/
function onlynumbers(e)
{
    var unicode = e.charCode ? e.charCode : e.keyCode;
    if( unicode != 8 )
    {
        if(unicode < 9 || unicode > 9 && unicode < 48 || unicode > 57) 
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    else
    {
        return true;
    }
}
/********************************************************************
Function Name : validateEmail()
Description   : Check for Email Validation
Created By    : Prakash Ranjan Satpathy
Date Created  : 16th Feb 2010(16/02/2010)
*********************************************************************/
function validateEmail(email)
{
	if(document.getElementById(email).value != "")
	{
		var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		str = document.getElementById(email).value;
		if(!str.match(emailRegEx))
		{
			alert("Invalid E-Mail Id!");
			document.getElementById(email).select();
		}
	}
}
/*********************************************************************************
Function Name : validateMobileNo()
Description   : Mobile No Validation
Created By    : Prakash Ranjan Satpathy
Date Created  : 16th Feb 2010(16/02/2010)
*********************************************************************************/
function validateMobileNo(mobileNo)
{
	letters = document.getElementById(mobileNo).value;
	let = new Array(letters);
	var len = letters.length;
	if(len > 10)
	{
		var a = letters.substr(0,10);
		alert("Invalid 'Mobile No.' ! Can not exceed '10' digit");
		document.getElementById(mobileNo).value = a;
	}
	if(len == 1 && let[0] != 9 && let[0] != 8 && let[0] != 7)
	{
		alert("'Mobile No' should start with '9' or '8' or '7'");
		document.getElementById(mobileNo).select();
	}

}
/*********************************************************************************
Function Name : logInformValidate(), logInFocus()
Description   : Login Form Validation & Focus on UserId
Created By    : Prakash Ranjan Satpathy
Date Created  : 16th Feb 2010(16/02/2010)
*********************************************************************************/
function logInformValidate(userid,password)
{
	var uname = document.getElementById(userid).value;
	var pass = document.getElementById(password).value;
	if(uname == "")
	{
		alert("Enter your 'Username'");
		document.getElementById(userid).focus();
		return false;
	}
	else if(pass == "")
	{
		alert("Enter your 'Password'");
		document.getElementById(password).focus();
		return false;
	}
	
	
}
function logInFocus(userid)
{
	document.getElementById(userid).focus();
}

/*********************************************************************************
Function Name : checkExcelFileOnly()
Description   : Validate Blank entry of File Upload
Created By    : Prakash Ranjan Satpathy
Date Created  : 16th Feb 2010(16/02/2010)
*********************************************************************************/
function checkExcelFileOnly(id)
{
	var fup = document.getElementById(id);
	var fileName = fup.value;
	var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
	if(ext == "jpg" || ext == "JPG" || ext == "gif" || ext == "GIF" || ext == "jpeg" || ext == "JPEG" || ext == "bmp" || ext == "BMP" || ext == "png" || ext == "PNG" || ext == "tif" || ext == "TIF")
	{
		return true;
	}
	else
	{
		alert("Invalid input file format ! Should be in '.jpg' or '.jpeg' or '.gif' or '.bmp' or '.png' or '.tif'");
		document.getElementById(id).value="";
		fup.focus();
		return false;
	}
}
function departmentPupUp(depId)
{
	var dep = document.getElementById(depId).value;
	if(dep == "new")
	{
		window.open("department_popup.php","Window1","resizable=no,menubar=no,status=no,toolbar=no,scrollbars=no,location=0, directories=0,dependant=yes,dialog=yes,modal=yes,unadorned=yes,width=700,height=250,location=center");
	}
}
/*********************************************************************************
Function Name : changeColor(), defaultColor()
Description   : Change Row color in View
Created By    : Prakash Ranjan Satpathy
Date Created  : 24th Feb 2010(24/02/2010)
*********************************************************************************/
function changeColor(a,b,c)
{
	document.getElementById(c).style.backgroundColor=b;
}
function defaultColor(a,b,c)
{
	document.getElementById(c).style.backgroundColor=a;
}
function popupImage(id)
{
	window.open("image_full_popup.php?id="+id,"Window1","toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=700,height=600,location=center");
}
function showHideBox(a,b)
{
	document.getElementById(a).style.display='none';
	document.getElementById(b).style.display='block';
}
function showHideBox_back(a,b)
{
	document.getElementById(a).style.display='block';
	document.getElementById(b).style.display='none';
}
/***************************************************************
Function Name : checkNode()
Description   : Timesheet View & Approved
Created By    : Prakash Ranjan Satpathy
Date Created  : 8th March 2010(08/03/2010)
***************************************************************/
if(!window.Node)
{
	var Node = {ELEMENT_NODE : 1, TEXT_NODE : 3};
}
function checkNode(node, filter)
{
	return (filter == null || node.nodeType == Node[filter] || node.nodeName.toUpperCase() == filter.toUpperCase());
}
function getChildren(node, filter)
{
	var result = new Array();
	if(node)
	{
		var children = node.childNodes;
		for(var i = 0; i < children.length; i++)
		{
			if(checkNode(children[i], filter)) result[result.length] = children[i];
		}
		return result;
	}
}
function getChildrenByElement(node)
{
	return getChildren(node, "ELEMENT_NODE");
}
function getFirstChild(node, filter)
{
	var child;
	var children = node.childNodes;
	for(var i = 0; i < children.length; i++)
	{
		child = children[i];
		if(checkNode(child, filter)) return child;
	}
	return null;
}
function getFirstChildByText(node)
{
	return getFirstChild(node, "TEXT_NODE");
}
function getNextSibling(node, filter)
{
	for(var sibling = node.nextSibling; sibling != null; sibling = sibling.nextSibling)
	{
		if(checkNode(sibling, filter)) return sibling;
	}
	return null;
}
function getNextSiblingByElement(node)
{
	return getNextSibling(node, "ELEMENT_NODE");
}
var activeMenu = null;

function showMenu()
{
	if(activeMenu)
	{
		activeMenu.className = "";
		getNextSiblingByElement(activeMenu).style.display = "none";
	}
	if(this == activeMenu)
	{
		activeMenu = null;
	}
	else
	{
		this.className = "active";
		getNextSiblingByElement(this).style.display = "block";
		activeMenu = this;
	}
	return false;
}
function initMenu()
{
	var menus, expandmenu, text, a, i;
	menus = getChildrenByElement(document.getElementById("expandmenu"));
	if(menus)
	{
		for(i = 0; i < menus.length; i++)
		{
			menu = menus[i];
			text = getFirstChildByText(menu);
			a = document.createElement("a");
			menu.replaceChild(a, text);
			a.appendChild(text);
			a.href = "#";
			a.onclick = showMenu;
			a.onfocus = function(){this.blur()};
		}
	}
	
}
if(document.createElement) window.onload = initMenu;
/******************************************************************
Function Name : ShowBox(), HideBox()
Description   : Slide Div Open Downwords
Created By    : Prakash Ranjan Satpathy
Date Created  : 9th March 2010(09/03/2010)
Attached CSS  : style.css (#cover_div, #open_box)
********************************************************************/
var hh=0;
var inter;
function ShowBox()
{
	document.getElementById('add').disabled=true;
	document.getElementById('add').value='Adding New...';
	document.getElementById('sp1').style.display='block';
	document.getElementById('sp2').style.display='none';
	if(hh==440)
	{
		document.getElementById('sp2').style.display='block';
		document.getElementById('sp1').style.display='none';
		document.getElementById('add').disabled=false;
		document.getElementById('add').value='Add New';
		clearInterval(inter);
		return;
	}
	
	obj = document.getElementById("cover_div");
	
	obj.style.display = 'block';
	hh+=40;
	obj.style.height = hh + 'px';
	
}
function HideBox()
{
	document.getElementById('exit').disabled=true;
	document.getElementById('exit').value='Exit Add New...';
	document.getElementById('sp1').style.display='none';
	document.getElementById('sp2').style.display='block';
	obj = document.getElementById("cover_div");

	if(hh==40)
	{
		document.getElementById('sp1').style.display='block';
		document.getElementById('sp2').style.display='none';
		document.getElementById('exit').disabled=false;
		document.getElementById('exit').value='Exit Add New';
		obj.style.display = 'none';
		obj.style.height = '0.1em';
		clearInterval(inter);
		return;
	}
	hh-=40;
	obj.style.height = hh + 'px';
}
function resizeTextarea(id)
{
	var len = document.getElementById(id).value.length;
	if(len > 35)
	{
		var y = (document.getElementById(id).value.length / 15);
		document.getElementById(id).rows = y+1;
	}
}
function resizeTextareaSmall(id)
{
	var len = document.getElementById(id).value.length;
	if(len > 12)
	{
		var y = (document.getElementById(id).value.length / 12);
		document.getElementById(id).rows = y+1;
	}
}
function funActiveUpdate(a,b)
{
	document.getElementById(a).disabled=false;
	document.getElementById(b).style.display='none';
}
function chkPassStrength(pasid,span)
{
	var pas = document.getElementById(pasid).value;
	var patt1 = /@/;
	var alt1 = pas.match(patt1);
	var patt2 = /_/;
	var alt2 = pas.match(patt2);
	var patt3 = /!/;
	var alt3 = pas.match(patt3);
	var patt4 = /$/;
	var alt4 = pas.match(patt4);
	if(pas != "")
	{
		if(pas.length <= 5)
		{
			var strBar = "<table width='120px' cellspacing='0' cellpadding='0'><tr height='5px' bgcolor='#D2D2D2'><td></td></tr><table>";
			var echoStr = "<span style='color:#676767'>Too short"+strBar+"</span>";
			document.getElementById(span).innerHTML = echoStr;
		}
		else if(pas.length > 5 && pas.length <= 9)
		{
			if(alt1 == "@" || alt2 == "_" || alt3 == "!" || alt4 == "$")
			{
				if(pas.length > 6)
				{
					var strBar = "<table width='120px' cellspacing='0' cellpadding='0'><tr height='5px' bgcolor='#008000'><td></td></tr><table>";
					var echoStr = "<span style='color:#008000'>Strong"+strBar+"</span>";
					document.getElementById(span).innerHTML = echoStr;
				}
			}
			else
			{
				var strBar = "<table width='120px' cellspacing='0' cellpadding='0'><tr height='5px'><td bgcolor='#AA0033' width='50%'></td><td bgcolor='#D2D2D2' width='50%'></td></tr><table>";
				var echoStr = "<span style='color:#AA0033'>Weak"+strBar+"</span>";
				document.getElementById(span).innerHTML = echoStr;
			}
		}
		else if(pas.length > 9)
		{
			var strBar = "<table width='120px' cellspacing='0' cellpadding='0'><tr height='5px' bgcolor='#008000'><td></td></tr><table>";
			var echoStr = "<span style='color:#008000'>Strong"+strBar+"</span>";
			document.getElementById(span).innerHTML = echoStr;
		}
	}
	else
	{
		document.getElementById(span).innerHTML = '';	
	}
}
function checkPasswordMatch(a,b,c)
{
	var pass = document.getElementById(c).value;
	if(pass != "")
	{
		var pass1 = document.getElementById(a).value;
		var pass2 = document.getElementById(b).value;
		if(pass1 == "")
		{
			alert("Enter new password!");
			document.getElementById(a).focus();
			return false;
		}
		else if(pass2 == "")
		{
			alert("Re-type new password!");
			document.getElementById(b).focus();
			return false;
		}
		else if(pass1 != "" && pass2 != "")
		{
			if(pass1 != pass2)
			{
				alert("Password did not match!");
				document.getElementById(b).focus();
				document.getElementById(b).select();
				return false;
			}
		}
	}
	else
	{
		alert("Enter current password!");
		document.getElementById(c).focus();
		return false;
	}
}
function validateForm(txa,sel,date,typ)
{
	var t = document.getElementById(txa).value;
	var s = document.getElementById(sel).value;
	if(t == "")
	{
		alert("'Description' should not be blank!");
		document.getElementById(txa).focus();
		return false;
	}
	else if(s == "")
	{
		alert("'Full Day/Half Day/Leave' should not be blank!");
		document.getElementById(sel).focus();
		return false;
	}
	else if(t != "" && s != "")
	{
		if(typ == "post")
		{
			var conf = confirm("Are you sure you want to post Timesheet for '"+date+"' ?")
		}
		else if(typ == "update")
		{
			var conf = confirm("Are you sure you want to update Timesheet for '"+date+"' ?")
		}
		if(conf == false)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}
function validatePost(mon,yy,today_m,today_y)
{
	var month = document.getElementById(mon).value;
	var year = document.getElementById(yy).value;
	
	if(month > today_m && year >= today_y)
	{
		alert("Sorry, you cannot post Timesheet in advance!");
		document.getElementById(mon).focus();
		return false;
	}
	else if(year > today_y)
	{
		alert("Sorry, you cannot post Timesheet in advance!");
		document.getElementById(yy).focus();
		return false;
	}
}
function makeConfirm(name,type)
{
	if(type == "resetPass")
	{
		var conf = confirm("Are you sure you want to reset password for '"+name+"' ?")
	}
	else if(type == "remove")
	{
		var conf = confirm("Are you sure you want to delete information for '"+name+"' ?")
	}
	else if(type == "editTimesheet")
	{
		var conf = confirm("Are you sure you want to edit Timesheet for '"+name+"' ?")
	}
	else if(type == "delEmpImg")
	{
		var conf = confirm("Are you sure you want to delete photo of '"+name+"' ?")
	}
	
	if(conf == false)
	{
		return false;
	}
	else
	{
		return true;
	}
}
