﻿
var count = 0;
var userWasNotified = false;
function Reconnect(root_url) {
    count++;
    if (count < max) {
        window.status = 'Link to Server Refreshed ' + count.toString() + ' time(s)';
        var img = new Image(1, 1);
        img.src = 'KeepAlivePage.aspx';
    }
    else {
      count = 0;
      
//      var rootdomain = "http://" + window.location.hostname
//      if (window.location.port.length > 0) {
//        rootdomain += ':' + window.location.port;
//      }

//      var url = document.location.toString(); //url
//      var e_url = ''; //edited url
//      var p = 0; //position
//      var p2 = 0; //position 2
//      p = url.indexOf("//");
//      e_url = url.substring(p + 2);
//      p2 = e_url.indexOf("/");
//      var root_url = url.substring(0, p + p2 + 3);


      window.location = root_url + '/default.aspx?signout=1';
    }
}

function ResetReconnectCount() {
    if (count > max - (max * 0.2) && userWasNotified == false) {
        var answer = confirm('In orer to keep your information safe you will be logged out. Do you wish to keep working on this page ?');
        if (answer) {
            count = 1;
        }
    }
}

//function CalendarExtender_Toggle(behaviorId) 
//{
//  var behavior = $find(behaviorId);
//  if (behavior != null) {
//    if (behavior._isOpen == true) {
//      try {
//        behavior.hide();
//      }
//      catch (e) { }
//    }
//    else if (behavior._isOpen == false) {
//      try {
//        behavior.show();
//      }
//      catch (e) { }
//    }
//  }
//} 


function clearTextBox(controlId) {
    var txtBox = document.getElementById(controlId + "_txtBox");
    txtBox.value = '';
}


/*************Print******************/

//Notice: his is not cross browser
function printpr() {
//    var OLECMDID = 7;
//    /* OLECMDID values:
//    * 6 - print
//    * 7 - print preview
//    * 1 - open window
//    * 4 - Save As
//    */

//    var PROMPT = 1; // 2 DONTPROMPTUSER
//    var WebBrowser = "<OBJECT ID='WebBrowser1' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></OBJECT>";
//    document.body.insertAdjacentHTML("beforeEnd", WebBrowser);

//    WebBrowser1.ExecWB(OLECMDID, PROMPT);
  //    WebBrowser1.outerHTML = "";
window.print();
}


/*******************************/







function hideCalendar(calExtender) {
    calExtender.hide();
}

function showHide(divId) {
    var div = document.getElementById(divId);
    if (div != null) {
        if (div.style.display == "none") {
            div.style.display = "block";
        }
        else {
            div.style.display = "none";
        }
    }
}
function show(divId) {
    var div = document.getElementById(divId);
    if (div != null) {
        div.style.display = "block";
    }
}

function hide(divId) {
    var div = document.getElementById(divId);
    if (div != null) {
        div.style.display = "none";
    }
}

// function clearField(txtBoxId,h) {
//   alert(txtBoxId);
//   alert(h);
//   //alert(txtBoxId.length);
////   var txtBox = document.getElementById(txtBoxId+"_txtBox");
////   txtBox.value = "";
// }


///////////////////***Master**//////////////////////


//Creat a new validation summary
function createSummary(summaryId, validationGroup) {
    if (typeof (validationArray[validationGroup]) == 'undefined') // if doesn't exist, define
    {
        validationArray[validationGroup] = new Valid(validationGroup);
    }
    validationArray[validationGroup].setSummaryId(summaryId);
}

function SetTabForValidation(validationGroup, tabHeaderId) {
    if (typeof (validationArray[validationGroup]) == 'undefined') // if exist, set Tab id
    {
        validationArray[validationGroup] = new Valid(validationGroup);
    }
    validationArray[validationGroup].setTabHeaderId(tabHeaderId);
}






//check validation of control, if summary show flag is on, show summary
function validate_required(txtBoxId, labelId, validationGroup) {
    var txtBox = document.getElementById(txtBoxId);
    var label = document.getElementById(labelId);
    var IsValidForServer = document.getElementById(txtBoxId + "IsValid");

    var validationObj = validationArray[validationGroup];

    if (validationGroup == null || (typeof (validationObj) == 'undefined')) // if no group was supplied, or it doesn't exist,use default
    {
        validationObj = validationArray["page"];
    }

    if (txtBox.value == null || txtBox.value == "")  //Not Valid
    {
        label.className = "labelNotValid";
        IsValidForServer.value = "0";
        validationObj.addError(txtBoxId, "Please provide " + label.innerHTML);

    }
    else //Valid
    {
        label.className = "labelValid";
        IsValidForServer.value = "1";
        validationObj.deleteError(txtBoxId)

    }
    //if show summary is on, handle its presentation 
    if (validationObj.isShow) {
        validationObj.ShowErrorSummary();
    }
    //if validation has a tab connected to it, handle it
    if (validationObj.tabHeaderId != null) {
        var tabHeader = document.getElementById(validationObj.tabHeaderId);
        if (tabHeader != null) {
            tabHeader.className = (validationObj.isValid()) ? "tabHeader" : "tabHeaderError";
        }
    }
}

var summaryArray = new Array();

var validationArray = new Array();
validationArray["page"] = new Valid("page"); //page is a default group, need validationSummary with group name "page" in order to show summary



//////////////not used might need some working//////////
function ShowValidationSummary(validationGroup) {
    if (validationGroup == null) {
        validationArray["page"].ShowErrorSummary();
    }
    else if (typeof (validationArray[validationGroup]) != 'undefined') // if exist, show
    {
        validationArray[validationGroup].ShowErrorSummary();
    }
}

function IsGroupValid(validationGroup) {
    if (validationGroup == null) {
        return validationArray["page"].isValid()
    }
    else if (typeof (validationArray[validationGroup]) != 'undefined') // if exist, show
    {
        return validationArray[validationGroup].isValid();
    }
}
//////////////////////////


///////////////////////***stam test***////////////////////






function alertError() {

    ShowValidationSummary("general");
    ShowValidationSummary("page");
}








//////////**********////////////////
//////////**********////////////////

function Valid(groupName) {
    this.groupName = groupName;
    this.summaryDivId = ""; //the id  of the div the page that show the summary
    this.errorArray = new HashTable();
    this.isShow = true;
    this.tabHeaderId = ""; //the id  of the tab that the group is connected to

    this.setSummaryId = function(summaryId) { this.summaryDivId = summaryId; };
    this.setIsShow = function(isShow) { this.isShow = isShow; };
    this.setTabHeaderId = function(tabHeaderId) { this.tabHeaderId = tabHeaderId; };

    this.isValid = function() { return (!this.errorArray.length > 0) };
    this.addError = function(controlId, errorMsg) { this.errorArray.put(controlId, errorMsg); };
    this.deleteError = function(controlId, errorMsg) { this.errorArray.remove(controlId); };

    this.getErrorString = function() {
        var errorString = "";
        for (var i in this.errorArray.hashArr) {
            errorString = errorString + "<li>" + this.errorArray.hashArr[i] + '</li>';
        }
        errorString = "<ul>" + errorString + "</ul>"
        return errorString;
    };

    this.ShowErrorSummary = function() {
        if (this.summaryDivId != "") {
            summaryDiv = document.getElementById(this.summaryDivId);
            summaryServer = document.getElementById(this.summaryDivId + "Server");

            if (!this.isValid()) {
                summaryDiv.innerHTML = this.getErrorString();
                summaryDiv.style.display = "block"
                if (summaryServer != null) {
                    summaryServer.value = summaryDiv.innerText;
                }
            }
            else {
                summaryDiv.style.display = "none"
                if (summaryServer != null) {
                    summaryServer.value = "";
                }
            }
        }
    };


    // return true;
}

// var validationArray2 = new HashTable(); // new Valid("general");
// validationArray2.put("b", "b");


// validationArray2.has("b");


////////////////////////////////////////
//////////////Hash Table/////////////////
//////////////////////////////////////
function HashTable() {

    this.hashArr = new Array();

    this.length = 0;

}

HashTable.prototype.get = function(key) {

    return this.hashArr[key];

};

HashTable.prototype.put = function(key, value) {

    if (typeof (this.hashArr[key]) == 'undefined') {

        this.length++;

    }

    this.hashArr[key] = value;

};

HashTable.prototype.remove = function(key) {

    if (typeof (this.hashArr[key]) != 'undefined') {

        this.length--;

        var value = this.hashArr[key];

        delete this.hashArr[key];

        return value;

    }

};

HashTable.prototype.has = function(key) {

    return (typeof (this.hashArr[key]) != 'undefined');

};























//function Validate(txtBoxId,labelId) {
//  var txtBox = document.getElementById(txtBoxId);
//  var label = null;
//  if (labelId != null && labelId!="") {
//    label = document.getElementById(labelId);
//  }
//   // alert(txtBox.value);
//    if (txtBox.value == "") {
//      txtBox.className = "notValid"
//      if (label != null) {
//      label.className = "labelNotValid"
//      }
//    }
//    else {
//      txtBox.className = "valid"
//      if (label != null) {
//        label.className = "labelValid"
//      }
//    }

//  }
//  
//  function validateInput(oSrc, args) {
//    alert(args.IsValid + args.Value);
//    args.IsValid = (args.Value % 5 == 0);
//  }


//  function checkDate(sender, args) {
//    
//    //create a new date var and set it to the
//    //value of the senders selected date
//    var selectedDate = new Date();
//    selectedDate = sender._selectedDate;
//    //create a date var and set it's value to today
//    var todayDate = new Date();
//    var mssge = "";

//   // alert((selectedDate < todayDate) + selectedDate + " todayDate " + todayDate);
//    if (selectedDate < todayDate) {
//      //set the senders selected date to today
//      sender._selectedDate = todayDate;
//      //set the textbox assigned to the cal-ex to today
//      sender._element.innerText = sender._selectedDate.format(sender._format);
//      //alert the user what we just did and why
//      alert("Warning! - Date Cannot be in the past" + todayDate);
//    }
//  }




//  

