﻿/* Common scripts
--------------------------  */

//browser detection
var strUserAgent = navigator.userAgent.toLowerCase(); 
var isIE = strUserAgent.indexOf("msie") > -1; 
var isNS6 = strUserAgent.indexOf("netscape6") > -1; 
var isNS4 = !isIE && !isNS6  && parseFloat(navigator.appVersion) < 5; 

// Manage window.onload scripts
var onloadScripts = new Array();
var preImages = 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;

// Ends With
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);
  };
}

/*
    Form disabling
*/
// Diable form items
function disableform(o) {

    var inputs = o.getElementsByTagName("INPUT");
    for (var i = 0; i < inputs.length; i++) {
        inputs[i].readOnly = true;
        inputs[i].disabled = true;
    }
    var texts = o.getElementsByTagName("TEXTAREA");
    for (var j = 0; j < texts.length; j++) {
        texts[j].readOnly = true;
        texts[j].disabled = true;
    }
    var selects = o.getElementsByTagName("SELECT");
    for (var k = 0; k < selects.length; k++) {
        selects[k].readOnly = true;
        selects[k].disabled = true;
    }
}
// Enable form items
function enableform(o) {

    var inputs = o.getElementsByTagName("INPUT");
    for (var i = 0; i < inputs.length; i++) {
        inputs[i].readOnly = false;
        inputs[i].disabled = false;
    }
    var texts = o.getElementsByTagName("TEXTAREA");
    for (var j = 0; j < texts.length; j++) {
        texts[j].readOnly = false;
        texts[j].disabled = false;
    }
    var selects = o.getElementsByTagName("SELECT");
    for (var k = 0; k < selects.length; k++) {
        selects[k].readOnly = false;
        selects[k].disabled = false;
    }
}

// Form IDs
function getID(o) {
    var elid;

    if (o.id.lastIndexOf("_") > -1) {
        elid = o.id.substr(0, o.id.lastIndexOf("_")+1)
    }
    else {
        elid = o.id + "_";
    }
    return elid;
}



// Images
function swapimage(o, ev) {
    if (ev == "over") {
        o.src = o.src.replace(".gif", "-over.gif");
    }
    else {
        o.src = o.src.replace("-over.gif", ".gif");
    }
}

function preloadimages() {
    var imageObj = new Image();
    for(var i = 0;i < preImages.length;i++) {
        imageObj.src = preImages[i];
    }
}

onloadAdd(preloadimages);

/* Offers */

var hreset = false;
var dh;
var el;
function toggleoffer(o) {
    el = o;
    if (document.getElementById("bannerinner").className == "hidden") {
        if (!dh) {
            dh = 250;
            hreset = true;
        }
        document.getElementById("bannerinner").className = "";
        document.getElementById("bannerinner").style.height = "20px"
        window.setTimeout(offerin, 5);
    }
    else {
        //document.getElementById("bannerinner").className = "hidden";
        window.setTimeout(offerout, 5);
    }
}

function offerin() {
    if (parseInt(document.getElementById("bannerinner").style.height.replace("px", "")) < dh) {
        if (parseInt(document.getElementById("bannerinner").style.height.replace("px", "")) + 20 < dh) {
            document.getElementById("bannerinner").style.height = (parseInt(document.getElementById("bannerinner").style.height.replace("px", "")) + 20) + "px";
        }
        else {
            document.getElementById("bannerinner").style.height = dh + "px";
        }
        window.setTimeout(offerin, 5);
    }
    else {
        el.firstChild.setAttribute("src", "/_resources/files/buttons/hide-offer.gif");
        if (hreset) {
            hreset = false;
            document.getElementById("bannerinner").style.height = "";
            document.getElementById("bannerinner").style.height = document.getElementById("bannerinner").offsetHeight + "px";
            dh = document.getElementById("bannerinner").offsetHeight;
        }
        // Set the cookie value
        createCookie("sniffaheader", 1, 0);
    }
}

function offerout() {
    if(!dh) {
        dh = document.getElementById("bannerinner").offsetHeight;
        document.getElementById("bannerinner").style.height = dh + "px";
    }

    if (parseInt(document.getElementById("bannerinner").style.height.replace("px", "")) > 0) {
        if (parseInt(document.getElementById("bannerinner").style.height.replace("px", "")) - 20 > 0) {
            document.getElementById("bannerinner").style.height = (parseInt(document.getElementById("bannerinner").style.height.replace("px", "")) - 20) + "px";
        }
        else {
            document.getElementById("bannerinner").style.height = "0px";
        }
        window.setTimeout(offerout, 5);
    }
    else {
        document.getElementById("bannerinner").className = "hidden";
        el.firstChild.setAttribute("src", "/_resources/files/buttons/show-offer.gif");
        // Set the cookie value
        createCookie("sniffaheader", 0, 0);
    }
}


/* Help */

function openhelp(htype) {
    var owindow;
    switch (htype) {
        case "tips":
            owindow = "hints-tips.aspx";
            break;
        case "guide": 
            owindow = "user-guide.aspx";
            break;
        default:
            owindow = "user-guide.aspx";
    }
    window.open("/help/" + owindow, "SniffaHelp", "width=610,height=500,toolbar=0,scrollbars=1,resizable=1");
}

// Ends With
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);
  };
}
// Starts With
if (!String.prototype.startswith) {
  String.prototype.startswith = function(prefix) {
    return (this.indexOf(prefix) == 0);
  };
}

// Trim
if (!String.prototype.trim) {
    String.prototype.trim = function () {
        return this.replace(/^\s*/, "").replace(/\s*$/, "");
    };
}
if (!String.prototype.trimend) {
    String.prototype.trimend = function (suffix) {
        if (this.endswith(suffix)) {
            return this.substring(0, this.length - 1);
        }
        else {
            return this;
        }
    };
}

// Greyout
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';
    }
}

function greyOutList(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.3;                      
        cdark.style.MozOpacity = 0.3;                   
        cdark.style.filter = 'alpha(opacity=30)'; 
    } else {
        cdark.style.opacity = 1;                      
        cdark.style.MozOpacity = 1;                   
        cdark.style.filter = 'alpha(opacity=100)'; 
    }
}

/* Cookies */

// Cookie scripts
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else {
        expires = "";
    }
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}