﻿

function PopUpLink(Url, Title, Width, Height) {
    window.open(Url, Title, 'menubar=0,resizeable=1,scrollbars=1,width=' + Width + ',height=' + Height);
}


function Trim(Value) {
    return Value.replace(/^\s+|\s+$/g, "");
}


function GetParameterByName(strName) {

    var urlParts = document.URL.split('?');
    if (urlParts.length < 2) {
        return "";
    }

    var params = urlParts[1].split('&');

    for (var i = 0; i < params.length; i++) {
        if (params[i].split('=')[0] == strName) {
            params = params[i].split('=')[1];
            break;
        }
    }

    return params;
}


function DaysDifference(date1, date2) {
    if (!date1 || !date2) { return 0; }
    var t1 = date1.getTime();
    var t2 = date2.getTime();
    var ms = t2 - t1;
    var s = ms / 1000;
    var h = s / 3600;
    var d = h / 24;
    if (d < 0) { d = 0; }
    d = Math.round(d);
    return d;
}


/*  Form pre-selection */

function SelectRadioButtonList(inputName, value) {
    if(!value){
        value = GetParameterByName(inputName);
    }
    if (!value || value.length == 0) {
        $('input[name="' + inputName + '"]:eq(0)').attr("checked", true);
    } else {
        $('input[name="' + inputName + '"]').filter("[value=" + value + "]").attr("checked", true);
    }
}
function SelectDropDown(inputName, value){
    if(!value){
        value = GetParameterByName(inputName);
    }
    if (value && value.length > 0) {
        $('select[name="' + inputName + '"]').val(value);
    }
}
function SelectCheckBox(inputName, value) {
    if (!value) {
        value = GetParameterByName(inputName);
    }
    if (value && value.length > 0) {
        $('input[name="' + inputName + '"]').attr("checked", true);
    } 
}
function SelectTextBox(inputName, value) {
    if (!value) {
        value = GetParameterByName(inputName);
    }
    if (value && value.length > 0) {
        $('input[name="' + inputName + '"]').val(value);
    }
}

//use attribute name for query string name
function PrefilValuesFromUrlFactory(ParentFilterPnlId) {
    var url = window.location.href;
    var hasUrlQuery = url.indexOf("?") > 1;
    if (hasUrlQuery) {
        var splitUrl = url.split("?");
        var rawUrl = splitUrl[0];
        var queryStrings = splitUrl[1].split("&");
        $("input, select", $("#" + ParentFilterPnlId)).each(function () {

            for (var i = 0; i < queryStrings.length; i++) {
                if (queryStrings[i].indexOf($(this)[0].name + "=") >= 0) {
                    var pair = queryStrings[i].split("=");
                    if ($(this).is("input:checkbox")) {
                        SelectCheckBox($(this)[0].name, pair[1]);
                    }
                    else if ($(this).is("input:radio")) {
                        SelectRadioButtonList($(this)[0].name, pair[1]);
                    }
                    else if ($(this).is("select")) {
                        SelectDropDown($(this)[0].name, pair[1]);
                    }
                    else {
                        var decVal = decodeURIComponent(pair[1]);
                        if (decVal != "") {
                            $(this).val(decodeURIComponent(pair[1])); 
                        }
                    }
                }
            }
        });
    }
}


function clearText(field){
	if (field.defaultValue == field.value) {
	    field.value = '';
	    field.className = "txtChangeColor";
		//document.getElementById(field.id).className="txtChangeColor";
	} else if (field.value == '') {
	    field.value = field.defaultValue;
	    field.className = "txtRightColumn";
		//document.getElementById(field.id).className="txtRightColumn";
	}
}


function clearTextMemberSearch(field) {
    if (field.defaultValue == field.value) {
        field.value = '';
        document.getElementById(field.id).className = "txtLarge";
    } else if (field.value == '') {
        field.value = field.defaultValue;
        document.getElementById(field.id).className = "txtLarge";
    }
}


sfHover = function() {
	var sfEls = document.getElementById("mainNav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
	
	var sfEls = document.getElementById("topNav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
	
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

//use for texboxes to set default submit btn on enter. call on 'onkeypress' as return with arguments 'event' and btn id.
//submit can be input or even link, but for link muest specify return function  'searchFunction'
function CallSubmitOnEnter(e, targetSubmit, searchFunction) {
    e = e || window.event;
    var code = e.keyCode || e.which;
    if (code == 13) {
        if ($("#" + targetSubmit).is("a")) {
            searchFunction();
        }
        else if ($("#" + targetSubmit).is("input")) {
            if (searchFunction != null) {
                searchFunction();
            }
            $("#" + targetSubmit).focus();
            $("#" + targetSubmit).click();
        }
        return false;
    }
}



$(document).ready(function () {
    $("a[track]").click(function () {
        var url = $(this).attr("href");
        var category = $(this).attr("trackCategory");
        var source = $(this).attr("trackSource");
        $.get("/nswbc/handlersandservices/linktracker.ashx", {
            source: source,
            category: category,
            target: url,
            noRedirect: true 
        });
    });
});
