 /**
 * @author Ishtiaque Shahrier
 * @version 1.0
 * @Company Terracor Business Solutions
 */
var $D = YAHOO.util.Dom;
var $E =  YAHOO.util.Event;	
var $C = YAHOO.util.Connect;

var TERRACOR = {};

/*************************************************************
UTILITY FUNCTIONS
*************************************************************/
TERRACOR.util = (function(){	

	return {		

		/**set attribute to an element**/
		setAttribute : function(el,name,value){
			 var attribute = document.createAttribute(name);
  			 attribute.nodeValue = value;
			 el.setAttributeNode(attribute);
			 return el;
		},	
				
		/**create a html element (div/span) instantle with styling info and content**/
		createHTMLEl : function(htmlElName,parentId,elID,cssClass,innerHtml){
			var newEl = document.createElement(htmlElName);								
			if(elID) this.setAttribute(newEl,"ID",elID);
			if(cssClass) this.setAttribute(newEl,"class",cssClass);
			if(innerHtml) newEl.innerHTML = innerHtml;				
			$D.get(parentId).appendChild(newEl);				
			return newEl;
		},	
							
		/**capture a form submission**/
	   captureFormSubmission : function(formID,urFunction){
	   		$E.on(formID,"submit",function(e){
				$E.stopEvent(e);				
				 urFunction() ? $D.get(formID).submit() :  false;					
			});				
	   },
	   
	   /**insert a hidden input anywhere on the document**/		   
	   newHiddenInput : function(id,value,parentId){
	   	
	   	   var newHdnEl = document.createElement('input');			   
		   this.setAttribute(newHdnEl,"type", "hidden");
		   this.setAttribute(newHdnEl,"value", value);
		   this.setAttribute(newHdnEl,"id", id);
		   $D.get(parentId).appendChild(newHdnEl);
		   return newHdnEl;			   		   
	   },
	   
	   /**Trims strings ; both left and right**/
	   trim  : function(str){
			return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	   },
	   
	   /**Empty a html list/select**/
	   emtryList : function(listId){
	   		var target = $D.get(listId);
			while(target.options.length>0){
				target.options[0]=null;
			}
			return target;
	   },
	   
	   /**Add an option to a list**/
	   addOptionToList: function(listId,optionObj,font_color){
			var new_   = document.createElement('option');			
			new_.text  = optionObj.TEXT;
			new_.value = optionObj.VALUE;
			if(font_color)	new_.style.color = font_color;			
			var target = $D.get(listId) ;
			if(target){
				try {
					target.add(new_,null);
				}
				catch(ex){
					target.add(new_);
				}
				return target;
			}else{
				alert('Select element not found');
				return -1;
			}
	   },
	   
	   /**remove an Option from a list**/
	   remOptionFromList: function(listId,index){	   	
	   		var list = document.getElementById(listId);
	   		if(list){
		   		var i = index || list.selectedIndex;	   		
		   		return i>=0 ? function(){
		   			list.options[i] = null ;	
		   			return true;
		   		}(): false;
	   		}else{
	   			alert('Select element not found');
	   			return false;
	   		}
	   },
	   /**get selected list otem**/
	   getSelectedListItem:function(listId,useThisIndex){
	   		var list = document.getElementById(listId);	   		
	   		if(list){
	   			var index= useThisIndex || list.selectedIndex;
	   			if(index>=0){
		   			return {
		   				'INDEX':index,
		   				'TEXT' :list.options[index].text,
		   				'VALUE':list.options[index].value	
		   			};
	   			}
	   			else{
	   				return null;
	   			}
	   		}else{
	   			alert('Select element not found');
	   			return null;
	   		}
	   },
	   /**update a list option by val or index**/	   
	   updateOption : function (listId,opt/*{INDEX:x,VALUE:'val',TEXT:'txt'}*/){		
			var listElement =null ;	
			try{
			 listElement  = $D.get(listId);
			}catch(ex){
				alert("List not found");
				return null;
			}			
			//at this point listElement!=null
			var index = -1 ;
			if(opt && listElement.options.length>0){		
				 opt.INDEX ? index = opt.INDEX : function(){
																var i=0;
																while(i<listElement.options.length){
																	var option = listElement.options[i];
																	if(option.value==opt.VALUE){
																		index=i;
																		i=listElement.options.length;					
																	}												
																	i++;
																}			
															}();
				index>=0 ? function(){
					listElement.options[index].value = opt.VALUE;
					listElement.options[index].text  = opt.TEXT;			
				}()   : alert('Index not found'); 															
		    }
			//finally update if index was supplied or found			
			else	alert("Invalid param supplied");
		
	   },
	   toString : function(o,output){
	   		var aReturn = YAHOO.lang.JSON.stringify(o);
			if(output==1)document.write(aReturn);
	   		return aReturn;
	   },
	   /**wrong naming use parse json...samea as this**/
	   toJson:function(o,output){
	   		var aReturn = YAHOO.lang.JSON.parse(o);
			if(output==1)document.write(aReturn);
	   		return aReturn;
	   },
	   parseJson:function(o,output){
	   		var aReturn = YAHOO.lang.JSON.parse(o);
			if(output==1)document.write(aReturn);
	   		return aReturn;		
	   },
	   /**if an item is in array : type sensitive**/
	   /** outputs object
	   		{
				found:false,
				index:-1
			}  
	   **/
	   isInArray :function (needle,haystack)  {
            var found = false;	
			var objRtn={
				found:false,
				index:-1
			};        
	        for (var i=0; i < haystack.length && !objRtn.found; i++){
	            if (haystack[i] === needle) {
	                objRtn.found=true;
   	                objRtn.index=i;
	            }
	        }        	
        	return objRtn;
	  },
	  /*****read  cookie****/
	  getCookie:function(name){
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
			return null;
	  },
	  /*****create cookie****/
	  setCookie:function(name,value,days) {
		var expires="";
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			expires = "; expires="+date.toGMTString();
		}
		document.cookie = name+"="+value+expires+"; path=/";
	 }
	 
		
}})();

