﻿var _sFormName = "";
var oMessageDIV;
var FormEditor = function () {

    //Properties
    this.Tables = new Array();
    
}

FormEditor.prototype.Version			= '1.1.0' ;
FormEditor.prototype.ShowError			= 'alert' ;

FormEditor.prototype.TextReplace = function (sMsg,ReplacementList) {

        if (typeof(ReplacementList) == "string") {
            sMsg = sMsg.replace('%1',ReplacementList);
        }
        else if (typeof(ReplacementList) == "object") {
            for (var i=0;i<ReplacementList.length;i++) {
                sMsg = sMsg.replace('%'+(i+1),ReplacementList[i]);
            }
        }
        return sMsg;
}
FormEditor.prototype.AddTable = function (oTable) {
    this.Tables[this.Tables.length] = oTable;
}

FormEditor.prototype.AddControl = function (sTableID, oCtrl) {
    var oTable = this.GetTableByID(sTableID);

    if (oTable) {
        oTable.Ctrls[oTable.Ctrls.length] = oCtrl;
    }
}

FormEditor.prototype.GetTableByID = function (sTableID) {
    var oTable;

    for (var i=0;i<this.Tables.length;i++) {
        if (this.Tables[i].TableID == sTableID) {
            oTable = this.Tables[i];
            break;
        }
    }
    return oTable;
    this.Ctrls[this.Ctrls.length] = oCtrl;
    alert(oCtrl.CtrlID);
   
}

FormEditor.prototype.GetControl = function (sTableID, sControlID) {
    var oTable = this.GetTableByID(sTableID);
    if (oTable) {
        for (var i=0;i<oTable.Ctrls.length;i++) {
            if (oTable.Ctrls[i].ID == sControlID) {
                return oTable.Ctrls[i];
                break;
            }
        }
    }
}

FormEditor.prototype.SetControlValidation = function (sTableID, sControlID , boolEmpty) {
    var oTable = this.GetTableByID(sTableID);
    if (oTable) {
        var oSpan;
        oSpan = document.getElementById( oTable.ID + '_heading_' + sControlID);
        if (oSpan) {
            if (boolEmpty) {
                oSpan.setAttribute("class", "HeaderText optional");
                oSpan.setAttribute("className", "HeaderText optional"); //for ie
            } else {
                oSpan.setAttribute("class", "HeaderText required");
                oSpan.setAttribute("className", "HeaderText required"); //for ie
            }
        }
    }
}

FormEditor.prototype.GetControlByID = function (sTableID, sControlID) {
    var oForm = document.aspnetForm;
    var oTable = this.GetTableByID(sTableID);
    if (oTable) {
        for (var i=0;i<oTable.Ctrls.length;i++) {
            if (oTable.Ctrls[i].CtrlID == sControlID) {
                return oTable.Ctrls[i];
                break;
            }
        }
    }
}

FormEditor.prototype.GetFormControlByID = function (sTableID, sControlID) {
    var oForm = document.aspnetForm;
    var oTable = this.GetTableByID(sTableID);
    if (oTable) {
        for (var i=0;i<oTable.Ctrls.length;i++) {
            if (oTable.Ctrls[i].CtrlID == sControlID) {
                return oForm[oTable.Ctrls[i].ID];
                break;
            }
        }
    }
}

FormEditor.prototype.Check = function (sTableID, sUniqueID) {
    var theForm = document.aspnetForm;
    if (theForm) {
        var oTable = this.GetTableByID(sTableID);
        if (oTable) {
            for (var i=0;i<oTable.Ctrls.length;i++) {
                if (oTable.Ctrls[i].ID == sUniqueID) {
                    var oCheckBox = oTable.Ctrls[i];
                    oCheckBox.DisableUnCheck(theForm);
                    break;
                }
            }
        }
    }
}

FormEditor.prototype.CheckSubmit = function (sMessageDIV) {
    var boolReturn = true;
    var sException = "";
    var sAllException = "";
    var boolAlert = false;
 
    if (_sFormName.length == 0) return (true);
    
    if (this.ShowError == "alert") {
        boolAlert = true;
    }
    
    oMessageDIV = document.getElementById(sMessageDIV);
    oMessageDIV.innerHTML = "";

    for (var i=0;i<this.Tables.length;i++) {
        sException = this.Tables[i].CheckSubmit(theForm , boolAlert);
        
        if (sException.length>0) {
            sAllException += "<li>" + sException + "</li>";
        }
    }

    if (sAllException.length >0) {
        boolReturn = false;
        if (boolAlert == false) {
            oMessageDIV.innerHTML = "<ul class='Errormsg'>" + sAllException + "</ul>";
        }
    }
    else {
        boolReturn = true;
    }
    //boolReturn=true;
    _sFormName = "";
    return boolReturn;
}

FormEditor.prototype.Submitting = function (sControlDIV ,sMessageDIV) {
        var oControlDiv = document.getElementById(sControlDIV);
        if (oControlDiv) {
            if (oMessageDIV) {
               oControlDiv.style.display = "none";
               oMessageDIV.innerHTML = LANG.Waiting;
           }
        }
}

//Table
var FormTable = function (sID, sTableID , sTableName , boolMultiRows) {
    this.ID = sID;
    this.TableID = sTableID;
    this.TableName = sTableName;
    this.MultiRows = boolMultiRows;
    
    //Controls
    this.Ctrls = new Array();
}

FormTable.prototype.CheckSubmit = function (oForm , boolAlert) {
    var sException = "";
    var sAllException = "";
    
    for (var i=0;i<this.Ctrls.length;i++) {
        sException = this.Ctrls[i].CheckSubmit(theForm);
        
        if (sException.length>0) {
            
            sAllException += "<li>" + sException + "</li>";
             
            if (boolAlert == true) {
                this.Ctrls[i].focus(theForm);
                alert(sException);
                break;
            }
        }
    }
    
    return sAllException;
}

//TextBox
var TextBox = function(sID, sCtrlID, sCtrlName, iMinLength , iMaxLength , sMatchPattern ,sMatchErrMsg) {
    this.ID = sID;
    this.CtrlID=sCtrlID;
    this.CtrlName = sCtrlName;
    this.MinLength = iMinLength;
    this.MaxLength = iMaxLength;
    this.MatchPattern = sMatchPattern;
    this.MatchErrMsg = sMatchErrMsg;
}

TextBox.prototype.CheckSubmit = function (oForm) {
    var sException ="";
    var oTextBox = document.getElementById(this.ID);
    if (oTextBox) {
        if (trim(oTextBox.value).length < this.MinLength) {
            sException = FormEditor.TextReplace(LANG.NotEnoughLength, new Array(this.CtrlName,this.MinLength));
        }
        else if (this.MaxLength>0 && trim(oTextBox.value).length > this.MaxLength) {
            sException = FormEditor.TextReplace(LANG.TooMuchLength, new Array(this.CtrlName,this.MaxLength));
        }
        if (this.MatchPattern.length>0) {
            if (string_match(this.MatchPattern,oTextBox.value) == false) {
                if (this.MatchErrMsg.length>0) {
                    sException = this.MatchErrMsg;
                }
                else {
                    sException = FormEditor.TextReplace(LANG.WrongTextFormat, this.CtrlName);
                }
            }
        }
    }
    else {
        sException = "Error TextBox";
    }
    return sException;
}

TextBox.prototype.focus = function (oForm) {
    var oTextBox = document.getElementById(this.ID);
    if (oTextBox.readOnly != true) {
        oTextBox.focus();
    }
}

//TextArea
var TextArea = function(sID, sCtrlID, sCtrlName, iMinLength , iMaxLength) {
    this.ID = sID;
    this.CtrlID=sCtrlID;
    this.CtrlName = sCtrlName;
    this.MinLength = iMinLength;
    this.MaxLength = iMaxLength;
}

TextArea.prototype.CheckSubmit = function (oForm) {
    var sException ="";
    var oTextArea = document.getElementById(this.ID);
    if (oTextArea) {
        if (trim(oTextArea.value).length < this.MinLength) {
            sException = FormEditor.TextReplace(LANG.NotEnoughLength, new Array(this.CtrlName,this.MinLength));
        }
        else if (this.MaxLength>0 && trim(oTextArea.value).length > this.MaxLength) {
            sException = FormEditor.TextReplace(LANG.TooMuchLength, new Array(this.CtrlName,this.MaxLength));
        }
    }
    else {
        sException = "Error TextArea";
    }
    return sException;
}

TextArea.prototype.focus = function (oForm) {
    var oTextArea = document.getElementById(this.ID);
    if (oTextArea.readOnly != true) {
        oTextArea.focus();
    }
}


//RadioBox
var RadioBox = function(sID, sCtrlID, sCtrlName, boolCanNull) {
    this.ID = sID;
    this.CtrlID=sCtrlID;
    this.CtrlName = sCtrlName;
    this.CanNull = boolCanNull;

}
RadioBox.prototype.CheckSubmit = function (oForm) {
    var sException ="";
    if (oForm[this.ID]) {
        if (this.CanNull == false) {
            if (oForm[this.ID].length>0) {
                var boolChecked = false;
                for (var i=0;i<oForm[this.ID].length;i++) {
                    if (oForm[this.ID][i].checked == true) {
                        boolChecked = true;
                        break;
                    }
                }
                
                if (boolChecked == false) {
                    sException = FormEditor.TextReplace(LANG.CannotNull, this.CtrlName);
                }
            }
            else {
                sException = "Error RadioBox";
            }
        }
    }
    else {
        sException = "Error RadioBox";
    }
    return sException;
}

RadioBox.prototype.focus = function (oForm) {
    if (oForm[this.ID]) {
        if (oForm[this.ID].length>1) {
            oForm[this.ID][0].focus();
        }
        else {
            oForm[this.ID].focus();
        }
    }
}

//SelectBox
var SelectBox = function(sID, sCtrlID, sCtrlName, boolCanNull) {
    this.ID = sID;
    this.CtrlID=sCtrlID;
    this.CtrlName = sCtrlName;
    this.CanNull = boolCanNull;

}
SelectBox.prototype.CheckSubmit = function (oForm) {
    var sException ="";
    if (document.getElementById(this.ID)) {
        if (document.getElementById(this.ID).value.length == 0 && this.CanNull == false) {
            sException = FormEditor.TextReplace(LANG.CannotNull, this.CtrlName);
        }
    }
    else {
        sException = "Error SelectBox";
    }
    return sException;
}

SelectBox.prototype.focus = function (oForm) {
    if (document.getElementById(this.ID)) {
        document.getElementById(this.ID).focus();
    }
}

//DateTimePicker
var DateTimePicker = function(sID, sCtrlID, sCtrlName, boolCanNull) {
    this.ID = sID;
    this.CtrlID=sCtrlID;
    this.CtrlName = sCtrlName;
    this.CanNull = boolCanNull;
}

DateTimePicker.prototype.CheckSubmit = function (oForm) {
    var sException ="";
    var oDateTimePicker = document.getElementById(this.ID);
    if (oDateTimePicker) {
        if (trim(oDateTimePicker.value).length == 0 && this.CanNull == false) {
            sException = FormEditor.TextReplace(LANG.CannotNull, this.CtrlName);
        }
    }
    else {
        sException = "Error DatetimePicker";
    }
    return sException;
}

DateTimePicker.prototype.focus = function (oForm) {
    var oDateTimePicker = document.getElementById(this.ID);
    if (oDateTimePicker.readOnly != true) {
        oDateTimePicker.focus();
    }
}

//Image
var ImageBox = function(sID, sCtrlID, sCtrlName, boolCanNull, sAcceptExt, sCurrentImage, sDeleteCheckBoxID) {
    this.ID = sID;
    this.CtrlID=sCtrlID;
    this.CtrlName = sCtrlName;
    this.CanNull = boolCanNull;
    this.AcceptExt = sAcceptExt;
    this.CurrentImage = sCurrentImage;
    this.DeleteCheckBoxID = sDeleteCheckBoxID;
}

ImageBox.prototype.CheckSubmit = function (oForm) {
    var sException ="";
    var oImageBox = document.getElementById(this.ID);
    if (oImageBox) {
        if (trim(oImageBox.value).length == 0 && this.CurrentImage.length == 0 && this.CanNull == false) {
            sException = FormEditor.TextReplace(LANG.CannotNull, this.CtrlName);
        }
        else if (this.AcceptExt.length>0 && trim(oImageBox.value).length>0) {
            var s = ","+ this.AcceptExt.toUpperCase() + ",";
            if (s.indexOf("," + GetFileExtension(oImageBox.value) + ",") == -1) {
                sException = FormEditor.TextReplace(LANG.WrongFileExtension, new Array(this.CtrlName,this.AcceptExt));
            }
        }
        if (sException.length == 0) {
            if (document.getElementById(this.DeleteCheckBoxID)) {
                var oDeleteCheck = document.getElementById(this.DeleteCheckBoxID);
                if (oDeleteCheck.checked == true && this.CanNull == false && trim(oImageBox.value).length == 0) {
                    sException = FormEditor.TextReplace(LANG.CannotNull, this.CtrlName);
                }
            }
        }
    }
    else {
        sException = "Error ImageBox";
    }
    return sException;
}
ImageBox.prototype.focus = function (oForm) {
    var oImageBox = document.getElementById(this.ID);
    if (oImageBox.readOnly != true) {
        oImageBox.focus();
    }
}

//FileBox
var FileBox = function(sID, sCtrlID, sCtrlName, boolCanNull, sAcceptExt, sCurrentFile , sDeleteCheckBoxID) {
    this.ID = sID;
    this.CtrlID=sCtrlID;
    this.CtrlName = sCtrlName;
    this.CanNull = boolCanNull;
    this.AcceptExt = sAcceptExt;
    this.CurrentFile = sCurrentFile;
    this.DeleteCheckBoxID = sDeleteCheckBoxID;
}

FileBox.prototype.CheckSubmit = function (oForm) {
    var sException ="";
    var oFileBox = document.getElementById(this.ID);
    if (oFileBox) {
        if (trim(oFileBox.value).length == 0 && this.CurrentFile.length == 0 && this.CanNull == false) {
            sException = FormEditor.TextReplace(LANG.CannotNull, this.CtrlName);
        }
        else if (this.AcceptExt.length>0 && trim(oFileBox.value).length>0) {
            var s = ","+ this.AcceptExt.toUpperCase() + ",";
            if (s.indexOf("," + GetFileExtension(oFileBox.value) + ",") == -1) {
                sException = FormEditor.TextReplace(LANG.WrongFileExtension, new Array(this.CtrlName,this.AcceptExt));
            }
        }
        
        if (sException.length == 0) {
            if (document.getElementById(this.DeleteCheckBoxID)) {
                var oDeleteCheck = document.getElementById(this.DeleteCheckBoxID);
                if (oDeleteCheck.checked == true && this.CanNull == false && trim(oFileBox.value).length == 0) {
                    sException = FormEditor.TextReplace(LANG.CannotNull, this.CtrlName);
                }
            }
        }
    }
    else {
        sException = "Error FileBox";
    }
    return sException;
}
FileBox.prototype.focus = function (oForm) {
    var oFileBox = document.getElementById(this.ID);
    if (oFileBox.readOnly != true) {
        oFileBox.focus();
    }
}


//TextEditor
var TextEditor = function(sID, sCtrlID, sCtrlName, iMinLength , iMaxLength) {
    this.ID = sID;
    this.CtrlID=sCtrlID;
    this.CtrlName = sCtrlName;
    this.MinLength = iMinLength;
    this.MaxLength = iMaxLength;
}

TextEditor.prototype.CheckSubmit = function (oForm) {
    var sException = "";
    var oEditor = FCKeditorAPI.GetInstance(this.ID);
    
    if (oEditor) {
        if (trim(oEditor.GetHTML()).length < this.MinLength) {
            sException = FormEditor.TextReplace(LANG.NotEnoughLength2, new Array(this.CtrlName,this.MinLength ,oEditor.GetHTML().length));
        }
        else if (this.MaxLength>0) {
            if (trim(oEditor.GetHTML()).length > this.MaxLength) {
                sException = FormEditor.TextReplace(LANG.NotEnoughLength2, new Array(this.CtrlName,this.MinLength ,oEditor.GetHTML().length));
            }
        }
    }
    
    return sException;
}

TextEditor.prototype.focus = function (oForm) {
    var oEditor = FCKeditorAPI.GetInstance(this.ID);
    if (oEditor) {
        oEditor.Focus();
    }
    //alert(oEditor);
}


//CheckBox
var CheckBox = function(sID, sCtrlID, sCtrlName, iCount , iMinSelect , iMaxSelect) {
    this.ID = sID;
    this.CtrlID=sCtrlID;
    this.CtrlName = sCtrlName;
    this.Count = iCount;
    this.MinSelect = iMinSelect;
    this.MaxSelect = iMaxSelect;
}

CheckBox.prototype.CheckSubmit = function (oForm) {
    var sException ="";
    var iTicked = 0;
    var sCheckID = "";
    for (var i=0;i<this.Count;i++) {
        sCheckID = this.ID + "$" + i;
        if (oForm[sCheckID]) {
            if (oForm[sCheckID].checked == true) {
                iTicked+=1;
            }
        }
    }

    if (iTicked < this.MinSelect && this.MinSelect>0) {
        sException = FormEditor.TextReplace(LANG.NoEnoughTick, new Array(this.CtrlName , this.MinSelect));
    }
    else if (iTicked > this.MaxSelect && this.MaxSelect>0) {
        sException = FormEditor.TextReplace(LANG.TooMuchTick, new Array(this.CtrlName , this.MaxSelect));
    }
    
    return sException;
}

CheckBox.prototype.focus = function (oForm) {
    if (oForm[this.ID]) {
        if (oForm[this.ID].length>1) {
            oForm[this.ID][0].focus();
        }
        else {
            oForm[this.ID].focus();
        }
    }
}

CheckBox.prototype.DisableUnCheck = function (oForm) {
    var sCheckID = "";
    var iTicked = 0;
    if (this.MaxSelect>0) {
        for (var i=0;i<this.Count;i++) {
            sCheckID = this.ID + "$" + i;
            if (oForm[sCheckID]) {
                if (oForm[sCheckID].checked == true) {
                    iTicked+=1;
                }
            }
        }
        
        if (iTicked >= this.MaxSelect) {
            for (var i=0;i<this.Count;i++) {
                sCheckID = this.ID + "$" + i;
                if (oForm[sCheckID]) {
                    if (oForm[sCheckID].checked == false) {
                        oForm[sCheckID].disabled = true;
                    }
                }
            }
        }
        else {
            for (var i=0;i<this.Count;i++) {
                sCheckID = this.ID + "$" + i;
                if (oForm[sCheckID]) {
                    if (oForm[sCheckID].disabled == true) {
                        oForm[sCheckID].disabled = false;
                    }
                }
            }
        }
    }
}

//Global Function
function trim (s) {
   //   /            open search
   //     ^            beginning of string
   //     \s           find White Space, space, TAB and Carriage Returns
   //     +            one or more
   //   |            logical OR
   //     \s           find White Space, space, TAB and Carriage Returns
   //     $            at end of string
   //   /            close search
   //   g            global search

   return s.replace(/^\s+|\s+$/g, "");
}

function string_match(sPattern,sValue) {
	var regexp = new RegExp(sPattern);
	var matched = sValue.match(regexp,"g");
	if (matched) {
		return (true);
	}
	else {
		return (false);
	}
}

function GetFileExtension(sFileName) {
    var sExt = "";
    var i = -1;
    i = sFileName.lastIndexOf(".");
    if (i >= 0) {
        sExt = sFileName.substring(i + 1, sFileName.length);
    }
    return sExt.toUpperCase();
}

function form_submit(sFormName) {
    _sFormName = sFormName;
   
}