﻿function GetWindowHeight()
{
   var myHeight = 0;
   if(typeof(window.innerWidth) == 'number' ) {
        myHeight = window.innerHeight;
      } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        myHeight = document.documentElement.clientHeight;
      } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        myHeight = document.body.clientHeight;
      }
  return myHeight;
}

function GetWindowWidth()
{
    var myWidth = 0;
    if(typeof(window.innerWidth) == 'number' ) {
        myWidth = window.innerWidth;
      } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        myWidth = document.documentElement.clientWidth;
      } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        myWidth = document.body.clientWidth;
      }
    return myWidth;
}
function AreAllValidatorsValid() {
    for (i = 0; i < Page_Validators.length; i++) {
        if (!Page_Validators[i].isvalid)
            return false;
    }
    return true;
}
function IsValidationGroupValid(validationGroupName) {
    for (i = 0; i < Page_Validators.length; i++) {
        if (Page_Validators[i].validationGroup == validationGroupName && !Page_Validators[i].isvalid)
            return false;
    }
    return true;
}
function ChangeLanguage(TargetLanguage) {    
    var newLocation = document.location.href;
    if (newLocation.indexOf("SwitchLanguageTo") > -1) {
        //alert("incl. swlt! - " + newLocation.indexOf("SwitchLanguageTo"));
        newLocation = newLocation.substring(0, newLocation.indexOf("SwitchLanguageTo")-1);
    }
    if (newLocation.indexOf("#") > -1) {
        newLocation = newLocation.substring(0, newLocation.indexOf("#"));
    }
    if (newLocation.indexOf("?") > -1) {
        newLocation += "&";
    } else {
        newLocation += "?";
    }
    document.location.href = newLocation + "SwitchLanguageTo=" + TargetLanguage;
}
function ChangeUnits(TargetUnit) {
    var newLocation = document.location.href;
    if (newLocation.indexOf("SwitchUnitsTo") > -1) {
        //alert("incl. swlt! - " + newLocation.indexOf("SwitchLanguageTo"));
        newLocation = newLocation.substring(0, newLocation.indexOf("SwitchUnitsTo") - 1);
    }
    if (newLocation.indexOf("#") > -1) {
        newLocation = newLocation.substring(0, newLocation.indexOf("#"));
    }
    if (newLocation.indexOf("?") > -1) {
        newLocation += "&";
    } else {
        newLocation += "?";
    }
    document.location.href = newLocation + "SwitchUnitsTo=" + TargetUnit;
}


// ###########################################
// AJAX - Framework ##########################
// ###########################################

//  IE XMLHttpRequest Problem Solver
/*@cc_on@if (@_win32 && @_jscript_version >= 5)if (!window.XMLHttpRequest)
    window.XMLHttpRequest = function() { return new ActiveXObject('Microsoft.XMLHTTP') }
@end@*/

var cw_http_request = null;
var cw_http_request_onsuccesshandler = null;
var cw_http_request_onfailhandler = null;
// OLD Function
function SendXMLHttpRequest(page, OnSuccessHandler, OnFailHandler) {

    cw_http_request_onsuccesshandler = OnSuccessHandler;
    cw_http_request_onfailhandler = OnFailHandler;
    cw_http_request = new XMLHttpRequest();
    cw_http_request.open("GET", page, true);
    cw_http_request.onreadystatechange = OnRequestOK;
    cw_http_request.send(null);

}
// Post geht so aber nicht wirklich......
// TODO: auf fragezeichen in paramString prüfen...
function SendAjaxRequest(method, url, paramString, OnSuccessHandler, OnFailHandler) {
    cw_http_request_onsuccesshandler = OnSuccessHandler;
    cw_http_request_onfailhandler = OnFailHandler;

    cw_http_request = new XMLHttpRequest();
    if (method == "GET") {
        cw_http_request.open("GET", url + "?" + paramString, true);
        cw_http_request.onreadystatechange = OnRequestOK;
        cw_http_request.send(null);
        return true;
    }
    else if (method == "POST") {
        cw_http_request.open("POST", url, true);
        cw_http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        cw_http_request.setRequestHeader("Content-llength", paramString.length);
        cw_http_request.setRequestHeader("Connection", "close");
        cw_http_request.onreadystatechange = OnRequestOK;
        cw_http_request.send(paramString);
        return true;
    }
    else
        return false;
}

function OnRequestOK() {
    if (cw_http_request.readyState == 4) {
        if (cw_http_request.status == 200) {
            var myJSONObj = eval("(" + cw_http_request.responseText + ")");
            //OnSendXMLHttpRequestSuccess(myJSONObj);
            eval(cw_http_request_onsuccesshandler + "(myJSONObj)");
        } else {
            //OnSendXMLHttpRequestFailed(cw_http_request.status);
            eval(cw_http_request_onfailhandler + "(cw_http_request.status)");
        }
    }
}