//Check whether key press in Enter key or not.
function checkEnterKey()
{
	var key=document.layers?e.which:document.all?event.keyCode:e.keyCode;
	if (key == 13)
	{
		//Prevent Browser submit twice.
		event.returnValue=false;
		event.cancelBubble = true;
		return true;
	}
	return false;
}


//Added by CWC, 26/05/2009
//same as checkEnterKey() but this one can be used on ff and chrome.
//will replace checkEnterKey() on coming soon.
function verifyEnterKey(e)
{
	var charCode;
    
    if(e && e.which){
        charCode = e.which;
    }else if(window.event){
        e = window.event;
        charCode = e.keyCode;
    }

    if(charCode == 13) {
    	//Prevent Browser submit twice.
		e.returnValue=false;
		e.cancelBubble = true;
       return true;
    }
    
    return false;
    
}

// common submit Delete
function submitDel(form,actionPath,actionValue,id,alertMsg,errorMsg){
	if(isVariableDefined(alertMsg) && isVariableDefined(form)){
	
		var checked = false;
		if(isVariableDefined(id)){
			checked = true;
		}else {
			for (var i=0;i<form.elements.length;i++){
				var e = form.elements[i];
				if ((e.type == 'checkbox') && (e.checked)){
					checked = true;
					break;
				}
			}
		}
		if(checked){
			if(confirm(alertMsg))
			{
				if(id != "")
					form.id.value = id;
				commonSubmitForm(form,actionPath,actionValue);
			}
		}else {
			alert(errorMsg);
		}
	}
}

// common submit Edit
function submitEdit(form,actionPath,actionValue,id){
	if(isVariableDefined(form)){
		form.id.value = id;
		commonSubmitForm(form,actionPath,actionValue);
	}
}
// common submit Edit with record Status
function submitEditWithRecordStatus(form,actionPath,actionValue,id,recordStatus){
	if(isVariableDefined(form)){
		form.recordStatus.value = recordStatus;
		form.id.value = id;
		commonSubmitForm(form,actionPath,actionValue);
	}
}

// common submit function
function commonSubmitForm(formName, actionPath, actionValue)
{
	formName.action = actionPath;
	formName.actionValue.value = actionValue;
	formName.submit();
}

// common back submit with clear form data
function commonSubmitBack(form, actionPath, actionValue){
	// clear form data
	for (var i=0;i<form.elements.length;i++){
    	var e = form.elements[i];
		if (isVariableDefined(e)){
			if(e.type != "button")
				e.value = "";
		}
	}
	commonSubmitForm(form, actionPath, actionValue);
}

function commonApprovalForm(approvalActionPath,approvalCaseId, approvalActionValue){

	if(approvalActionValue!=null){
	
		window.location = approvalActionPath +"?actionValue="+approvalActionValue+"&sourceTableKey=" + approvalCaseId;
		
	}else{

		window.location = approvalActionPath;
		
	}

}

//common refresh function
function commonRefreshForm(formName)
{
	formName.submit();
}

function checkAll(form, checkParent)
{
   var count = 0;
   for (var i=0;i<form.elements.length;i++)
   {
      var e = form.elements[i];
      if ((e.type == 'checkbox') && (e.name != checkParent.name)) {
        e.checked = checkParent.checked;
        count++;
      }
   }

   return count;
}

function updateCheckAll(form, checkParent, check) {
	var unchecked = false;

	for (var i=0;i<form.elements.length;i++)
	{
    	var e = form.elements[i];

		if ((e.type == 'checkbox') && (e.name != checkParent.name)){
			if (!e.checked) {
				unchecked = true;
			}
		}
	}

	if (unchecked)
		checkParent.checked = false;
	else
		checkParent.checked = true;
}

//toggle drop down menu
function fnToggle(item) {
   obj=document.getElementById(item);
   visible=(obj.style.display!="none")
   //key=document.getElementById("x" + item);
   if (visible) {
     obj.style.display="none";
     //key.innerHTML="<img src='folder.gif' width='16' height='16' hspace='0' vspace='0' border='0'>";
   } else {
      obj.style.display="block";
     // key.innerHTML="<img src='textfolder.gif' width='16' height='16' hspace='0' vspace='0' border='0'>";
   }
}


// check number function -- return true false
function local_isInteger (s)
{
    if (s != "")
    {
    	var dotCount = 0;
    	
	    for (var i = 0; i < s.length; i++)
	    {
	        // Check that current character is number.
	        var c = s.charAt(i);
			if(c != "."){
	        	if (!local_isDigit(c))
	        		return false;
	        }else{
	        	dotCount = dotCount+1;
	        }
	    }
	    
	    if((dotCount > 1) || (dotCount == s.length)){
	    	return false;
	    }
	    
	}

    // All characters are numbers.
    return true;
}

function local_isNumeric(s){
	if (s != "")
    {
	    for (var i = 0; i < s.length; i++)
	    {
	        // Check that current character is number.
	        var c = s.charAt(i);
			if(c != "."){
	        	if (!local_isDigit(c))
	        		return false;
	        }
	    }
	}
    // All characters are numbers.
    return true;
}

function local_isDigit (c)
{   
	return ((c >= "0") && (c <= "9"))
}

// onselect field
function fnSelectField(field){
	selectField = field; 
	field.bgColor='#FFFFFF';
	setTimeout("selectField.select()",100);
}

// js number rounding
function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
  var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  return newnumber.toFixed(rlength);
}

//script to refresh a listing table when record filtering been applied.
//if  aazone is not null then it will be a ajax refresh.
function commonFilter(form, aaZone, filterType){

	form.filterType.value = filterType;
	
	//reset the page no. always start on first page on every type of filter
	form.pageNo.value = 0;
	
	if(aaZone!=null){
		ajaxSubmitForm(form,aaZone);
	}else{
		form.submit();
	}
}

//script to refresh a listing table when record filtering been applied.(custom filter properties & custom filter value)
//if  aazone is not null then it will be a ajax refresh.
//function commonCustomFilter(form, aaZone, filterType,customFilterProp,customFilterValue){
function commonCustomFilter(form, aaZone, filterType,customFilterProp){
	
	if(filterType != null)
		form.filterType.value = filterType;
	else 
		form.filterType.value = "";
	form.customFilterProp.value = customFilterProp;
	//form.customFilterValue.value = customFilterValue;
	
	//reset the page no. always start on first page on every type of filter
	form.pageNo.value = 0;
	
	if(aaZone!=null){
		ajaxSubmitForm(form,aaZone);
	}else{
		form.submit();
	}
}


// js return date in format dd/MM/yyyy or MM/dd/yyyy
function getFormatDate(date,fmt){
	if(isVariableDefined(date) && isVariableDefined(fmt)){
		var tempDate = date.getDate();
		if(isVariableDefined(tempDate)){
			var tempMonth = date.getMonth() + 1;
			var tempYear = date.getFullYear();
			if(tempDate < 10)
				tempDate = "0" + tempDate;
			if(tempMonth < 10) 
				tempMonth = "0" + tempMonth;
			if(fmt == "dd/MM/yyyy"){
				return tempDate+"/"+tempMonth+"/"+tempYear;
			}else if(fmt == "MM/dd/yyyy"){
				return tempMonth+"/"+tempDate+"/"+tempYear;
			}else 
				return date;
		}
	}
}

// Check out whether the variable is defined.
function isVariableDefined(variable){
	try{
		if(null != variable && typeof variable != 'undefined' && variable != ""){
			return true;		
		}else{
			return false;
		}
	}catch(err){
		return false;
	}
}

//counter for a textarea
function counterUpdate(opt_countedTextBox, opt_countBody, opt_maxSize) {
        var countedTextBox = opt_countedTextBox ? opt_countedTextBox : "counttxt";
        var countBody = opt_countBody ? opt_countBody : "countBody";
        var maxSize = opt_maxSize ? opt_maxSize : 1024;

        var field = document.getElementById(countedTextBox);

        if (field && field.value.length >= maxSize) {
        	field.value = field.value.substring(0, maxSize);
        }
        
        var txtField = document.getElementById(countBody);
        
        if (txtField) { 
        
        	txtField.innerHTML = field.value.length;
        }
}


/** a function to set value into the specified field.
* @Params 1 : formField, the pass in param's value should be "document.urFormName.urFieldName".
* @Parmas 2 : valToSet, the value to be set into the "formField"
* @ set "" when params2(valToSet) is not defined.
* @ error orrur when params1(formField) is not defined.
* by Kesler
*/
function setFormFieldVal(formField,valToSet){
	if(isVariableDefined(valToSet))
		formField.value = valToSet;
	else
		formField.value = "";
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function getSelectedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.selected)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].selected) {
			return radioObj[i].value;
		}
	}
	return "";
}

/**
*
* Add by @CWC@
* Form Reset: reset form before ajax form been submitted to server controller
*/
function resetForm(myForm, actionValue){
	try{	
	
		//myForm.reset();
		// reset filer bar.(A...Z) 
		/*if(null != myForm.stFilter && typeof myForm.stFilter != 'undefined' && myForm.stFilter != ""){
			myForm.stFilter.value = "";
		}*/
		//ajaxSubmitForm(myForm,zoneVal);
		
		myForm.actionValue.value = actionValue;
		myForm.submit();

	}catch(err){
		alert("Form Reset Error! " + err);
	}
}

