var validatorNamesArray = new Array();
var validatorsArray = new Array();

var Sorted_Validators = null;

function doSortedValidators(){
    if(Sorted_Validators==null) {
        if(Sorted_Validators_PrePopulate) Sorted_Validators=Page_Validators;
        else{
            Sorted_Validators = new Array(Page_Validators.length);
            currSort=0;
            for(x=0;x<theForm.elements.length && currSort<Sorted_Validators.length;x++){
                theid=theForm.elements[x].id;
                for(y=0;y<Page_Validators.length && theid!="" && theid.indexOf('chxStandardsSelectStandardItem') == -1;y++)
                    if(theid==Page_Validators[y].controltovalidate || 
                        theid=='RADIOBUTTONLISTECHALKER'+Page_Validators[y].controltovalidate){
                        Sorted_Validators[currSort]=Page_Validators[y];
                        currSort++;
                    }
            }
        }
    }
    return Sorted_Validators;
}

function doeChalkValidation(vG, overrideFocus, skipSort, noPrompt){
    if (typeof(Page_Validators) == "undefined") return true;
    Sorted_Validators1=Page_Validators;
    if(!skipSort) Sorted_Validators1=doSortedValidators();
    if(vG==null || vG=="undefined" || vG=="") vG="eChalkDefaultValidationGroup";
    if(overrideFocus!=true) overrideFocus=false;
    vG=vG.split(",");
    var retArr=new Array();
    for (i = 0; i < Sorted_Validators1.length; i++) {
        val=Sorted_Validators1[i];
        if(val!=null){
            var x=0;
            for(;x<vG.length;x++) if(val.validationGroup.toLowerCase()==vG[x].toLowerCase()) break;
            if(x<vG.length){
                for(ind=0;ind<validatorNamesArray.length;ind++)
                    if(validatorNamesArray[ind]==val.id) break;
                if(ind!=validatorNamesArray.length){
                    origId=val.controltovalidate;
                    if(isElementVisible(document.getElementById(origId))){
                        resetValue="";
                        for(x=0;x<TextBoxMasks.length && resetValue=="";x++)
                            if(TextBoxMasks[x][0]==origId)
                                if(document.getElementById(origId).value==TextBoxMasks[x][1]){
                                    resetValue=TextBoxMasks[x][1];
                                    document.getElementById(origId).value="";
                                }
                        
                        val.popTitle=(validatorsArray[ind])[0];
                        val.popPage=(validatorsArray[ind])[1];
                        val.popParams=(validatorsArray[ind])[2];
                        if(document.getElementById(val.controltovalidate).type=="text" || document.getElementById(val.controltovalidate).type=="textarea")
                            document.getElementById(val.controltovalidate).value=trim(document.getElementById(val.controltovalidate).value);
                        if((validatorsArray[ind])[4]==1 || (validatorsArray[ind])[4]==2 || (validatorsArray[ind])[4]==3) {
                            if((validatorsArray[ind])[4]==3)
                                ret=GridValidatorFunc(val, (validatorsArray[ind])[5], (validatorsArray[ind])[6]);
                            else if((validatorsArray[ind])[4]==2) ret=ListValidatorFunc(val, (validatorsArray[ind])[4], (validatorsArray[ind])[5], (validatorsArray[ind])[6]);
                            else if((validatorsArray[ind])[4]==1) ret=CheckValidatorFunc(val, (validatorsArray[ind])[4], (validatorsArray[ind])[5], (validatorsArray[ind])[6]);
                            
                            isValid=ret==0;
                            if(ret>0){
                                val.popTitle=(validatorsArray[ind])[7];
                                val.popPage=(validatorsArray[ind])[8];
                                val.popParams=(validatorsArray[ind])[9];
                            }
                        }
                        else isValid=val.evaluationfunction(val);
                        if(!isValid){
                            if(noPrompt)
                                retArr[retArr.length]=val.popParams;
                            else {
                                val.focusonerror=(validatorsArray[ind])[3];
                                if((validatorsArray[ind])[4]==1) origId+="_"+0;
                                aspnet_confirmWindow(val.popTitle, val.popPage, "if("+val.focusonerror+" || "+overrideFocus+") try{window.opener.theForm."+origId+".focus();}catch(e){}" , "", "", 300, 150, "OK", "", "", "", val.popParams, ",", "true");
                                if(resetValue!="") document.getElementById(origId).value=resetValue;
                                return false;
                            }
                        }
                        if(resetValue!="") document.getElementById(origId).value=resetValue;
                    }
                }
            }
        }
    }
    if(!noPrompt) retArr=true;
    return retArr;
}

function addValidationArray(validatorID, popTitle, popPage, popParams, focusonerror, isCheckBoxList, minSelected, maxSelected, maxSelectedpopTitle, maxSelectedpopPage, maxSelectedpopParams){
    validatorNamesArray[validatorNamesArray.length] = validatorID;
    validatorsArray[validatorsArray.length] = new Array(popTitle, popPage, popParams, focusonerror, isCheckBoxList, minSelected, maxSelected, maxSelectedpopTitle, maxSelectedpopPage, maxSelectedpopParams);
}

function GridValidatorFunc(val, minNum, maxNum){
    selCount=0;
    for(x=2;true;x++){
        y=x;
        if(y<10)y="0"+trim(x);
        if(document.getElementById(val.controltovalidate+"_ctl"+y+"_ctl00")==null) break;
        if(document.getElementById(val.controltovalidate+"_ctl"+y+"_ctl00").checked) selCount++;
    }
    if(selCount<minNum) return -1;
    if(selCount>maxNum && maxNum > 0) return 1;
    return 0;
}

function CheckValidatorFunc(val1, type, minNum, maxNum){
    var elem;
    var x=0;
    doMaxNum=maxNum!=0;
    do{
        eval('elem=theForm.'+val1.controltovalidate+'_'+x);
        if(elem!=null){
            if(elem.checked){
                minNum--;
                if(doMaxNum) maxNum--;
                if(maxNum<0) return 1;
            }
        }
        x++;
    }while(elem!=null)
    eval('elem=theForm.'+val1.controltovalidate);
    if(minNum>0 && elem!=null) return -1;
    return 0;
}
    
 function ListValidatorFunc(val1, type, minNum, maxNum){ 
    eval('elem=theForm.'+val1.controltovalidate);
    doMaxNum=maxNum!=0;
    if (elem != null) {
        r=elem.options;
        for(x=0;x<r.length;x++) 
            if(r[x].selected){
                minNum--;
                if(doMaxNum) maxNum--;
                if(maxNum<0) return 1;
            }
        if(minNum>0) return -1;
    }
    return 0;
}

function FileUploadValidatorFunc(sender, args){
    blockedExtensions=singleFileUploadBlockedExtensions.split(",");
    for(x=0;x<blockedExtensions.length;x++)
        if(trim(args.Value.substring(args.Value.length-blockedExtensions[x].length,args.Value.length)).toLowerCase()==blockedExtensions[x].toLowerCase())
            args.IsValid=false;
}

var TextBoxMasks = new Array();

function addTextBoxMask(id,mask){
    TextBoxMasks[TextBoxMasks.length]=new Array(id,mask);
    if(document.getElementById(id)!=null)
        if(document.getElementById(id).value=="")
            document.getElementById(id).value=mask;
}

function undoTextBoxMasks(){
    for(x=0;x<TextBoxMasks.length;x++){
        elm=document.getElementById(TextBoxMasks[x][0]);
        if(elm!=null) if(elm.value==TextBoxMasks[x][1]) elm.value="";
    }
}

function applyTextBoxMask(id,mask,bOnBlur){
    elm=document.getElementById(id);
    if(elm.value=="" && bOnBlur) elm.value=mask;
    if(elm.value==mask && !bOnBlur) elm.value="";
}

function GridViewSelectAll(id,id2,isCheckImage){
    var x=2;
    for(;eval("theForm."+id2+"$ctl"+(x<10?"0"+x:x)+"$ctl01")!=undefined;x++){}
    isChecked=document.getElementById(id+"_ctl"+(x<10?"0"+x:x)+"_checkAllChecked").checked;
    if(isCheckImage) {
        isChecked=!isChecked;
        document.getElementById(id+"_ctl"+(x<10?"0"+x:x)+"_checkAllChecked").checked=isChecked;
    }
    x--;
    for(;x>1;x--) document.getElementById(id+"_ctl"+(x<10?"0"+x:x)+"_ctl00").checked=isChecked;
}

function GridViewUndoSelectAll(id, ck){
    var x=0;
    for(x=2;!ck.checked;x++)
        if(document.getElementById(id+"_ctl"+(x<10?"0"+x:x)+"_ctl00")==null) break;
    if(x>2) document.getElementById(id+"_ctl"+(x<10?"0"+x:x)+"_checkAllChecked").checked=false;
}


//Removing this function will break the maxlength property used
//in lesson planning
//onkeyup="return ismaxlength(this)"
function ismaxlength(obj){
    var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
    v=obj.id+"variableformaxlen";
    if (obj.getAttribute && obj.value.length>mlength){
        doeChalkValidation(v,false,false);
        eval("obj.value="+v);
    }
    eval(v+"=obj.value");
}

if (typeof (Sys) != 'undefined') 
    Sys.Application.notifyScriptLoaded();
