var Validator = function(name) {
    this.formName = name;
    this.errMsg = new Array();

   
    this.required = function(controlId, msg) {
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];
        if ((typeof(obj) == "undefined") || ((obj.value) && (Utils.trim(obj.value) == "")))
        {
            this.addErrorMsg(msg);
        } else {
        	if ((obj.selectedIndex)&&(obj.selectedIndex > 0)) {
        		return;
        	} else if (obj.length) {
				 for(i = 0; i < obj.length; i++){
        			if(obj[i].checked) {
        				return;
        			}
        		}
        		this.addErrorMsg(msg);
        	} else if (Utils.trim(obj.value) == "") {
        		this.addErrorMsg(msg);
        	}
        }
    };
    
    this.getSelectedRadiosIndex = function(controlId) {
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];
        if (obj.length)
        {
        	is_set = false;
        	for (i = 0; i < obj.length; i++)
        	{
        		if (obj[i].checked) 
        		{
        			is_set = true;
        			return i+1;
        		}
        	}        	
        }
        return null;       
    };

	this.requiredRadios = function(controlId, msg) {
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];
        if (obj != null) 
        {
	        if (obj.length)
	        {
	        	is_set = false;
	        	for (i = 0; i < obj.length; i++)
	        	{
	        		if (obj[i].checked) 
	        		{
	        			is_set = true;
	        			break;
	        		}
	        	}
	        	if (!is_set)
	        	{
	        		this.addErrorMsg(msg);
	        	}
	        }else{
	        	if (typeof(obj) == "undefined" || Utils.trim(obj.value) == "")
	        	{
	            	this.addErrorMsg(msg);
	        	}
	        }
        }
    };
    
     this.getSelectedCheckboxesIndex = function(controlId) {
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];
        index_list = new Array();
        if (obj.length)
        {        	
        	for (i = 0; i < obj.length; i++)
        	{
        		if (obj[i].checked) 
        		{
        			index_list[index_list.length] = i+1;        			
        		}
        	}        	
        }
        return index_list;       
    };
    
    this.requiredCheckboxes = function(controlId, maxCount, msg) {
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];
        if (obj.length)
        {
        	selected_count = 0;
        	for (i = 0; i < obj.length; i++)
        	{
        		if (obj[i].checked) 
        		{
        			selected_count++;        			
        		}
        	}        	
        	if ( (maxCount > 0 && selected_count > maxCount) ||
        		 (maxCount == 0 && selected_count == 0) )
        	{
        		this.addErrorMsg(msg);
        	}
        }else{
        	if (typeof(obj) == "undefined" || Utils.trim(obj.value) == "")
        	{
            	this.addErrorMsg(msg);
        	}
        }                   
    }

    this.isEmail = function(controlId, msg, required) {
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];
        obj.value = Utils.trim(obj.value);

        if (!required && obj.value == '')
        {
            return;
        }

        if (!Utils.isEmail(obj.value))
        {
            this.addErrorMsg(msg);
        }
    }
    
    this.eqaul = function(fstControl, sndControl, msg)
    {
        var fstObj = document.forms[this.formName].elements[fstControl];
        var sndObj = document.forms[this.formName].elements[sndControl];
        
        if (fstObj != null && sndObj != null)
        {
            if(fstObj.value!='' && sndObj.value != '')
            {
	            if (fstObj.value == sndObj.value)
	            {
	                this.addErrorMsg(msg);
	            }
            }

        }
    }

    this.gt = function(fstControl, sndControl, msg)
    {
        var fstObj = document.forms[this.formName].elements[fstControl];
        var sndObj = document.forms[this.formName].elements[sndControl];
            
        if (fstObj != null && sndObj != null)
        {
			//if ((fstObj.value < sndObj.value)&&(required ||((fstObj.value != '')&&(sndObj.value != ''))))
	        
	        var value1 = fstObj.value;
	        var value2 = sndObj.value;
if(value1 == '')
{
	value1 = '0';
}
if(value2 == '')
{
	value2 = '0';
}

	        if (parseInt(value1) < parseInt(value2))
	        {
	           this.addErrorMsg(msg);
		    }
        }
    }


    this.isNumber = function(controlId, msg, required) {
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];
        //obj.value = Utils.trim(obj.value);

        if (Utils.trim(obj.value) == '' && !required)
        {
            return;
        } else
        {
		    var temp="0123456789.";  //QWERTYUIOPASDFGHJKLZXCVBNM~-_
		    
		    for(i=0;i < Utils.trim(obj.value).length; i++) 
		    {
		    	var letter = Utils.trim(obj.value).substring(i, i + 1);
		    	
		    	if(-1 == temp.indexOf(letter)) 
		    	{
		    	  this.addErrorMsg(msg);
		    	  return false;
		    	}
		    }
		    if (Utils.trim(obj.value) == ".") { //only one "." also error
		    	  this.addErrorMsg(msg);
		    	  return false;
		    }
		    	
		    return true;
        }
    }

    this.isNumberNoTen = function(controlId, msg, required) {
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];
        obj.value = Utils.trim(obj.value);

        if (obj.value == '' && !required)
        {
            return;
        } else
        {
		    var temp="0123456789";  //QWERTYUIOPASDFGHJKLZXCVBNM~-_
		    
		    for(i=0;i < obj.value.length; i++) 
		    {
		    	var letter = obj.value.substring(i, i + 1);
		    	
		    	if(-1 == temp.indexOf(letter)) 
		    	{
		    	  this.addErrorMsg(msg);
		    	  return false;
		    	}
		    }
		    if (obj.value == ".") { //only one "." also error
		    	  this.addErrorMsg(msg);
		    	  return false;
		    }
		    	
		    return true;
        }
    }
    
    this.isPositiveInt = function(controlId, msg, required) {
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];
        obj.value = Utils.trim(obj.value);

        if (obj.value == '' && !required)
        {
            return;
        }
        else
        {
            if (!Utils.isInt(obj.value) || obj.value < 0) 
            	this.addErrorMsg(msg);
        }
    }
    
    this.isRangInt = function(controlId, msg, required, minValue, maxValue) {
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];
        obj.value = Utils.trim(obj.value);

        if (obj.value == '' && !required)
        {
            return;
        }
        else
        {
            if (!Utils.isInt(obj.value) || obj.value < minValue || obj.value > maxValue) 
            	this.addErrorMsg(msg);
        }
    }
    
     this.isInt = function(controlId, msg, required) {
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];
        obj.value = Utils.trim(obj.value);

        if (obj.value == '' && !required)
        {
            return;
        }
        else
        {
            if (!Utils.isInt(obj.value)) this.addErrorMsg(msg);
        }
    }
	
    this.isNullOption = function(controlId, msg) {
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];

        obj.value = Utils.trim(obj.value);

        if (obj.value > '0' )
        {
            return;
        }
        else
        {
            this.addErrorMsg(msg);
        }
    }

		this.isDate = function(controlId, msg, required) {
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];
        obj.value = Utils.trim(obj.value);

        if (obj.value == '' && !required)
        {
            return;
        }
        else
        {
            if (!Utils.isDate(obj.value)) this.addErrorMsg(msg);
        }
    }
    
    this.isTime = function(controlId, msg, required) {
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];
        obj.value = Utils.trim(obj.value);

        if (obj.value == '' && !required)
        {
            return;
        }
        else
        {
            if (!Utils.isTime(obj.value)) this.addErrorMsg(msg);
        }
    }
    
    this.isDateOrTime = function(controlId, msg, required) {
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];
        obj.value = Utils.trim(obj.value);

        if (obj.value == '' && !required)
        {
            return;
        }
        else
        {
            if (!Utils.isTime(obj.value) && !Utils.isDate(obj.value) ) this.addErrorMsg(msg);
        }
    }
    
    this.isRegex = function(controlId, msg, required, regex) {
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];
        obj.value = Utils.trim(obj.value);

        if (obj.value == '' && !required)
        {
            return;
        }
        else
        {
        	
            if (!regex.test(obj.value)) this.addErrorMsg(msg);
        }
    }

    this.requiredCheckbox = function(chk, msg) {
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];
        var checked = false;

        for(var i = 0; i < objects.length; i++) {
            if (objects[i].type.toLowerCase() != "checkbox") continue;
            if (objects[i].checked)
            {
                checked = true;
                break;
            }
        }

        if (!checked) this.addErrorMsg(msg);
    }
    
    this.ltLength = function(controlId, len, msg)
    {
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];
        //obj.value = Utils.trim(obj.value);
		if (Utils.trim(obj.value).length > len)
        {
            this.addErrorMsg(msg);
        }
    }

    this.gtLength = function(controlId, len, msg)
    {
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];
        obj.value = Utils.trim(obj.value);
		if (obj.value.length < len)
        {
            this.addErrorMsg(msg);
        }
    }

    this.hasError = function() {
    		return this.errMsg.length > 0;
	  }
	
    this.passed = function() {
        if (this.errMsg.length > 0)
        {
            var msg = "";
            for (i = 0; i < this.errMsg.length; i++)
            {
              msg += "" + this.errMsg[i] + "\n";
            }

            alert(msg);
            return false;
        }
        else
        {
          return true;
        }
    }

    this.addErrorMsg = function(str) {
        this.errMsg.push(str);
    }
    
    this.isNumberOrLetter = function(controlId, msg) {
    
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];

	    var param = obj.value;	
		
	    if (param == null || param=="") 
	    {
	     return false;
	    }
	    
	    if (param.length >= 1) {
	    	var letter = param.substring(0, 1);
	    	if ((letter < 'a') || (letter > 'z')) {
	    	  this.addErrorMsg(msg);
	    	  return false;
	    	}
	    }
	    var temp="0123456789qwertyuiopasdfghjklzxcvbnm";  //QWERTYUIOPASDFGHJKLZXCVBNM~-_
	    
	    for(i=0;i < param.length; i++) 
	    {
	    	var letter = param.substring(i, i + 1);
	    	
	    	if(-1 == temp.indexOf(letter)) 
	    	{
	    	  this.addErrorMsg(msg);
	    	  return false;
	    	}
	    }
	    
	    return true;
	 }
	 

	 this.isInRange = function(controlId, beginNum, endNum, msg) {
    
    	var obj = controlId;
    	if(typeof(obj) == 'string' )
        	obj = document.forms[this.formName].elements[controlId];

	    var param = obj.value;	
		
	    if (param == null || param=="") 
	    {
	     return false;
	    }
	    
	    if(param.length < beginNum || param.length > endNum) 
	    {
	    	this.addErrorMsg(msg);
	    	return false;
	    }
	    
	    return true;
	 }
}

function showNotice(objId)
{
      var obj = document.getElementById(objId);

    if (obj)
    {
        if (obj.style.display != "block")
        {
            obj.style.display = "block";
        }
        else
        {
            obj.style.display = "none";
        }
    }
}
/**
 * add one option of a select to another select.
 */
function addItem(src, dst)
{
    for(var x = 0; x < src.length; x++)
    {
        var opt = src.options[x];
        if (opt.selected && opt.value != '')
        {
            var newOpt = opt.cloneNode(true);
            newOpt.className = '';
            dst.appendChild(newOpt);
        }
    }

    src.selectedIndex = -1;
}

/**
 * move one selected option from a select.
 */
function delItem(ItemList)
{
    for(var x=ItemList.length-1;x>=0;x--)
    {
        var opt = ItemList.options[x];
        if (opt.selected)
        {
            ItemList.options[x] = null;
        }
    }
}
/**
 * join items of an select with ",".
 */
function joinItem(ItemList)
{
    var OptionList = new Array();
    for (var i=0; i<ItemList.length;i++)
    {
        OptionList[OptionList.length] = ItemList.options[i].text +"|"+ ItemList.options[i].value;
    }
    return OptionList.join(",");
}

