﻿function validateTextField(ctrlid,required,limit,type,iscsv){
    var txt=document.getElementById(ctrlid);
    var lbl=document.getElementById(ctrlid+"label");
    
    /*ALWAYS HIDE THE ERROR AS IT WILL BE SHOWN LATER IF NEEDED*/
    var errid=ctrlid+"error";
    hideErrorMessage(errid);
    
    if(required=="Y"){
        if(txt!=null && lbl!=null){
            /*VALIDATE THERE IS INPUT IN THE TEXT FIELD*/
            if (trimStr(txt.value)==""){
                showErrorMessage(errid,"Input is required from the text field ["+lbl.value+"].");
                txt.focus();
                return(false);
            }
        }
        
        
        
    }
        
    if(parseInt(limit)>0){
        if(txt!=null && lbl!=null){
            /*VALIDATE THE FIELD LENGTH*/
            if (txt.value.length>limit){
                showErrorMessage(errid,"The current length of the field ["+lbl.value+"] is "+txt.value.length+" characters.<br />The length of this field cannot exceed "+limit+" characters.");
                txt.focus();
                return(false);
            }
        }
    }
    
    if(iscsv=="Y"){
        /*COMMA SEPARATED VALUES*/
        var thetext = trimStr(txt.value);
        
        if(thetext.charAt(0)==","){
            /*Leading comma not allowed*/
            showErrorMessage(errid,"This field cannot start with a comma.");
            txt.focus();
            return(false);
        }
        
        if(thetext.charAt(thetext.length-1)==","){
            /*Trailing comma not allowed*/
            showErrorMessage(errid,"This field cannot end with a comma.");
            txt.focus();
            return(false);
        }
//      Bug 267 testing 12/10/2008       
        var csvitems = txt.value.split(",");
        for(i=0;i<csvitems.length;i++){
            var csvitem=trimStr(csvitems[i]);
            
            if(csvitem==""){
//                /*This field can contain blank CSV items*/
//                showErrorMessage(errid,"The values in the comma separated list cannot be blank.");
//                txt.focus();
//                return(false);
            }
            
            if(type=="Numeric"){
                if(numericOnly(csvitem)==false){
                    /*This field can only contain numbers*/
                    showErrorMessage(errid,"Each value in this comma separated list can only contain numbers.");
                    txt.focus();
                    return(false);
                }
            }
            else if(type=="Alpha"){
                if(alphaOnly(csvitem)==false){
                    /*This field cannot contain numbers*/
                    showErrorMessage(errid,"Each value in this comma separated list cannot contain any numbers.");
                    txt.focus();
                    return(false);
                }
            }
        }
    }
    else{
        /*STANDARD TEXT VALUES*/
        if(type=="Numeric"){
            if(numericOnly(txt.value)==false){
                /*This field can only contain numbers*/
                showErrorMessage(errid,"This field can only contain numbers.");
                txt.focus();
                return(false);
            }
        }
        else if(type=="Alpha"){
            if(alphaOnly(txt.value)==false){
                /*This field cannot contain numbers*/
                showErrorMessage(errid,"This field cannot contain any numbers.");
                txt.focus();
                return(false);
            }
        }
    }

    
    
    return(true);
}

function validateCheckboxRequired(ctrlname,writeinidx){           
    var chks = document.getElementsByName(ctrlname);
    var lbl=document.getElementById(ctrlname+"label");
    
    /*ALWAYS HIDE THE ERROR AS IT WILL BE SHOWN LATER IF NEEDED*/
    var errid=ctrlname+"error";
    hideErrorMessage(errid);
    
    /*SET THE WRITE IN ID'S AND PARAMETERS*/
    var writeinid = "";var writeintext = "";
    if(writeinidx!=""){
        writeinid=ctrlname+"_"+writeinidx;/*WRITE IN CHECKBOX ID*/
        writeintext=trimStr(document.getElementById(ctrlname+"writein").value);/*WRITE IN TEXTBOX INPUT*/
    }
    
    var selection=false;
    for(i=0;i<chks.length;i++){                   
        if(chks[i].id==writeinid){
            if(chks[i].checked==true && writeintext!=""){
                selection=true;/*WRITE IN WAS SELECTED AND THERE IS A VALUE*/
                i=chks.length;/*EXIT THE LOOP AS THERE IS A SELECTION*/
            }
        }
        else if(chks[i].checked==true){
            selection=true;/*NORMAL CHECK OPTION SELECTED*/
            i=chks.length;/*EXIT THE LOOP AS THERE IS A SELECTION*/
        }
    }
    
    if(selection==false){
        showErrorMessage(errid,"A selection is required from the checkbox group ["+lbl.value+"].");
        if(chks[0]!=null){chks[0].focus();}
        return(false);
    }
    return(true);
}

function validateRadioRequired(ctrlname,writeinidx){           
    var rds = document.getElementsByName(ctrlname);
    var lbl=document.getElementById(ctrlname+"label");
    
    /*ALWAYS HIDE THE ERROR AS IT WILL BE SHOWN LATER IF NEEDED*/
    var errid=ctrlname+"error";
    hideErrorMessage(errid);
    
    /*SET THE WRITE IN ID'S AND PARAMETERS*/
    var writeinid = "";var writeintext = "";
    if(writeinidx!=""){
        writeinid=ctrlname+"_"+writeinidx;/*WRITE IN RADIO ID*/
        writeintext=trimStr(document.getElementById(ctrlname+"writein").value);/*WRITE IN TEXTBOX INPUT*/
    }
    
    var selection=false;
    for(i=0;i<rds.length;i++){                   
        if(rds[i].id==writeinid){
            if(rds[i].checked==true && writeintext!=""){
                selection=true;/*WRITE IN WAS SELECTED AND THERE IS A VALUE*/
                i=rds.length;/*EXIT THE LOOP AS THERE IS A SELECTION*/
            }
        }
        else if(rds[i].checked==true){
            selection=true;/*NORMAL RADIO OPTION SELECTED*/
            i=rds.length;/*EXIT THE LOOP AS THERE IS A SELECTION*/
        }
    }
    
    if(selection==false){
        showErrorMessage(errid,"A selection is required from the radio group ["+lbl.value+"].");
        if(rds[0]!=null){rds[0].focus();}
        return(false);
    }
    return(true);
}

function validateDropdownRequired(ctrlid){
    var ddl=document.getElementById(ctrlid);
    var lbl=document.getElementById(ctrlid+"label");
    var wrtin=document.getElementById(ctrlid+"writein");
    var wrtlbltxt="";
    var wrttxt="";
    var wrtidx=-1;
    var errid=ctrlid+"error";
       
    /*ALWAYS HIDE THE ERROR AS IT WILL BE SHOWN LATER IF NEEDED*/
    hideErrorMessage(errid);
    
    /*SET THE WRITE IN ID'S AND PARAMETERS*/
    if(wrtin!=null){
        wrttxt=trimStr(wrtin.value);/*THERE IS A WRITE IN FOR THIS DROPDOWN*/
        wrtidx=parseInt(ddl.options.length)-parseInt(1);
        wrtlbltxt=ddl.options[parseInt(wrtidx)].text
    } 
       
    var selection=false;
    if(parseInt(ddl.selectedIndex)==parseInt(wrtidx)){
        if(wrttxt!=""){
            selection=true;/*WRITE IN WAS SELECTED AND THERE IS A VALUE*/
        }
        else{
            /*TELL THE USER TO ENTER SOMETHING IN THE WRITE IN FIELD*/
            showErrorMessage(errid,"The current dropdown selection requires input in the text field ["+wrtlbltxt+"].");
        }
    }
    else if(parseInt(ddl.selectedIndex)>0){
        selection=true;/*THERE IS SOMETHING SELECTED*/
    }
    else{
        /*TELL THE USER A SELECTION IS REQUIRED*/
        showErrorMessage(errid,"A selection is required from the dropdown ["+lbl.value+"].");
    }
    
    if(selection==false){
        ddl.focus();
        return(false);
    }    
    return(true);
}

function showErrorMessage(id,text){
    if(document.getElementById(id)!=null){
        document.getElementById(id).style.visibility="visible";
    	document.getElementById(id).innerHTML="<br />"+text;
    }
}

function hideErrorMessage(id){
    if(document.getElementById(id)!=null){
	    document.getElementById(id).style.visibility="hidden";
	    document.getElementById(id).innerHTML="";
	}
}

function trimStr(str){
	/*Trim left*/while(str.substring(0,1)==" "){str=str.substring(1,str.length);}
	/*Trim right*/while(str.substring(str.length-1,str.length)==" "){str=str.substring(0,str.length-1);}
	return str;
}

function numericOnly(txt){
    if(trimStr(txt)!=""){
        /*CHECK FOR ONLY NUMBERS IN THE INPUT*/
        return txt.search(/^[\d]+$/)>=0;
    }
    else{
        return(true);
    }    
}

function alphaOnly(txt){
    if(trimStr(txt)!=""){
        /*CHECK FOR ANY NUMBERS IN THE INPUT*/
        return txt.search(/[\d]+/)==-1;
    }
    else{
        return(true);
    }  
}