/**
 * 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 getObject(objectId)
{
if(document.all && !document.getElementById)
	return document.all(objectId);
else
	return document.getElementById(objectId);
}

function cnf(msg)
{
	return confirm(msg);
}

function toggle(showId)
{
	getObject(showId).style.display = (getObject(showId).style.display=='none' ? 'block' : 'none');
}

function isEmpty(strObj)
{
	if(strObj ? (strObj.type=='text' || strObj.type=='password') : false){
		str=strObj.value;
		var whtSpEnds = new RegExp("^\\s*|\\s*$", "g");
		var whtSpMult = new RegExp("\\s\\s+", "g");

		str = str.replace(whtSpMult, " ");  // Collapse any multiple whites space.
		str = str.replace(whtSpEnds, "");   // Remove leading or trailing white space.

		strObj.value=str;
		return(str.length);
	}
}

function isValidEmail(emailid)	// Email Validation
	{		
	var email=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	return email.test(emailid);
	} 

function isWhiteSpaceOnly(str)	// Cannot check a password type input for whitespace
{
	var re = /^\s+$/
	return (re.test(str))
}

function isValidURL(url_name)  // URL Validation
	{
	var url_format=/^\w+([\.-]?\w+)*(\.\w{2,3})+$/;         
	//var url_format=/^[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}$/;
	return url_format.test(url_name);
	}
	
// Alpha Numeric Validation
function isAlphaNumeric(str)
{
  var re = /[a-zA-Z0-9]/g;
  return (re.test(str));
}

function isNumeric(str) //Numeric Validation
{
  var re =/^[0-9]/g
  return (re.test(str));
}

function isWhiteSpaceOnly(str)	// Cannot check a password type input for whitespace
{
  var re = /[\S]/g
  return (re.test(str))
}

function isIllegalChars(strng)	//illegal characters
{
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
	return (strng.match(illegalChars));
}

//Phone Validation
function isValidPhoneNo(strng)
{
//strip out acceptable non-numeric characters
	var stripped = strng.replace(/[\(\)\.\+\-\ ]/g, '');
	return !isNaN(stripped);
}
function checkAll(frm,checked)
{
	if((frm['items']))
	{
		var e = frm.items;
		if(e.type="checkbox")
			e.checked = checked;
		for(var i=0;i<frm['items'].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)
    {
	if(!frm[word])	//Check the field exists or not
		continue;
	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();
			msg[count] = fld_arr[word];
    		count++;
         }
	}
	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
				  */
			if(DownImage)
				  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="";
	if(DownImage)
		img.src = DownImage;
		  } else {
			  /**
			  * Hide the div
			  */
			  div.style.display="none";
	if(DownImage)
		  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";
	  }
	 }
}


