﻿//browser detection
var strUserAgent = navigator.userAgent.toLowerCase();
var isIE = strUserAgent.indexOf("msie") > -1;

// Manage window.onload scripts
var onloadScripts = new Array();

function onloadProcess() {
    for (var i = 0; i < onloadScripts.length; i++) {
        eval(onloadScripts[i]);
    }
}

function onloadAdd(func) {
    onloadScripts[onloadScripts.length] = func;
}

// Add onload
window.onload = onloadProcess;

// Grey out
function greyOut(vis, divid) {

    if (!divid) {
        divid = "greyout";
    }

    var cdark = document.getElementById(divid);

    if (vis) {
        //set the shader to cover the entire page and make it visible.
        cdark.style.opacity = 0.7;
        cdark.style.MozOpacity = 0.7;
        cdark.style.filter = 'alpha(opacity=' + 70 + ')';
        cdark.className = 'greyout';
    } else {
        cdark.className = 'hidden';
    }
}

/*
    AJAX Method Failure
*/
function ajaxfailure(ex, ctx, methodName) {
    alert(ex.get_message());
    // Hide any passed loader animations
    if (ctx) {
        if (ctx.greyout == "true") {
            greyOut(false);
        }
        if (ctx.greyoutcontent == "true") {
            greyOut(false, 'greyout');
        }
    }
}

/*
    Array
*/
Array.prototype.contains = function (element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == element) {
            return true;
        }
    }
    return false;
}

Array.prototype.arraycontains = function (element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i][0] == element) {
            return true;
        }
    }
    return false;
}

Array.prototype.get = function (element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i][0] == element) {
            return this[i];
        }
    }
    return null;
}

Array.prototype.getnextitem = function (element) {
    var retel = 9999999999;
    for (var i = 0; i < this.length; i++) {
        if (this[i][0] > element) {
            retel = Math.min(this[i][0], retel);
        }
    }
    if (retel != 9999999999) {
        return this.get(retel);
    }
    else {
        return null;
    }
}

/*
    String
*/
if (!String.prototype.endswith) {
    String.prototype.endswith = function (suffix) {
        var startPos = this.length - suffix.length;
        if (startPos < 0) {
            return false;
        }
        return (this.lastIndexOf(suffix, startPos) == startPos);
    };
}

if (!String.prototype.startswith) {
    String.prototype.startswith = function (prefix) {
        return (this.indexOf(prefix) == 0);
    };
}

/*
    Search
*/

function searchtext(o, f) {
    if (f == 1) {
        if (o.value == "Search...") {
            o.value = "";
        }
    }
    else {
        if (o.value == "") {
            o.value = "Search...";
        }
    }
}

// Positioning

function getPosition(o) {
	var curleft = curtop = 0;
	if (o.offsetParent) {
	    do {
	        curleft += o.offsetLeft;
	        curtop += o.offsetTop;
	    } while (o = o.offsetParent);
	}
    return { x: curleft, y: curtop }
}

// Newsletter Unsubscribe
function ValidateEmail(fvalue) {
    var objRegExp = /^.+@[^\.].*\.[a-z]{2,}$/;
    //check for format
    return objRegExp.test(fvalue);
}

function unsubscribe() {
    if (document.getElementById("txtunsubscribeemail").value == "") {
        return false;
    }
    if (!ValidateEmail(document.getElementById("txtunsubscribeemail").value)) {
        alert("Invalid e-mail address");
        return false;
    }
    PageMethods.Newslstter_Unsubscribe(document.getElementById("txtunsubscribeemail").value, unsubscribesuccess, ajaxfailure);
}

function unsubscribesuccess(value, ctx, methodName) {
    document.getElementById("unsubscribeform").className = "hidden";
    document.getElementById("unsubscribeconfirm").className = "";
}


