﻿//lgramatikov, Follow MS coding conventions: functions start with small letter, open brackets are on the same row as code.
// dterziev, shortcuts.

var YesNo = {
    Yes: "1"
    , No: "0"
    , Partially: "2"
    , Undefined: ""
};

var HouseType = {
    Unknown: 0
    , TweeOnderEenKap: 1
    , Etage: 3
    , GrachtenPand: 4
    , Flat: 8
    , RecreatieWoning: 10
    , RijtjesHuis: 11
    , Vrijstaand: 13
    , Boerderij: 14
    , Woonboot: 15
    , Woonwagen: 16
}
            
            
function $G(fieldName) {
    return $find("state").get_value(fieldName); 
}
function $S(fieldName, value, raiseEvent) {
    if(raiseEvent !== null && raiseEvent === false)
        $find("state").setValueNoEvent(fieldName,value);
    else
        $find("state").set_value(fieldName,value);
}
function $C(f) {
    var i = $find("idcntrl").get_controlIdByFieldName(f);
    if(i != null) return $get(i);
    return null;
}
function $E(f, e) {
    var c = $C(f); 
    if(c!=null)
    {
        if(c.tagName == "INPUT")
            c.readOnly = !e;
        else
            c.disabled = !e;
    }
}
function $HL(f){
    var c = $C(f); !c || highlightSelect(c.parentNode.parentNode);
}
function $F(f){
    var c = $C(f); !c || c.focus();
}
function $FF(f){
    var id = $find("idcntrl").get_controlIdByFieldName(f); 
    if(id !== null) 
        return $find(id);
    else
        return null;
}
//lgramatikov, no need for this. use $find(your_control_id).get_fieldName();
//function $FID(c){
//    var fid = $find("idcntrl").get_fieldNameByControlId(c);
//    return fid;
//}
function $addCss(e,c) {
    if(typeof(e) == "string") e = $get(e);
    if(e !== null) Sys.UI.DomElement.addCssClass(e,c);
}

//calls trace with label and value. i'm always forgetting that JS trace does not accept text and value, but only text
function $T (l, v) {
	if (typeof(v)==="undefined") {
	    Sys.Debug.trace (l);
	}
	else {
	    Sys.Debug.trace (l+":"+v);
	}
}

//same as $T, but aligned with .NET Debug.write, where first comes value, then category (label in our case)
function $TVL (v,c) {
    var t = new Date()
    
    var ts = "["+t.getHours()+":"+t.getMinutes()+":"+t.getSeconds()+":"+t.getMilliseconds()+"],";
    if (typeof(v)==="undefined") {
	    Sys.Debug.trace (ts+c);
	}
	else {
	    Sys.Debug.trace (ts+c+":"+v);
	}
}

function $removeCss(e,c){
    if(typeof(e) == "string") e = $get(e);
    if(e !== null) Sys.UI.DomElement.removeCssClass(e,c);
}
function $toggleCss(e,c){
    if(typeof(e) == "string") e = $get(e);
    if(e !== null) Sys.UI.DomElement.toggleCssClass(e,c);
}

function highlightSelect(target) {
	if (typeof(AjaxControlToolkit)!="undefined") {
		AjaxControlToolkit.Animation.ColorAnimation.play(target, 0.5, 10, "style", "backgroundColor", "#ffff66", "#ffffff");
	}
}

function showElement (elementId) {
    if(typeof(elementId) != "string") return;
    if ($get(elementId).style.display === "") return;

    $get(elementId).style.display="";
    highlightSelect($get(elementId));
}

function showElementNoHL (elementId) {
    if(typeof(elementId) != "string") return;
	var container = $get(elementId);
    if (container.style.display === "") return;

    container.style.display="";
	EnableDisableValidators(container, true);
}

function showInline (elementId) {
    if(typeof(elementId) != "string") return;
	var b = $get(elementId);
	if (b===null) return;
    if (b.style.display === "inline") return;
    b.style.display="inline";
}
//lgramatikov, showElement as it is right now will not work for elements which have inline style set to none.
// For some reason it is needed as it is by someone, despite the "small" issue with inline styles.
function showBlock (elementId) {
	var b = $get(elementId);
	if (b===null) return;
    if (b.style.display === "block") return;

    b.style.display="block";
    highlightSelect(b);
}

function hideElement (elementId) { 
    if(typeof(elementId) !== "string") return;
    if($get(elementId).style.display === "none") return;

    $get(elementId).style.display="none";
}

function hideBlock (elementId) { 
	var b = $get(elementId);
	if (b===null) return;
    if(b.style.display === "none") return;
	b.style.display="none";
}

function showContainer(elementId) { 
    showHideContainer(elementId, true);
}

function hideContainer(elementId, bShow) {
    showHideContainer(elementId, false);
}

function showHideBlockContainer(containerId, bShow){

	var container = $get(containerId);
	
	if (bShow === true) {
		showBlock(containerId);
	}
	else {
		hideBlock(containerId);
	}
	
	EnableDisableValidators(container, bShow);
}

function showHideContainer(containerId, bShow) {
    
    var container = $get(containerId);
    
    if(bShow === true) {
        showElement(containerId);
    }
    else {
        hideElement(containerId);
    }
	
	EnableDisableValidators(container, bShow);
}

function EnableDisableValidatorsForControl (controlId, bIsEnabled) {
	var ctl = $get(controlId);
	if (ctl === null) return;
	if (ctl.Validators !== "undefined") {
		var validators = ctl.Validators;
		if (validators !== null) {
			for (var k = 0, l = ctl.Validators.length; k < l; k++) {
				ctl.Validators[k].enabled = bIsEnabled;
				if (bIsEnabled) {
					var ctlVal = ValidatorGetValue(ctl.Validators[k].controltovalidate);
					if (typeof(ctlVal) !== "undefined" && ctlVal != null && ctlVal != "") {
						ValidatorValidate(ctl.Validators[k]);
					}
				}
			}
			ValidatorUpdateIsValid();
		}
	}
}

function EnableDisableValidators (container, bIsEnabled) {
    if (container === null) return;
	var tags = container.getElementsByTagName("input");
    for(var i = 0, j = tags.length; i < j; i++) {
        if (typeof(tags[i].Validators) !== "undefined") {
			var validators = tags[i].Validators;
			if (validators !== null) {
				for (var k = 0, l = tags[i].Validators.length; k < l; k++) {
					tags[i].Validators[k].enabled = bIsEnabled;
				    if(bIsEnabled) {
				        var ctlVal = ValidatorGetValue(tags[i].Validators[k].controltovalidate);
				        if(typeof(ctlVal) !== "undefined" && ctlVal != null && ctlVal != "")
				        {
				            ValidatorValidate(tags[i].Validators[k]);
				        }
				    }
				}
			}
		}
    }
    ValidatorUpdateIsValid();
}


function ValidateField(id, element) {
    var ctl;
	if (typeof(element) != "undefined") {
		ctl = $get(id, element);
	}
	else {
		ctl = $get(id);
	}
    if(ctl == null) return false;
    var isValid = true;
	if (typeof(ctl.Validators) !== "undefined") {
		var validators = ctl.Validators;
		if (validators !== null) {
			for (var i = 0; i < validators.length; i++) {
				ValidatorValidate(validators[i], null, null);
				if(validators[i].isvalid === false)
				    isValid = false;
			}
			ValidatorUpdateIsValid();
		}
	}
	return isValid;
}
	
function clearContainerControls (containerId) {
    var inputs = $get(containerId).getElementsByTagName("INPUT");
    var cntrl = null;
    var cntrlId = null;

    for(var i = 0, il = inputs.length; i < il; i++) {
        if (inputs[i].id !== null) {
            cntrl = $find(inputs[i].id);
            if (cntrl!==null) {
                cntrl.clear_value();
            }
            else {
                inputs[i].value="";
            }
        }
        else {
            Sys.Debug.trace ("clearContainerControls: container -"+containerId+" has an element "+i+" with null ID");
        }
    }
}

function showErrorMessage(message) {
    alert(message);
}

//lgramatikov, I know, this should not be touched. But allowDot check would never execute in previous version.
function isKeyDigit (k, allowDot) {
    var n = Sys.CultureInfo.CurrentCulture.numberFormat;
    var r = (((k>=48) && (k <= 57)) || (k==0) || (k==Sys.UI.Key.enter) || (k==Sys.UI.Key.tab) || (k==Sys.UI.Key.backspace) || (k==Sys.UI.Key.right) || (k==Sys.UI.Key.del));
	if (allowDot === false) {
		if (r === true) 
			return true;
	}
	else {
		if (k == n.CurrencyDecimalSeparator.charCodeAt(0)) 
			return true;
	}
    return false;
}

function sia (cts) {
    
    Array.clear(inputs); 
    var is= document.getElementsByTagName("INPUT");
 	var sls= document.getElementsByTagName("SELECT");
//for (var i=0, len=arr.length; i<len; i++) {}
    for (var i=0, isl = is.length; i< isl;i++) {
        if (((is[i].type.toLowerCase()==="text") || (is[i].type.toLowerCase()==="radio") || (is[i].type.toLowerCase()==="checkbox")) && (is[i].disabled===false)) {
            if (is[i].id !== cts) {
                is[i].disabled = true;
				Sys.UI.DomElement.addCssClass(is[i], "disableBg");
                Array.add(inputs, is[i].id);
            }
        }
    }
	
	if (sls.length > 0) {
	    for (var k = 0, slsl = sls.length; k < slsl; k++) {
		    sls[k].disabled = true;
			Sys.UI.DomElement.addCssClass(sls[k], "disableBg");
            Array.add(inputs, sls[k].id);
		}
	}
}

function disableHelps(cts) {
    var helpLinks= document.getElementsByTagName("IMG");
	var helpIcoName = "icon_help.gif";
	var helpIcoOffUrl = "/Resources/ImagesM/icon_help_off2.gif";
	var hell = helpLinks.length;
	if (hell > 0) {
	    for (var i = 0; i < hell; i++) {
			//> 0 is safe here, because this image is always with some path in front of it
		    if ((helpLinks[i].id !== cts) && (helpLinks[i].src.indexOf(helpIcoName) > 0)) {
				helpLinks[i].src =helpIcoOffUrl; 
				helpLinks[i].style.cursor="default";
			}
		}
	}
}

function enableHelps(containerId){

 	var helpLinks;
	if (typeof(containerId) == "undefined") {
		helpLinks = document.getElementsByTagName("IMG");
	}
	else {
		helpLinks = $get(containerId).getElementsByTagName("IMG");
	}
	var helpIcoName = "icon_help_off2.gif";
	var helpIcoUrl = "/Resources/ImagesM/icon_help.gif";
	var hell = helpLinks.length;
	if (hell > 0) {
	    for (var i = 0; i < hell; i++) {
			//> 0 is safe here, because this image is always with some path in front of it
		    if (helpLinks[i].src.indexOf(helpIcoName) > 0) {
				helpLinks[i].src =helpIcoUrl; 
				helpLinks[i].style.cursor="pointer";
			}
		}
	}
	
	if (typeof(enabledHelpFields)!="undefined") {
		enabledHelpFields = null;
	}
}

function show_ep(dialogId) {
	var ddc = $find("ddc");
    if (ddc !== null) {
		ddc.show_dialog(dialogId);
	}
	else {
		$create(Dtz.Mrtg.UI.DialogDisplayController, {id:"ddc"}, null, null, null);
		ddc = $find("ddc");
		if (ddc === null) {
			throw Error.invalidOperation("Can't find DialogDisplayController for dialog with id:" + dialogId);
		}
		else {
			ddc.show_dialog(dialogId);
		}
	}
}

function hide_ep () {
	var ddc = $find("ddc");
    if (ddc !== null) {
		ddc.hide_dialog();
	}
	else {
		$create(Dtz.Mrtg.UI.DialogDisplayController, {id:"ddc"}, null, null, null);
		ddc = $find("ddc");
		if (ddc === null) {
			throw Error.invalidOperation("Can't find DialogDisplayController for dialog with id:" + dialogId);
		}
		else {
			ddc.hide_dialog();
		}
	}
}

function reg_ep (htId, epId) {
    var idc = $find("idcntrl");
    if (idc !== null) {
        idc.set_value(htId, epId);
    }
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
