/**
 * This class is for JavaScript generator Class.
 *
 * @category 		JavaScript Utilities
 * @package 		SanApp
 * @author			Niyaz Ahamad <niyaz@niyaz.com>
 * @developer		Sanjeev Mishra <mr.sanjeev@gmail.com>
 * @developer		Virender Singh <vir61076@gmail.com>
 * @license 		http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @version 		Release: 1.0.0
 * @link     		http://www.phpdoc.org
 * @link     		http://pear.php.net/PhpDocumentor
 * @todo     		JavaScript Utilities - This class will return the required javascript for the currnet page
*/

function print_todays_date(element_id)
	{
	var d = new Date();
	d=d.toLocaleString();
	getObject(element_id).innerHTML='&nbsp;'+d+'&nbsp;';
	setTimeout("print_todays_date('"+element_id+"')",1000);
	}

function checkAll(frm,checked,chk_box_name)
{
	if((frm[chk_box_name]))
	{
		if(frm[chk_box_name].type=="checkbox")
			frm[chk_box_name].checked = checked;
		else
		{	
		for(var i=0;i<frm[chk_box_name].length;i++)
		{
			var e = frm.items[i];
			if(e.type == "checkbox")
				e.checked = checked;
		}
		}
	}
}

function SetAction(action,frm,chk_box_name)
{
var i=0;
var check=false;

if(getObject(chk_box_name))
	{
			
		if(frm[chk_box_name].length){
		for(i=0;i<frm[chk_box_name].length;i++)
		{
			if(frm.items[i].checked==true){
			check=true;
			break;
			}
		}}else{
	if(frm[chk_box_name].checked == true)
		check=true;
		}
			
if(check==false)
	getObject("Error").innerHTML="Error : Please check any checkbox before continue!<br /><br />";
		else{
	//grid_task a hidden field in Grid Class
	frm.grid_task.value=action;
	actName=action.toLowerCase();
		var conf=cnf('Are you sure to '+actName+' ?');
		if(conf)
			frm.submit();
		}
	}
}

//Open popUpWindow
var pop='';
function openwin(nm,width,height,w_per,h_per)
{
	var name=nm;
	var screen_h=window.screen.height;
	var screen_w=window.screen.width;
	var x_cor=parseInt((w_per/100)*screen_w);
	var y_cor=parseInt((h_per/100)*screen_h);
	if (pop && !pop.closed)
			pop.close();

pop=eval("window.open('"+name+"','NewWIN','chrome[4],toolbar=no,left=5,top=5,width="+width+",height="+height+",directories=no,menubar=no,SCROLLBARS=yes,left='+x_cor+',top='+y_cor+'')");
	
pop.moveTo(x_cor,y_cor);

if (!pop.opener)
	popUpWin.opener = self;
	
if(window.focus)
		pop.focus();
}

function check(frm, fld_arr)
{
	var msg=Array();
     var count=0;
	var whtSpMult = new RegExp("\\s\\s+", "g");
	var str;
	for (var word in fld_arr)
    	{
		str=frm[word].value;
		str = str.replace(whtSpMult, " ");  // Collapse any multiple whites space.
		frm[word].value=trim(str);
		 if(frm[word].value=="")
		 {	
		 if(count==0)
		 	frm[word].focus();
			frm[word].className='txtbox';
			msg[count] = fld_arr[word];
    		count++;
         }
		 else
		{
			frm[word].className='txtbox';
		}
	}
	return msg;
}

function trim(str, chars) 
{
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) 
{
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) 
{
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function display_message(arr_msg)
{	
var msg='Please review the following error message before continue:<br>';
	for (var word in arr_msg){
		msg+="&bull; "+arr_msg[word]+'<br>';
	}
	msg+="<br>";
return msg;
}


/**
 * Function to show and hide the subMenu 
 * @para MenuId di="Name"
 * @param ImageTag <img id="img">
 * @param UpImagePath
 * @param DownImagePath
 * 
 */
function ShowSubMenu(ObjectName, img, UpImage, DownImage) {
	 /**
	  * Store object Name in a variable div or table object name
	  */
	 var div = getObject( ObjectName );
	 /**
	  * Store image object in a variable
	  */
	 var img = getObject( img );
	 /**
	  * Search and get the all image object id in aDiv array
	  */
	 var aDiv = document.getElementsByTagName("img");
	 
	 /**
	  * Run the for loop to the length of aDiv array to match and replace
	  * the src of img object
	  */
	 for(var i=0; i < aDiv.length; i++) {
		  /**
		  * if object is img type
		  */
		  if(aDiv[i].id.indexOf('img') >= 0) {
			  /**
			  * Check it image id and the aDiv id is same
			  */
			  if ( aDiv[i].id == img.id) {
				  /**
				  * Replace the matched image object src with image DonwImage means Open
				  */
				  aDiv[i].src = DownImage;
			  } else {
				  /**
				  * Replace the all unmatched image object src with image UpImage means Close
				  */
					aDiv[i].src = UpImage;
			  }
		}
	 }
 
	 /**
	  * Now Show or hide the div if object is div
	  */
	 if ( div != null ) {
		  if ( div.style.display == "none" ){
			  /**
			  * Show given div
			  */
			  div.style.display="";
			img.src = DownImage;
		  } else {
			  /**
			  * Hide the div
			  */
			  div.style.display="none";
			  img.src = UpImage;
		  }
	 }
 
	 /**
	  * if table object found Show or hide the object 
	  */
	 var aDiv=document.getElementsByTagName("tr");
 
	 /**
	  * Find all visiable submenu tr and hide them if another menu have been clicked
	  */
	 for(var i=0; i<aDiv.length; i++){
	  if(aDiv[i].id != ObjectName && aDiv[i].id.indexOf('menu') >= 0){
		  aDiv[i].style.display="none";
	  }
	 }
}
