// JavaScript Document

/* By Mohammad Sajjad Hossain*/
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function confirmDelete(entity) {
	return confirm("Are you sure you want to delete " + entity + "?");
}

function validateTextAreaBlank(txtArea, ctlRealName) {
	if(txtArea.value.length == 0) {
		alert('Please enter "' + ctlRealName + '"');
		txtArea.focus();
		return false;
	}
	
	return true;
}

function validateTextAreaMaxLength(txtArea, ctlRealName, maxLength) {
	len = txtArea.value.length;
	
	if(len > maxLength) {
		alert('Maximum length of "' + ctlRealName + '" exceeds. Maximum length is ' + maxLength);
		return false;
	}
	
	return true;
}

function validateRadioButton(radioBtn, ctlRealName) {
	for(i = 0; i < radioBtn.length; i++) {
		if(radioBtn[i].checked) {
			return true;
		}
	}
	radioBtn[0].focus();
	alert('Please select "' + ctlRealName + '"');
	return false;
}

function validateCheckBox(checkBoxIdPrefix, ctlRealName, totalCheckBoxes) {
	for(i = 0; i < totalCheckBoxes; i++) {		
		if(document.getElementById(checkBoxIdPrefix + "_" + i).checked == true)
		{
			return true;
		}
	}
	document.getElementById(checkBoxIdPrefix + "_0").focus();
	alert('Please select "' + ctlRealName + '"');
	return false;
}

function isValidData(frm)
{
	if(!validateForm(frm))
	{
		return false;
	}
	return validateStandard(frm);
}

function isNumeric(value, errorMessage)
{
	var regex = /^\d+$/;
	
	if (!regex.test(value))
	{
		alert(errorMessage);
		return false;
	}
	else
	{
		return true;
	}
}

function openWindow(url, width, height)
{
	window.open(url, "", "height=" + height + ",width=" + width + "location = 0, status = 1, resizable = 0, scrollbars=1, toolbar = 0");
}

function openResizableWindow(url, width, height)
{
	window.open(url, "", "height=" + height + ",width=" + width + "location = 0, status = 1, resizable = 1, scrollbars=1, toolbar = 0");
}

function refreshParent(url, closeMe)
{
	parent.location = url;
	
	if(closeMe != undefined && closeMe)
	{
		self.close();
	}
}

function pad(number,length) {
    var str = '' + number;
    while (str.length < length)
        str = '0' + str;
    return str;
}

function addOption(selectCtl, optionText, optionValue)
{
	if(optionText.trim() == "" || optionValue.trim() == "")
	{
		alert("Please insert value");
		return false;
	}
	else if(!hasNoInvalidCharacter(optionText))
	{
		alert("Invalid characters (|, ~) found");
		return false;
	}
	
	var optElement = document.createElement("option");
	optElement.value = optionValue;
	optElement.text = optionText;
	optElement.title = optionText;
	selectCtl.options[selectCtl.length] = optElement;
}

function removeOption(selectCtl)
{
	if(selectCtl.options.length > 0)
	{
		index = selectCtl.selectedIndex;
		if(index == -1)
		{
			return false;
		}
		selectCtl.options[index] = null;
	}
}

/*
* Added By: Mohammad Sajjad Hossain
* Duplicates row of a table. Finds the table with table ID
*
* @param targetTableId - Table ID
* @param targetRowIndex - index of the target row tobe duplicated
*/
function duplicateRow(targetTableId, targetRowIndex)
{
	if(targetRowIndex == undefined)
	{
		targetRowIndex = 0;
	}
	
	var targetTable = document.getElementById(targetTableId);
	var tableBody = targetTable.tBodies[0];
	var targetRow = tableBody.getElementsByTagName("tr")[targetRowIndex];
	//var targetRow = tableBody.getElementsByTagName("tr")[tableBody.rows.length - 1];
	var newRow = targetRow.cloneNode(true);
	tableBody.appendChild(newRow);
}


/*
function checking(targetTableId)
{
	var targetTable = document.getElementById(targetTableId);
	var tableBody = targetTable.tBodies[0];
	var targetRow = tableBody.rows[currentRowIndex];
	
	var inputElements = targetRow.getElementsByTagName("input");
	var textAreaElements = targetRow.getElementsByTagName("textarea");
	var selectElements = targetRow.getElementsByTagName("select");
	
	alert( currentRowIndex )
	for(i = 0; i < inputElements.length; i++)
	{
		alert(inputElements[i].name);
	}
	
	for(i = 0; i < textAreaElements.length; i++)
	{
		alert(textAreaElements[i].name);
	}
		
	for(i = 0; i < selectElements.length; i++)
	{
		alert(selectElements[i].name);
	}
}
var currentRowIndex = 1;

function duplicateRow(targetTableId, targetRowIndex)
{
	//if(currentRowIndex == 2)
	checking(targetTableId);
	if(targetRowIndex == undefined)
	{
		targetRowIndex = 0;
	}
	var targetTable = document.getElementById(targetTableId);
	var tableBody = targetTable.tBodies[0];
	var targetRow = tableBody.rows[targetRowIndex];
	var newRow = targetRow.cloneNode(true);

	var inputElements = newRow.getElementsByTagName("input");
	var textAreaElements = newRow.getElementsByTagName("textarea");
	var selectElements = newRow.getElementsByTagName("select");

	for(i = 0; i < inputElements.length; i++)
	{
		if(inputElements[i].type == "radio")
		{
			inputElements[i].name = changeElementName(inputElements[i].name) + "[]";
			inputElements[i].id = changeElementName(inputElements[i].id);
		}
		else
		{
			inputElements[i].name = changeElementName(inputElements[i].name);
			inputElements[i].id = changeElementName(inputElements[i].id);
		}
	}
	for(i = 0; i < textAreaElements.length; i++)
	{
		textAreaElements[i].name = changeElementName(textAreaElements[i].name);
		textAreaElements[i].id = changeElementName(textAreaElements[i].id);
	}
	for(i = 0; i < selectElements.length; i++)
	{
		selectElements[i].name = changeElementName(selectElements[i].name);
		selectElements[i].id = changeElementName(selectElements[i].id);
	}
	
	tableBody.appendChild(newRow);
	currentRowIndex++;
}

function changeElementName(oldName)
{
	var newName = "";
	var indexOfBracketStart = oldName.indexOf("[");
	var indexOfBracketEnd = oldName.indexOf("]");
	
	if(indexOfBracketStart < 0)
	{
		return oldName;
	}
	
	var firstNamePart = oldName.substr(0, indexOfBracketStart + 1);
	//alert("first part = " + firstNamePart);
	
	var secondNamePart = oldName.substr(indexOfBracketEnd);
	//alert("second part = " + secondNamePart);
	
	newName = firstNamePart + currentRowIndex + secondNamePart;
	//alert(newName);
	return newName;
}
*/



/*
* Added By: Mohammad Sajjad Hossain
* Removes row of a table. Finds the table with table ID
*
* @param targetTableId - Table ID
* @param targetRowIndex - index of the target row tobe removed
* @param skipRows - Number of rows to be skipped
*/
function removeRow(targetTableId, targetRowIndex, skipRows)
{
	var targetTable = document.getElementById(targetTableId);
	var tableBody = targetTable.tBodies[0];
	var totalRows = tableBody.rows.length;

	if(totalRows == skipRows)
	{
		return false;
	}
	
	if(targetRowIndex == undefined || targetRowIndex == "")
	{
		targetRowIndex = totalRows - 1;
	}

	if(tableBody.hasChildNodes())
	{
		tableBody.removeChild(tableBody.childNodes[targetRowIndex]);
	}
}


/*
* Used to move options up and down
*
* @param selectCtl the control
* @param up boolean 1 = up 0 = down
*/
function move(selectCtl, up)
{
	var index = selectCtl.selectedIndex;
	var totalOptions = selectCtl.options.length;
	
	if(totalOptions == 0)
	{
		return false;
	}
	
	if(index < 0 && totalOptions > 0)
	{
		alert("Please select an option");
		return false;
	}

	var option1 = selectCtl.options[index];
	var index2 = 0;
	
	if(up == 1)
	{
		if(index == 0)
		{
			return false
		}
		index2 = index - 1;
	}
	else
	{
		if(index == totalOptions - 1)
		{
			return false;
		}
		index2 = index + 1;
	}

	var option2 = selectCtl.options[index2];
	
	var newopt1 = document.createElement("option");
	newopt1.value = option1.value;
	newopt1.text = option1.text;
	newopt1.title = option1.title;
	var newopt2 = document.createElement("option");
	newopt2.value = option2.value;
	newopt2.text = option2.text;
	newopt2.title = option2.title;
	selectCtl.options[index] = newopt2;
	selectCtl.options[index2] = newopt1;
	selectCtl.selectedIndex = index2;
}

function hasNoInvalidCharacter(value)
{
	invalidChars = ["|", "~"];
	
	for(i = 0; i < invalidChars.length; i++)
	{
		if(value.indexOf(invalidChars[i]) != -1)
		{
			return false;
		}
	}
	return true;
}

/**
 * Added By Mohammad Sajjad Hossain
 * For attaching event to a field
 */
var DynaEvent = {
	add: function(obj,type,fn) {
		if (obj.attachEvent) {
			obj['e'+type+fn] = fn;
			obj[type+fn] = function() { obj['e'+type+fn](window.event); }
			obj.attachEvent('on'+type,obj[type+fn]);
		} else
		obj.addEventListener(type,fn,false);
	},
	remove: function(obj,type,fn) {
		if (obj.detachEvent) {
			obj.detachEvent('on'+type,obj[type+fn]);
			obj[type+fn] = null;
		} else
		obj.removeEventListener(type,fn,false);
	}
}

/***********************************************
* Cool DHTML tooltip script-  Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var offsetxpoint=-60 //Customize x offset of tooltip
var offsetypoint=20 //Customize y offset of tooltip
var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
if (ie||ns6)
var tipobj=null;

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ddrivetip(thetext, thecolor, thefont, thefontsize, thefontcolor, thebordercolor, thebordersize, thewidth) {
	tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""
	if (ns6||ie) {
		if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
		if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
		if (typeof thefont!="undefined" && thefont!="") tipobj.style.fontFamily=thefont
		if (typeof thefontsize!="undefined" && thefontsize!="") tipobj.style.fontSize=thefontsize
		if (typeof thefontcolor!="undefined" && thefontcolor!="") tipobj.style.color=thefontcolor
		if (typeof thebordercolor!="undefined" && thebordercolor!="") tipobj.style.borderColor=thebordercolor
		if (typeof thebordersize!="undefined" && thebordersize!="") tipobj.style.borderWidth=thebordersize
		
		tipobj.innerHTML=thetext
		enabletip=true
		return false
	}
}

function positiontip(e){
if (enabletip){
var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
//Find out how close the mouse is to the corner of the window
var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20

var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000

//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<tipobj.offsetWidth)
//move the horizontal position of the menu to the left by it's width
tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
else if (curX<leftedge)
tipobj.style.left="5px"
else
//position the horizontal position of the menu where the mouse is positioned
tipobj.style.left=curX+offsetxpoint+"px"

//same concept with the vertical position
if (bottomedge<tipobj.offsetHeight)
tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
else
tipobj.style.top=curY+offsetypoint+"px"
tipobj.style.visibility="visible"
}
}

function hideddrivetip(){
if (ns6||ie){
enabletip=false
tipobj.style.visibility="hidden"
tipobj.style.left="-1000px"
tipobj.style.backgroundColor=''
tipobj.style.width=''
}
}

document.onmousemove=positiontip
