
var GmessageControlId = "message";
var GmessageContainerControlId = "messageContainer";
var GdateFormatString = "mm/dd/yyyy";
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
function entsub(myform) {
  if (window.event && window.event.keyCode == 13)
    return false;
  else
    return true;
}

function showMessage(msg) {
  if ( msg == null || msg == "" )
    return;

  var ctrlMsgContainer = document.getElementById(GmessageContainerControlId);
  if ( ctrlMsgContainer == null ) {
    alert("There no no control " + GmessageContainerControlId);
    return;
  }

  var ctrlMsg = document.getElementById(GmessageControlId);
  if ( ctrlMsg == null ) {
    alert("There no no control " + GmessageControlId);
    return;
  }

  ctrlMsg.innerHTML = msg;
  ctrlMsgContainer.style.display = "block";
}
function hideMessage() {
  var ctrlMsgContainer = document.getElementById(GmessageContainerControlId);
  if ( ctrlMsgContainer == null )
    return;
  ctrlMsgContainer.style.display = "none";
}
/* pop up window */
function popup(link, windowName, width, height) {
  var property = 'width=' + width + ',height=' + height + ',status=no';
  window.open(link, windowName, property);
}

/* Center windows */
function centerScreen() {
  if ( navigator.userAgent.indexOf("Gecko") > 0 ) {
    centerScreenGecko();
  } else {
    centerScreenIE();
  }
}
function centerScreenIE() {
  var w = window.document.body.offsetWidth + 6;
  var h = window.document.body.offsetHeight + 25;
  var W = screen.availWidth;
  var H = screen.availHeight - 20;
  window.moveTo((W - w) / 2, (H - h) / 2);
}
function centerScreenGecko() {
  var w = window.outerWidth;
  var h = window.outerHeight;
  var W = screen.availWidth;
  var H = screen.availHeight - 20;
  window.moveTo((W - w) / 2, (H - h) / 2);
}

function isInteger(s){
	var i;
  for (i = 0; i < s.length; i++){
    var c = s.charAt(i);
    if (((c < "0") || (c > "9"))) return false;
  }
  return true;
}
function trim(str) {
  return str.replace(/^\s*|\s*$/g,"");
}


/*****************************************************************************/
function checkAll(form, containerName) {
    for (var i = 0; i < form.elements.length; i++) {
        var element = form.elements[i];
        if (element.type == "checkbox" && !element.disable && element.name.indexOf(containerName) != -1) {
            element.checked = true;
        }
    }
}

function uncheckAll(form, containerName) {
    for (var i = 0; i < form.elements.length; i++) {
        var element = form.elements[i];
        if (element.type == "checkbox" && !element.disable && element.name.indexOf(containerName) != -1) {
            element.checked = false;
        }
    }
}

function confirmActionOnItemsChecked(form, containerName, actionName, selectedItems) {
    var isAnyChecked = false;
    for (var i=0; i<form.elements.length; i++)
    {
        var element = form.elements[i];
        if (element.type == "checkbox" && element.name.indexOf(containerName) != -1 && element.checked == true) {
            isAnyChecked = true;
        }
    }
    if (isAnyChecked == false) {
        alert("Error! No item was selected. Please choose item(s).");
        return false;
    }
    else {
        return confirmAction(actionName, selectedItems);
    }
}

function confirmAction(actionName, selectedItems) {
    return confirm("Are you sure to " + actionName + " the item(s)?");
}

function onShowSummary(summary, validators) {
	var messages = [];
	var hilightClass = "textBoxError";
	validators.each(function(validator) {
		var controlToValidate = $(validator.options.ControlToValidate);
		var className = controlToValidate.className;
		if (!validator.evaluateIsValid()) {
			if (className.indexOf(hilightClass) == -1) {
				controlToValidate.className = className + " " + hilightClass;
			}
		}

		var message = validator.getErrorMessage();
		if(typeof(message) == 'string' && message.length > 0) {
			messageInfo = message.split('|');
			found = false;
			for(i = 0; i < messages.length; i++) {
				if (messages[i]['Message'] == messageInfo[0]) {
					found = true;
					break;
				}
			}
			if (!found) {
				messages.push({Message: messageInfo[0], Parameters: []});
				i = messages.length - 1;
			}

			if (messageInfo.length > 1) {
				messages[i]['Parameters'].push(messageInfo[1]);
			}
		}
	});

	messageArray = [];
	for (i = 0; i < messages.length; i++) {
		var messageText = messages[i]['Message'] + messages[i]['Parameters'].join(', ');
		messageArray.push(messageText);
	}

	summary.updateHTMLMessages(messageArray);
	summary.messages.show();
	window.scroll(0, findPosY(document.getElementById(summary.options["ID"])) - 20);
	return false;
}

/* trim function  */
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;
	}
} //End Function

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;
		} //End While
	return strTemp;
} //End Function

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;
	} //End While
	return strTemp;
} //End Function


/**############################################3/
/*script using in list page*/


function parentCheckBoxOnclick(parentForm, status, subCheckBoxName)
{
    for (var i=0; i<parentForm.elements.length; i++) {
        if(parentForm.elements[i].type == "checkbox" && parentForm.elements[i].name == subCheckBoxName) {
           parentForm.elements[i].checked = status;
        }
    }
}

function subCheckBoxOnClick(subCheck, parentCheckBox)
{
    if (subCheck.checked == false)
    {
        parentCheckBox.checked = false;
    }
    else
    {
        status = true;
        subChecks = document.getElementsByName(subCheck.name);
        for (i=0; i<subChecks.length; i++)
        {
            if (subChecks[i].checked == false)
            {
                status = false;
                break;
            }
        }
        parentCheckBox.checked = status;
    }
}

function checkSubCheckBoxIsChecked(form, subCheckBoxName, actionName)
{

    var isAnyChecked = false;
    for(var i=0; i<form.elements.length; i++)
        if( form.elements[i].type == "checkbox" &&
            form.elements[i].name == subCheckBoxName   &&
            form.elements[i].checked == true)
            {
                isAnyChecked = true;
            }
    if (isAnyChecked == false)
    {
        alert("Please choose item(s).");
        return false;
    }
    else
    {
        return confirm("Are you sure you want to " + actionName + " selected item(s)?");
    }
}

function getWindowHeight() {
	var windowHeight=0;
	if (typeof(window.innerHeight)=='number') {
		windowHeight=window.innerHeight;
	}
	else {
		if (document.documentElement&&document.documentElement.clientHeight) {
			windowHeight=document.documentElement.clientHeight;
		}
		else {
			if (document.body&&document.body.clientHeight) {
				windowHeight=document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function setFooter()
{
	var windowHeight = getWindowHeight();
	var contentHeight=document.getElementById('content').offsetHeight;
	if (windowHeight > contentHeight + 135)
	{
		document.getElementById('footer').style.position = "absolute";
	}
}
/* Common Script */
/* Nguyen Anh Tuan */
/* June 01st, 2007 */

//function process Ajax with response data
function updateContent(updatedItem, url){
	new Ajax.Request(
		url,
		{
			method:'get',
			onSuccess: function(transport){
				Element.update(updatedItem , transport.responseText);

			},
			onFailure: function(){
				alert('Something went wrong...')
			}
		});
}//endof function


/* pop up window
 * @author: anhtuan.nguyen
 * @date: Nov 20th, 2006
 * @reference from: http://nate.malamud.com/tinmen/win.js
*/
function showPopupWin(thiswidth,thisheight,thisURL) {
   var theWIN;
   var thisWIN;
   var thisURL;
   var thiswidth;
   var thisheight;
   thisWIN = self;
   if (thisWIN.name == "theWIN") { theWIN = thisWIN }
   if (!theWIN || theWIN.closed) {
      theWIN = window.open(thisURL,"theWIN","toolbar=0,location=0,directories=0,status=0,scrollbars=yes,resizable=yes,copyhistory=0 ,width=" + thiswidth + ",height=" + thisheight + ",top=10,left=10,screeny=25,screenx=50");
   } else {
      theWIN.location.href = thisURL ;
      theWIN.resizeTo(thiswidth,thisheight) ;

   }
   theWIN.focus() ;
}
function image_open(image_loc)
{
	  HTML = "<html><style>body{margin:0px 0px 0px 0px; text-align:center; text-valign:middle}</style><body onBlur='top.close()' ><img src='"+ image_loc +"' border=0 name=load_image onLoad='window.resizeTo(document.load_image.width+0,document.load_image.height+40);window.moveTo(parseInt(window.screen.availWidth)/2-parseInt(document.load_image.width+0)/2,parseInt(window.screen.availHeight)/2-parseInt(document.load_image.height+0)/2)'></body></html>";
	  popupImage = window.open('','_blank','toolbar=no,scrollbars=no');
	  popupImage.document.open();
	  popupImage.document.write(HTML);
	  popupImage.document.close();
}

/** function to correct value incase of invalided*/
function correctData(obj){
		//Remove special characters from a string.
		//strExp = /\$|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\-|\_|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$|\./g;
		//Remove spaces
		obj.value = obj.value.replace(/^\s*|\s*$/g,"");
		//obj.value = obj.value.replace(strExp, "");
}//endof function


