
function objectNumKeys(obj) 
{
    var count = 0;
    for(var prop in obj)
    {
        count++;
    }
    return count;
}

//has been written to prevent grid updating erro
//I dont know will work or not
 function updateStoreValue(store,id,field,new_val,old_val)
{	
	if(store.getById(id))
	{
		store.getById(id).set(field, new_val);
	}
	else
	{
		var index = store.indexOfId(id);
		if(index > 0)
		    store.getAt(index).set(field, new_val);
	}
}

//whether a function or a variable is declared
/*function isDeclared(param1)
{	
   if(typeof(eval(param1)) != "undefined")
    return true;
   else
    return false;
}
*/

function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

//string padding
String.prototype.pad = function(l, s, t){
    return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
        + 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
        + this + s.substr(0, l - t) : this;
};

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
};

String.prototype.replaceAll=function(s1, s2) {return this.split(s1).join(s2)};


//string trim
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}

////// this section of is to set the div element or element visibility

    //just doesnt display the content does not removes from the layaot
	function toggleVisibility(szDivID, iState) // 1 visible, 0 hidden . 
	{
		try{
		   var obj = document.layers ? document.layers[szDivID] :
		   document.getElementById ?  document.getElementById(szDivID).style :
		   document.all[szDivID].style;
		   obj.visibility = document.layers ? (iState ? "show" : "hide") :
		   (iState ? "visible" : "hidden");
		}catch(ex){}
	}	
	var browserType;
	if (document.layers) {browserType = "nn4"}
	if (document.all) {browserType = "ie"}
	if (window.navigator.userAgent.toLowerCase().match("gecko")) {
	 browserType= "gecko"
	}
	
	//if you have an array of object
	//this function returns an array of a field in the object
	//I dont think it is working though 
	//an object can be used as an array why do you need this
	function getObjectFieldAsArray(obj_arr,field)
	{
		var field_arr = [];
		for (var i=0; i<obj_arr.length; i++) {
			field_arr.push(obj_arr[i].field);
        };
        return field_arr;
	}
	
	function toggleElementVisibility(el_id,visibility)  // removes from the layout
	{
		try{
		  var setVisible = (visibility ? "inline" : "none")	
		  if (browserType == "gecko" )
		     document.poppedLayer = 
		         eval('document.getElementById("'+el_id+'")');
		  else if (browserType == "ie")
		     document.poppedLayer = 
		        eval('document.getElementById("'+el_id+'")');
		  else
		     document.poppedLayer =   
		        eval('document.layers["'+el_id+'"]');
		  document.poppedLayer.style.display = setVisible;
	  }catch(ex){}
    }    
//////////////////	

    // getting date period between two dates using good formats
    // used in calendar
function getDatePeriod(start,end)
{
	var date_period = '';
	if(start == end)
	   date_period = start.format('D, M d, Y');
	else if(start.format('M-D-Y') == end.format('M-D-Y'))
       date_period = start.format('D, M d, Y') + '  '+start.format('h i a')+' - '+end.format('h i a');
    else 
       date_period = start.format('D, M d, Y, h i a') + ' - '+end.format('D, M d, Y, h i a');
       
    return date_period;
}

var filesadded="" //list of files already added

//Load a javascript or Css file dynamicallay
/*
 * params filename: js or css path and name
 * params parameter is when you include a smarty template
 * you can include that with start paramter like {include file='' paramname=value}
 * filetype either js or css
 */
function loadJsCssfile(filename, filetype,parameters, control_name){

 if(VE.case_id)
    var controller_url ='/' + VE.case_id + '/javascript/include/?&js_path=' + filename + ( parameters ? '&params_include='+parameters : '' );
 else
    var controller_url ='/javascript/include/?&js_path=' + filename + ( parameters ? '&params_include='+parameters : '' );
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", controller_url)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", controller_url)
 }
 if (typeof fileref!="undefined"){
  if(control_name != null)
    fileref.setAttribute("id", control_name);	
  document.getElementsByTagName("head")[0].appendChild(fileref)
 }
  
 filesadded+="["+filename+"]" //List of files added in the form "[filename1],[filename2],etc" 
}
//the one we are using
function removeJsCssfile1(control_name)
{
	var file = document.getElementById(control_name);	
	if(file)
	{
		file.parentNode.removeChild( file );
	}
}



//control if the file is already added, and then add it if not exists
//we need to call this
function checkLoadJsCssfile(filename, filetype,parameters){
 if (filesadded.indexOf("["+filename+"]")==-1){
  loadJsCssfile(filename, filetype,parameters)  
 }
}

//return true if the js or css file is already loaded dynamically
function checkJsCssLoaded(filename){
  return (filesadded.indexOf("["+filename+"]") > 0); 
}

function removeJsCssfile(filename, filetype){
 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist from
 var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
 var allsuspects=document.getElementsByTagName(targetelement)
 for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
 	var suspect = allsuspects[i];
  if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1)
   allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild()
 }
}

function createJsCssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 return fileref
}

function replaceJsCssfile(oldfilename, newfilename, filetype){
 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist using
 var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
 var allsuspects=document.getElementsByTagName(targetelement)
 for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
  if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(oldfilename)!=-1){
   var newelement=createJsCssfile(newfilename, filetype)
   allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i])
  }
 }
}

function cloneObject(obj) {
    var clone = {};
    for(var i in obj) {
        if(typeof(obj[i])=="object")
            clone[i] = cloneObject(obj[i]);
        else
            clone[i] = obj[i];
    }
    return clone;
}



