/**
 * Project: Javascriptian 
 * 
 * Source file:  jsian-version-1.0.1.js
 * 
 * Description: Responsible for all the utility task related to javascript
 * 
 * Author : Sahil Azim
 * 
 * Creation Date: Dec-01-2008 
 * 
 * Modified Date:
 * 
 * Version: 1.0
 * 
 * Revisions: 0
 * 
 */
var JSClass = {
   newClass:function(){
   	   function func(){
   	      if(this.init){
               this.init.apply(this, arguments);
          }
       }
       func.prototype.constructor = func;
       return func;
   }
};

function isValidSession(req) {
	var isValid = true;
	var appPath = "./";
	var responseText = req.responseText;
	if (responseText.indexOf("j_security_check") > 0) {
		window.location = appPath;
		isValid = false;
		document.cookie = "jsian=expired";
	}
	return isValid;
}

var ErrorUtil = {
	errorCode:[404,500],
	errorMessage:["File Not Found","Server Error"],
	messagePrefix:"Javascriptian <br>",//You can change it according to your application
	message:function(code){
		for(var i=0;i<this.errorCode.length;i=i+1){
			if(code===this.errorCode[i]){
				return this.messagePrefix+this.errorMessage[i];
			}
		}
	}
};

var FormConstant = {
    Text:"text",
    Password:"password",
    Hidden:"hidden",
    File:"file",
    DropDown:"select-one",
    TextArea:"textarea",
    ListBox:"select-multiple",
    CheckBox:"checkbox",
    Radio:"radio",
    GET:"GET",
    POST:"POST"
};

var AjaxConstant = {
    SINGLE_REQUEST:true,
    REQUIRED_CACHING:false,//by default caching is off. means it will call fresh call every time.
    BUSY:false,
    ALL_PARAMETER:"all_parameter",
    FREE:true,
    ASYNCHRONOUS:true,
    REQUEST_STATUS:true,
    isRequestFree:function(){
        return this.SINGLE_REQUEST?this.REQUEST_STATUS:this.FREE;
    }
};

var BrowserUtil = {
    ns:(navigator.appName == "Netscape"),
	ie:(navigator.appName == "Microsoft Internet Explorer")
};

var EventUtil = {

    addEventListener:function(target, type, callback, captures) {
        if (target.addEventListener) {
            target.addEventListener(type, callback, captures);
        } else if (target.attachEvent) {
            target.attachEvent('on'+type, callback);
        } else {
            target['on'+type] = callback;
        }
    },

    fireEvent : function(obj, type) {
        if(BrowserUtil.ie){
            obj.fireEvent("on" + type);
        }else {
            var evt = document.createEvent('MouseEvents');
            evt.initEvent(type, true, true);
            obj.dispatchEvent(evt);
        }
    },

    getSrcElement : function(evt){
        if(evt){
             if(BrowserUtil.ie){
              return evt.srcElement;
             }else{
              return evt.target;
             }
        }
    },

    getParentElement : function(evt){
        var srcElement = EventUtil.getSrcElement(evt);
        if(srcElement){
             if(BrowserUtil.ie){
                return srcElement.parentElement;
             }else{
                return srcElement.parentNode;
             }
        }
    }
};

var List = JSClass.newClass();

List.prototype = {
        list:null,
        init:function(listObj){
        	this.list = listObj;              
        },
        addOption:function(text,value){
            this.list.options[this.list.options.length] = new Option(text,value);
        },
        removeAll:function(){
            var len =this.list.options.length;
            for(var i=0;i<len;i=i+1){
                this.list.options[0]= null;
            }
        },
        remove:function(index){
            if(index<this.list.options.length)
                this.list.options[index]= null;
        },
        getLength:function(){
            return this.list.options.length;            
        },
        selectIndex:function(index){
        	this.list.option[index].selected = true; 
        },
        selectByValue:function(value){
        	this.list.value = value; 
        },
        getSelectedValue:function(){
        	return this.list.value
        }
}

var WebForm = JSClass.newClass();

WebForm.prototype = {

    elements:new Array(),

    init:function(){
    	//alert("calling 0.0");
        this.loadForm();
    },

    loadForm:function(){
    	//alert("calling 0.1");
        if(this.elements || this.elements.length===0){
            this.elements = new Array();
            //alert("calling 0.2");
            if(document.getElementsByTagName("input")){
                var input = document.getElementsByTagName("input");
                for(var i=0;i<input.length;i=i+1){
                    this.elements[this.elements.length] = input[i];
                }
            }
            if(document.getElementsByTagName("select")){
                var select = document.getElementsByTagName("select");
                for(i=0;i<select.length;i=i+1){
                    this.elements[this.elements.length] = select[i];
                }
            }
            if(document.getElementsByTagName("textarea")){
                var textarea = document.getElementsByTagName("textarea");
                for(i=0;i<textarea.length;i=i+1){
                    this.elements[this.elements.length] = textarea[i];
                }
            }
        }
    },
    /*
    * might be multiple object with the same name if you are sure or you can take it for safe side
    * like radio button, check box, textbox with same name etc.
    */
    getComponentArray:function(name){
        var comp = null;
        if(this.elements && this.elements.length>0){
            comp = new Array();
            for(var index in this.elements){
                if(this.elements[index].name == name){
                    comp[comp.length] = this.elements[index];
                }
            }
        }
        return comp;
    },

    /*
     * you can use it for that component where you are sure that it has only one
     */
    getFormObject:function(name){
        var comp = null;
        if(this.elements && this.elements.length>0){
            for(var index in this.elements){
                if(this.elements[index].name == name){
                    comp = this.elements[index];
                    break;
                }
            }
        }
        return comp;
    },

    getElements:function(){
        return this.elements;
    },

    getUniqueElements:function(){
        var comp = null;
        if(this.elements && this.elements.length>0){
            comp = new Array();
            for(var i in this.elements){
                var exist = false;
                for(var j  in comp){
                    if(comp[j].name == this.elements[i].name){
                        exist = true;
                    }
                }
                if(!exist){
                    comp[comp.length] = this.elements[i];
                }
            }
        }
        return comp;
    }
};

var CommonUtil = {
    /**
     * Method to sort value of List in descending order.
     */
    sortAsc:function (a, b){
        var i=-1;
        try{
        if(a){
            a = CommonUtil.trim(a).toLowerCase();
            if(a.length>0 && a.substring(0,1).charCodeAt(0)==160){
                a=a.substring(1);
            }
        }
        if(b){
            b = CommonUtil.trim(b).toLowerCase();
            if(b.length>0 && b.substring(0,1).charCodeAt(0)==160){
                b=b.substring(1);
            }
        }
        try{
        i=a>b?1:a<b?-1:0;
        }catch(e){i=0;}
        }
        catch(e){}
        return i;
    },
    /**
      * This function is responsible for removing right space.
      */
     rtrim:function(value) {
        if(value){
	        if (isNaN(value)) {
	            var re = /((\s*\S+)*)\s*/;
	            return value.replace(re, "$1");
	        }else{
	        	return value;
	        }
        }
        
        return "";
    },
    /**
      * Removes leading and ending whitespaces.
      */
    trim:function (value) {
        if (value === null) {
            value = "";
        }
        return CommonUtil.ltrim(CommonUtil.rtrim(value));
    },
    /**
      * This function is responsible for removing left space.
      */
    ltrim:function(value){
	    if(value){
	        if (isNaN(value)) {
	            var re = /\s*((\S+\s*)*)/;
	            return value.replace(re, "$1");
	        }else{
	        	return value;
	        }
	     }
        return "";
    },

    getEncodedString:function(value){
        var encoded = "";
        value = CommonUtil.trim(value);
        if(value){
        	//to convert number into string.
        	value=""+value;
            while(value.indexOf("  ")>=0){
                value = value.replace(/  /g," ");
            }
            var SAFECHARS = "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "-_.!~*'()";
            var HEX = "0123456789ABCDEF";
            var plaintext = value;
            for (var i = 0; i < plaintext.length; i=i+1 ) {
                var ch = plaintext.charAt(i);
                if (ch == " ") {
                    encoded += "+";
                } else if (SAFECHARS.indexOf(ch) != -1) {
                    encoded += ch;
                } else {
                    var charCode = ch.charCodeAt(0);
                    if (charCode < 255) {
                        encoded += "%";
                        encoded += HEX.charAt((charCode >> 4) & 0xF);
                        encoded += HEX.charAt(charCode & 0xF);
                    }
                }
            }
        }
        return encoded;
    }
};

/*
* ParameterUtil is the utility class used to build the parameter to send to the server.
*/
var FormParameter = JSClass.newClass();

FormParameter.prototype = {
    /*
    * When you give null argument in this method it will pick all the form component
    * from the current page and return. But it is recommended to pass the parameter name
    * list in this method it is the type of array
    */
    form:null,

    parameter:null,

    init:function(){
    	this.form = new WebForm();
        this.parameter = "";
    },


    getAllParameter:function(param,seperator,selected){
        //default list box part is selected one
        var parameter = "";
        if(param){
            for(var i in param){
                var value = this.getParameterString(param[i],seperator,selected);
                if(value && value!==""){
                	if(parameter!="")parameter+="&";
	                parameter+=value;
	            }
            }
        }else{
            var elements=this.form.getElements();
            var j=0;
            for(i in elements){

                value = this.getParameterString(elements[i].name,seperator,selected);
                if(value && value!==""){
                    if(parameter!="")parameter+="&";
	                parameter+=value;
	            }
                //alert(elements[i].name +" :: "+value);
            }
        }
        return parameter;
    },

    getParameterString:function(name , seperator, selected){
        var param = "";
        var seperatedBy = seperator?CommonUtil.getEncodedString(seperator):"&";
        var formArray = this.form.getComponentArray(name);
        if(formArray && formArray.length>0){
            for(var i in formArray){
               var value = "";
               var obj = formArray[i];
               if((obj.type == FormConstant.Text
                       || obj.type == FormConstant.TextArea
                       || obj.type == FormConstant.Hidden
                       || obj.type == FormConstant.DropDown
                       || obj.type == FormConstant.File
                       || obj.type == FormConstant.Password) && !obj.disabled){
                    value = obj.name +"="+ CommonUtil.getEncodedString(obj.value);
               }else if((obj.type == FormConstant.Radio || obj.type == FormConstant.CheckBox) && obj.checked && !obj.disabled){
                   if(seperator){
                       if(i==0)value += obj.name + "=";
                        value += CommonUtil.getEncodedString(obj.value);
                   }else{
                        value += obj.name + "="+CommonUtil.getEncodedString(obj.value);                       
                   }
               }else if(obj.type == FormConstant.ListBox && !obj.disabled){
                    var val = "";
                    for (var j = 0; j < obj.options.length; j=j+1) {
                        if(selected){
                            if(obj.options[j].selected){
                                if(!seperator)
                                    val += obj.name +"="+ CommonUtil.getEncodedString(obj.options[j].value)+seperatedBy;
                                else
                                    val += CommonUtil.getEncodedString(obj.options[j].value)+seperatedBy;
                            }
                        }else{
                            if(!seperator)
                                val += obj.name +"="+ CommonUtil.getEncodedString(obj.options[j].value)+seperatedBy;
                            else
                                val += CommonUtil.getEncodedString(obj.options[j].value)+seperatedBy;
                        }
                    }
                    if(seperator)val=obj.name+"="+val;
                    if(val!="")value=val.substring(0,val.lastIndexOf(seperatedBy));
               }
               if(value!="")param+=value+seperatedBy;
            }
            if(param!="" && param.length == param.lastIndexOf(seperatedBy)+seperatedBy.length){
                param = param.substring(0,param.lastIndexOf(seperatedBy));
            }
        }
        return param;
    },

    setParameter:function(name, seperator, selected){
        var param = this.getParameterString(name, seperator, selected);
        if(param && param!=""){
            this.parameter += this.parameter==""?param:"&"+param;
        }
        
    },

    setExternalParameter:function(name,value){
           this.parameter += "&"+name+"="+CommonUtil.getEncodedString(value);
        
    },

    getParameter:function(){
        return this.parameter;
    }

}

var AjaxRequest = JSClass.newClass();

AjaxRequest.prototype = {

     req:null,
     init:function(){
        var request = null;
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            request = new XMLHttpRequest();
            if (request.overrideMimeType) {
                request.overrideMimeType("text/xml");
            }
        } else {
            if (window.ActiveXObject) { // IE
                try {
                    request = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch (e) {
                    try {
                        request = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch (e) {
                    }
                }
            }
        }
        this.req = request;
     },

     sendRequest:function(requestInfo){
         if(AjaxConstant.isRequestFree()){
            AjaxConstant.REQUEST_STATUS = AjaxConstant.BUSY;
            requestInfo.onProcess();
            var request = this.req;
            request.onreadystatechange = function(){
                this.states = request.readyState;
                if (this.states == 0 || this.states == 1 || this.states == 2 || this.states == 3){
                    requestInfo.onProcess();
                }
                if (this.states == 4) {
                    AjaxConstant.REQUEST_STATUS=AjaxConstant.FREE;
                    if (request.status == 200) {
                    	isValidSession(request);
                        requestInfo.ResponseText=request.responseText;
                        requestInfo.ResponseXML=request.responseXML;
                        requestInfo.onComplete();
                    } else {
                        requestInfo.onError(request.status);
                    }
                }
            };
            //alert("Method:"+requestInfo.Method+":: URL:"+requestInfo.URL+":"+requestInfo.Parameter+":: asynchronous"+requestInfo.Asynchronous);
            request.open(requestInfo.Method, requestInfo.URL, requestInfo.Asynchronous);
            if (requestInfo.Method == FormConstant.POST) {
                request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                request.setRequestHeader("Content-length", requestInfo.Parameter.length);
                request.setRequestHeader("Connection", "close");
            }
            request.send(requestInfo.Parameter);
         }else{
             requestInfo.onBusy();
         }
     }
}


var RequestInfo = JSClass.newClass();

RequestInfo.prototype = {
      init:function(method,url,param,appObj){
          this.Parameter = param?param:""; // if you pass null here it will pick all the form component and send it. but it is not recommended.
          if(!AjaxConstant.REQUIRED_CACHING){
          	if(this.Parameter!="")this.Parameter+="&";
          	this.Parameter+="jstime="+DateUtil.getCurrentTimeInMS();
          }
          this.ApplicationObject = appObj;//set the application object for your action after request complete.
          this.URL = url;
          this.Method = method?method:FormConstant.POST;//default method is POST
          if(this.Method == FormConstant.GET){
            this.URL = this.URL + "?" + this.Parameter;
          }
      },
      ApplicationObject:null,
      Method:FormConstant.POST,//default method is POST
      Parameter:null,
      URL:null,
      Asynchronous:AjaxConstant.ASYNCHRONOUS,
      ResponseText:null,
      ResponseXML:null,
      onProcess:function(){
          if(this.ApplicationObject.onProcess)
           this.ApplicationObject.onProcess();
      },
      onComplete:function(){
          this.ApplicationObject.xml = this.ResponseXML;
          this.ApplicationObject.text = this.ResponseText;
          this.ApplicationObject.onComplete();
      },
      onError:function(code){
        if(this.ApplicationObject.onError)
           this.ApplicationObject.onError(code);
      },
      onBusy:function(){
          if(this.ApplicationObject.onBusy)
           this.ApplicationObject.onBusy();
      }
}

var Ajax = {
      sendRequest:function(method,url,parameter,appObj){
        var req = new AjaxRequest();
        if(AjaxConstant.ALL_PARAMETER == parameter){
        	var parameter = new FormParameter().getAllParameter();
        }
        req.sendRequest(new RequestInfo(method,url,parameter,appObj));
      }
}

var  ListElement = JSClass.newClass();

ListElement.prototype = {
    /**
     * Constructor to intialize form and its element.
     */

    text : null,
    value : null,
    element :null,
    name :null,
    sortByValue : false,


    init:function(name){
        this.text = new Array();
        this.value = new Array();
        this.form = new WebForm()
        this.element = this.form.getFormObject(name);
        this.name = name;
        this.sortByValue = false;
        this.addFromList();
    },

    /**
     * Method to add
     */
    add : function(){
        // we expect name of list that is going to add the selected option into the current one
        var listLength = arguments.length;
        if(listLength > 0){
            for(var index = 0; index<listLength; index=index+1){
                var tempListObj = new ListElement(arguments[index]);
                //alert(arguments[index]+"step 212 "+eval("this.form."+arguments[index]));
                //retrieve the list of text and values from the given list name in the argument
                var tempText = tempListObj.getSelectedText();
                var tempValue = tempListObj.getSelectedValue();
                tempListObj.removeSelection();
                //loop upto the selected value;
                for(var j=0;j<tempValue.length;j=j+1){
                    if(!this.isExist(tempValue[j])){
                        this.text[this.text.length] = tempText[j];
                        this.value[this.value.length] = tempValue[j];
                    }
                }
                this.refresh();
                this.empty();
                for(var i=0;i<this.text.length;i=i+1){
                    this.addToList(this.text[i],this.value[i]);
                }
            }
        }
    },
    /**
     * Method to add value and text to List.
     */
    addToList : function(text, val){
        var length = this.getLength();
        if(!this.isExist(val)){
            //alert("this.element.options[length] "+new Option(text,val));
            this.element.options[length] = new Option(text,val);
        }
    },
    /**
     * Method to sort List by value.
     */
    sortValue : function(){
        this.sortByValue = true;
        this.addFromList();
    },
    /**
     * Method to remove value and text to List.
     */
    removeSelected : function(){
        for(var i=0;i<this.getLength();i=i+1){
            if(this.element.options[i].selected){
                this.element.options[i]=null;
                i=i-1;
            }
        }
        this.addFromList();
    },
    /**
     * Method to refresh List Element.
     */
    refresh : function(){

        var tempList = new Array();
        for(var i=0;i<this.text.length;i=i+1){
            if(this.sortByValue){
                tempList[tempList.length]=this.value[i]+" ****"+this.text[i];
            }else{
                tempList[tempList.length]=this.text[i]+" ****"+this.value[i];
            }
        }
        //alert("tempList before "+tempList);
        tempList.sort(CommonUtil.sortAsc);
        //alert("tempList after "+tempList);
        //refresh the Array();
        this.text = new Array();
        this.value = new Array();
        for(var i=0;i<tempList.length;i=i+1){
            var temp = tempList[i].split(" ****");
            if(temp[1]!==null){
                if(this.sortByValue){
                    this.text[this.text.length]=temp[1];
                    this.value[this.value.length]=temp[0];
                }else{
                    this.text[this.text.length]=temp[0];
                    this.value[this.value.length]=temp[1];
                }
            }
        }
    },
    /**
     * Method to empty List.
     */
    empty : function(){
        var length = this.getLength();
        for(var i=0;i<length;i=i+1){
            this.element.options[0]=null;
        }
    },
    /**
     * Method to add value and text from List.
     */
    addFromList : function(){
        var length = this.getLength();
        this.text = new Array();
        this.value = new Array();
        for(var i=0;i<length;i=i+1){
            this.text[this.text.length] = this.element.options[i].text;
            this.value[this.value.length] = this.element.options[i].value;
        }
    },
    /**
     * Method to sort value in List.
     */
    sortMe : function(){
        this.refresh();
        this.empty();
        for(var i=0;i<this.text.length;i=i+1){
            this.addToList(this.text[i],this.value[i]);
        }
    },
    /**
     * Method to get length of List.
     */
    getLength : function(){
        var length = 0;
        if(this.element){
            length = this.element.options.length;
        }
        return length;
    },
    /**
     * Method to get selected value from List.
     */
    getSelectedValue : function(){
        var values = new Array();
        var length = this.getLength();
        for(var i=0;i<length;i=i+1){
            if(this.element.options[i].selected){
                values[values.length] = this.element.options[i].value;
            }
        }
        return values;
    },
    /**
     * Method to get selected text from List.
     */
    getSelectedText : function(){
        var texts = new Array();
        var length = this.getLength();
        //alert("step 2121 "+length);
        for(var i=0;i<length;i=i+1){
            if(this.element.options[i].selected){
                texts[texts.length] = this.element.options[i].text;
            }
        }
        //alert("step 2122 "+texts);
        return texts;
    },
    /**
     * Method to remove selection from List box.
     */
    removeSelection : function(){
        var length = this.getLength();
        for(var i=0;i<length;i=i+1){
            if(this.element.options[i].selected){
                this.element.options[i].selected = false;
            }
        }
    },

    /**
     * Method to check values exist or not in List.
     */
    isExist : function(values){
        var exist = false;
        var length = this.getLength();
        for(var i=0;i<length;i=i+1){
            if(this.value[i] == values){
                exist = true;
                break;
            }
        }
        return exist;
    }
}

var ListUtil = JSClass.newClass();

ListUtil.prototype = {
    object:null,

    init:function(){
        this.object = new Array();
    },

    addListObject :  function (){
        var length = arguments.length;
        this.object = new Array();
        if(length >0){
            for(var i=0;i<length;i=i+1){
                var listObj= new ListElement(arguments[i]);
                listObj.sortMe();
                this.object[this.object.length] = listObj;
            }
        }
    },

    addListOption :  function (){
        var length = arguments.length;
        //expecting first argument is the name, into which another argumented list options need to add

        if(length >1){
            for(var i=0;i<this.object.length;i=i+1){
                var listElement = this.object[i];
                if(listElement.name == arguments[0]){
                    for(var j=1;j<length;j=j+1){
                        listElement.add(arguments[j]);
                    }
                    break;
                }
            }
        }
    },

    removeSelected :  function (name){
        var length = arguments.length;
        //expecting first argument is the name into another argumented list options need to add
        if(length >0){
            for(var i=0;i<this.object.length;i=i+1){
                var listElement = this.object[i];
                if(listElement.name == arguments[0]){
                    listElement.removeSelected();
                    break;
                }
            }
        }
    },

    sortByValue :  function (name){
        if(name){
            for(var i=0;i<this.object.length;i=i+1){
                var listElement = this.object[i];
                if(listElement.name == name){
                    listElement.sortValue();
                    break;
                }
            }
        }
    }

}

var ElementUtil = JSClass.newClass();

ElementUtil.prototype = {

    elements:null,

    init:function(){
        this.elements = new Array();
    },

    /**
    * Method to refresh element.
    */
    refresh : function(){
        this.elements = new Array();
    },

    /**
     * Method to show element given as parameter
     */
    showBlock : function(){
        var length = arguments.length;
        if(length>0){
            this.hideAll();
            for(var i=0;i<length;i=i+1){
                 var obj=this.findById(arguments[i]);
                 if(obj!==null){
                     obj.style.display = "block";
                 }
            }
        }
    },

    /**
     * Method to show element given as parameter.
     */
    show : function(){
        var length = arguments.length;
        if(length>0){
            this.hideAll();
            for(var i=0;i<length;i=i+1){
                 var obj=this.findById(arguments[i]);
                 if(obj!==null){
                     obj.style.display = "" ;
                 }
            }
        }
    },

    /**
     * Method to show element given as parameter
     */
    showOnly : function(){
        var length = arguments.length;
        if(length>0){
            for(var i=0;i<length;i=i+1){
                 var obj=this.findById(arguments[i]);
                 if(obj!==null){
                     obj.style.display = "" ;
                 }
            }
        }
    },

    /**
     * Method to hide All element.
     */
    hideAll : function(){
        var length = this.elements.length;
        if(length>0){
            for(var i=0;i<length;i=i+1){
                 var obj=this.elements[i];
                 if(obj!==null){
                     obj.style.display = "none" ;
                 }
            }
        }
    },

    /**
     * Method to hide element given as parameter.
     */
    hide : function(){
        var length = arguments.length;
        if(length>0){
            for(var i=0;i<length;i=i+1){
                 var obj=this.findById(arguments[i]);
                 if(obj!==null){
                     obj.style.display = "none" ;
                 }
            }
        }
    },

    /**
     * Method to set element object by id.
     */
    setObjectById : function(){
        var length = arguments.length;
        if(length>0){
            for(var i=0;i<length;i=i+1){
                var obj = document.getElementById(arguments[i]);
                if(obj!==null && this.findById(arguments[i])===null){
                    obj.style.display = "none";
                    this.elements[this.elements.length] = obj;
                }
            }
        }
    },

    /**
     * Method to find element by id.
     */
    findById : function(id){
        if(this.elements.length>0 && id!==null){
            for(var i=0;i<this.elements.length;i=i+1){
                if(this.elements[i].id==id){
                    return this.elements[i];
                }
            }
        }
        return null;
    }

}

var JSObject = {

    getObject:function(name){
        return document.getElementById(name);
    },

    showInline : function(name){
        if(JSObject.getObject(name) && JSObject.getObject(name).style){
            JSObject.getObject(name).style.display = "inline";
        }
    },

    show : function(name){
        if(JSObject.getObject(name) && JSObject.getObject(name).style){
            JSObject.getObject(name).style.display = "";
        }
    },

    hide : function(name){
        if(JSObject.getObject(name) && JSObject.getObject(name).style){
            JSObject.getObject(name).style.display = "none";
        }
    },

    innerHTML : function(name,content){
        if(JSObject.getObject(name)){JSObject.getObject(name).innerHTML = content;}
    },

    getInnerHTML : function(name){
         if(JSObject.getObject(name))
            return JSObject.getObject(name).innerHTML;
         else
            return "";
    },

    removeMe : function(name){
        if (JSObject.getObject(name).parentNode){
            JSObject.getObject(name).parentNode.removeChild(JSObject.getObject(name));
        }
    }

}

var Validation = {

    /**
    * check the given component name with the blank if it is list box then it is for atleast one selection.
    */
    isBlank : function(name,msg,checkForSelected){
        var webForm  = new WebForm();
        this.elements = webForm.getElements();
        if(this.elements!==null){
           for(var i=0;i<this.elements.length;i=i+1){
               if(this.elements[i].name == name
                       && (this.elements[i].type == FormConstant.Text
                       || this.elements[i].type == FormConstant.TextArea
                       || this.elements[i].type == FormConstant.File
                       || this.elements[i].type == FormConstant.Password)
                       ){
                    var value =  this.elements[i].value;
                    if(value === null || CommonUtil.trim(value).length == 0){
                        if(msg)alert(msg);
                        this.elements[i].focus();
                        return true;
                    }
                    break;
               }else if(this.elements[i].type == FormConstant.ListBox
                       && this.elements[i].name == name){
                   var obj = this.elements[i];
                   if(checkForSelected){
                       var notSelected = true;
                       for(var j in obj.options){
                           if(obj.options[j].selected){
                               notSelected = false;break;
                           }
                       }
                       if(!notSelected){
                           if(msg)alert(msg);
                           return notSelected;
                       }
                   }else{
                       if(obj.options.length<=0){
                           if(msg)alert(msg);
                           return true;
                       }
                   }
                   break;

               }
           }
       }
       return true;
    },

    /**
    * Method is responsible to check that field first and second is matching or not.
    */
    match : function(first,second,msg){
        var webForm  = new WebForm();
        var firstObj = webForm.getFormObject(first);
        var secondObj = webForm.getFormObject(second);
        if((firstObj.type == FormConstant.Text
                       || firstObj.type == FormConstant.Password
                       || firstObj.type == FormConstant.DropDown
                       || firstObj.type == FormConstant.TextArea)
            && (secondObj.type == FormConstant.Text
                       || secondObj.type == FormConstant.Password
                       || secondObj.type == FormConstant.DropDown
                       || secondObj.type == FormConstant.TextArea)){
                    if(firstObj.value !=  secondObj.value){
                        if(msg)alert(msg);
                        secondObj.value = "";
                        secondObj.focus();
                        return false;
                    }
        }
        return true;
    },

    /**
    * check the given emailName components for having valid email and display message if error
    */
    isValidEmail:function(emailName,msg){
        var webForm  = new WebForm();
        var emailObj = webForm.getFormObject(emailName);
        var email = emailObj.value;
        if(email){
            var regEx = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
            var valid = email.match(regEx);
            if(!valid){
                if(msg)alert(msg);
                emailObj.focus();
                return false
            }
            return true;
        }else{
            return false;
        }
    },

    /**
    * this will work for both listbox and textbox. list box for minimun selection
    * and text box for min length
    */
    isLessThan:function(name,length,msg){
       var webForm  = new WebForm();
       var object = webForm.getFormObject(name);
       if(object.type == FormConstant.ListBox){
           var count = 0;
           for(var i in object.options){
               if(object.options[i].selected){
                   count=count+1;
               }
           }
           if(count<length){
               if(msg)alert(msg);
               return false;
           }
       }else{
           if(object.value.length < length){
               if(msg)alert(msg);
               return false
           }
       }
       return true;
    },

    /**
    * this will work for both listbox and textbox. list box for maximum selection
    * and text box for min length
    */
    isGreaterThan:function(name,length,msg){
       var webForm  = new WebForm();
       var object = webForm.getFormObject(name);
       if(object.type == FormConstant.ListBox){
           var count = 0;
           for(var i in object.options){
               if(object.options[i].selected){
                   count=count+1;
               }
           }
           if(count>length){
               if(msg)alert(msg);
               return false;
           }
       }else{
           if(object.value.length > length){
               if(msg)alert(msg);
               return false
           }
       }
       return true;
    },

    /**
    * this will work for both listbox and textbox. list box for minimun selection
    * and text box for min length
    */
    isAlphaNumeric:function(name,msg){
       var webForm  = new WebForm();
        var alphaObj = webForm.getFormObject(name);
        var alpha = alphaObj.value;

        if(alpha){
            var regEx = /^([a-zA-Z0-9_-]+)$/;
            var valid = alpha.match(regEx);
            if(!valid){
                if(msg)alert(msg);
                alphaObj.focus();
                return false
            }
            return true;
        }
        return false;
    },
    
    isEqual:function(name,length,msg){
       var webForm  = new WebForm();
       var object = webForm.getFormObject(name);
       var equal=false;
       if(object.type == FormConstant.ListBox){
           var count = 0;
           for(var i=0;i<object.length;i=i+1){
           		if(object.options[i].selected){
                   count=count+1;
               }
           }
           equal = (count==length)
       }else{
       	   equal = (object.value.length == length)
       }
       if(!equal && msg)alert(msg);
       return equal;
    }  
}

var DateUtil = {
	getCurrentTimeInMS:function(){
		var date = new Date();
		var day = date.getDate();      // Returns the day of the month
		var mon = date.getMonth();      // Returns the month as a digit
		var year = date.getFullYear();  // Returns 4 digit year
		var hour = date.getHours();     // Returns hours
		var min = date.getMinutes();    // Returns minutes
		var sec = date.getSeconds();    // Returns seocnds
		var mil = date.getMilliseconds();  // Returns Milliseconds
		return day+""+mon+""+year+""+hour+""+min+""+sec+""+mil;
	}
}