// General Functions
var d = document;
function writeToDiv(divID,text){
    d.getElementById(divID).innerHTML = text;
}

function setDisplay(id,display){
    if ((getBrowserType() == "msie") && (display == "table")) display = "block"
    var object = d.getElementById(id).style;
    object.display = display;
}

function changeClass(id, newClass) {
    identity=d.getElementById(id);
    identity.className=newClass;
}

function toggle(obj) {
    var el = d.getElementById(obj);
    if ( el.style.display != 'none' ) {
        el.style.display = 'none';
    }
    else {
        el.style.display = 'block';
    }
}

function enforceNumbersOnly(evt,input){
    // Get the keycode from IE or Mozilla
    var keyCode = evt == null ? event.keyCode : evt.which;
    //If Enter/Return is entered submit poll
    if ((keyCode == 13) && (input == "id")) previewPoll()
    // Any number or a backspace/delete/tab
    return (keyCode > 47 && keyCode < 58) || keyCode == 8 || keyCode == 17 || keyCode == 86 || // Keycode "17" is ctrl && keycode 86 is "V"
        (keyCode > 36 && keyCode < 41) || keyCode == 46 ||
        (keyCode > 95 && keyCode < 106 || keyCode == 9);
}

function rollOn(on,off){
    setDisplay(on,"block")
    setDisplay(off,"none")
}

function rollOff(off,on){
    setDisplay(off,"block")
    setDisplay(on,"none")
}

var trim = function(strText) {
   // this will get rid of leading spaces
    while (strText.substring(0,1) == ' ')
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
}

function getSelected(group){
    for (i=0;i<group.length;i++){
        if(group[i].checked){
            return i;
        }
    }
}

function checkWord(str,pattern){
    var reg = new RegExp(pattern,"i");
    if (reg.test(str))
        return true;
    else
        return false;
}

function getBrowserType(){
    var agt = navigator.userAgent.toLowerCase();
    var isIE = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    var isFF = (agt.indexOf("firefox") != -1);
    if(isIE) return "msie";
    else if(isFF) return "FF";
    else return "";
}

// Open Window
var URL,width,height,scroll,menubar,toolbar,loc,resize,xPos,yPos,newWin,winName,center,alignRight;
function openWin(URL,width,height,scroll,menubar,loc,toolbar,resize,xPos,yPos,winName,center,alignRight,fullscreen){
    if (width == "") width = screen.availWidth;
    if (height == "") height = screen.availHeight;
    if (loc == "") loc = "no";
    if (scroll == "") scroll = "auto";
    if (menubar =="") menubar = "no";
    if (toolbar == "") toolbar = "no";
    if (resize == "") resize = "yes";
    if (xPos == "") xPos = "5";
    if (yPos == "") yPos = "5";
    if (winName == "") winName = "win";
    if (center == 'yes'){
        var centerWidth = screen.availWidth/2;
        var centerHeight = screen.availHeight/2;
        tempW = width/2;
        tempH = height/2;
        xPos = centerWidth - tempW;
        yPos = centerHeight - tempH;
    }
    if (alignRight == "yes") xPos = screen.availWidth - width - 7;
    if (fullscreen == "yes"){
        xPos = 0;
        yPos = 0;
        if (navigator.appVersion.indexOf("mac") == 1){
            width = screen.availWidth;
            height = screen.availHeight;
        } else {
            width = screen.availWidth - 7;
            height = screen.availHeight - 20;
        }
    }
    var features ="width=" +width+ ",height=" +height+ ",loc=" +loc+ ",scrollbars=" +scroll+ ",menubar=" +menubar+ ",toolbar=" +toolbar+ ",resizable=" +resize+ ",left=" +xPos+ ",top=" +yPos;
     newWin = window.open(URL,winName,features);
    if (window.focus) newWin.focus();
}

function changeOpac(opacity, id) {
    var object = d.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}



// XML Request
function getData(url, callback){
    var XMLHttpRequestObject = false;
    url += "&t=" + new Date().getTime();
    //Create XMLHttpRequestObject
    if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            XMLHttpRequestObject = new
            ActiveXObject("Microsoft.XMLHTTP");
        }
    //Create Inner Function to Handle Data and Pass to Callback Function
    if(XMLHttpRequestObject) {
        XMLHttpRequestObject.open("GET", url);
        XMLHttpRequestObject.onreadystatechange = function(){
            if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
                callback(XMLHttpRequestObject.responseXML);
                delete XMLHttpRequestObject;
                XMLHttpRequestObject = null;
            }
        }
    //Send Request to Server. This will call onreadystatechange function
    XMLHttpRequestObject.send(null);
    }
}

function postData(url, data, callback){
    var XMLHttpRequestObject = false;

    if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new
        ActiveXObject("Microsoft.XMLHTTP");
    }

    if(XMLHttpRequestObject) {
        XMLHttpRequestObject.open("POST", url);
        XMLHttpRequestObject.setRequestHeader('Content-Type', 'text/xml');

        XMLHttpRequestObject.onreadystatechange = function(){
            if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
                callback(XMLHttpRequestObject.responseXML);
                delete XMLHttpRequestObject;
                XMLHttpRequestObject = null;
            }
        }
    XMLHttpRequestObject.send(data);
    }
}

function addCommas(nStr) {
        nStr += '';
        x = nStr.split('.');
        x1 = x[0];
        x2 = x.length > 1 ? '.' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }
        return x1 + x2;
 }


function getCookie(cId)  {
        cookie = document.cookie;
        tag = cId;       // Cookie identifier
        tlen = tag.length;    // Length of cookie identifier
        index = 0;            // Begin loop at 0
        foundCookie = false;  // Processing Flag
        while (index < cookie.length)  {  // Loop through cookie to find tag
               if (cookie.substring(index, index+tlen) == tag)  {
                      foundCookie = true;
                      index = index + tlen;
                      cookie = cookie.substring(index,cookie.length);
                      return true;
                      break;
               }
               index = index+1;
        }
        if (foundCookie == false) return false;
}

function setCookie(cId){
    var cookieName = 'vizu-' + cId;  // Set cookie identifier
    var expDate = new Date();  // Date variable for expiration date
    var cookieValue = 'vizu-'+((expDate.getTime()))+'-'+(parseInt(Math.random() * 100000000)); // Set cookie value - numeric datetime value - random generated number
    // Set cookie expiration date for 30 years
    expDate.setTime (expDate.getTime() + 946080000000);
    // Set the actual cookie
    document.cookie = cookieName + '=' + escape (cookieValue) + '; expires=' + expDate.toGMTString() + '; path=/'
}

// Set Referral Cookie
function setRefCookie(cId,cValue,cDays){
    var cookieName = cId;  // Set cookie identifier
    var expDate = new Date();  // Date variable for expiration date
    var cookieValue = cValue;
    // Set cookie expiration date for 30 years if days is empty
    if (cDays == "") cDays = 10950;
    cDays = (86400000 * cDays);
    expDate.setTime (expDate.getTime() + cDays);
    // Set the actual cookie
    document.cookie = cookieName + '=' + escape (cookieValue) + '; expires=' + expDate.toGMTString() + '; path=/'
}

// Remove HTTPS from sign in box form if not Production //
    function changeSignInAction(){
        var xSign=document.getElementById("quickSignIn");
        var prodURL = "answers.vizu.com";
        var globalURL = document.domain;
        //alert("Original action: " + xSign.action)
        if (prodURL != globalURL) {
            xSign.action = "/sign-in.html";
        }
    }

// Returns the url parameter with name that is passed in
function getUrlParam(name){
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var tmpURL = window.location.href;
  var results = regex.exec( tmpURL );
  if(results == null)
    return "";
  else
    return results[1];
}

// Add Event to Window... typically onload
// example: addEvent(window,"load",init);
function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj["e"+type+fn] = fn;
    obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
    obj.attachEvent( "on"+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}

// Detect Flash
var hasFlash = false;
function detectFlash(requiredVersion){
    // Flash Detetection
    // system globals
    var flash5Installed = flash6Installed = flash7Installed = flash8Installed = flash9Installed = flash10Installed = flash11Installed = hasRightVersion = false;
    var maxVersion = 11;
    var actualVersion = 0;
    //vbscript detection for Win IE
    var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
    var isWin = (navigator.appVersion.indexOf("Windows") != -1) ? true : false;
    if(isIE && isWin){
        fs = 'Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.';
        d.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n');
        d.write('on error resume next \n');
        d.write('flash5'+fs+'5"))) \n');
        d.write('flash6'+fs+'6"))) \n');
        d.write('flash7'+fs+'7"))) \n');
        d.write('flash8'+fs+'8"))) \n');
        d.write('flash9'+fs+'9"))) \n');
        d.write('flash10'+fs+'10"))) \n');
        d.write('flash11'+fs+'11"))) \n');
        d.write('<\/SCR' + 'IPT\> \n');
    }
    if (navigator.plugins){
        if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]){
            var isVersion2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
            var flashDescription = navigator.plugins["Shockwave Flash" + isVersion2].description;
            // a flash plugin-description looks like this: Shockwave Flash 4.0 r5
            var flashVersion = parseInt(flashDescription.substr(flashDescription.indexOf(".") - 2,flashDescription.indexOf(".") - 1));
            flash5Installed = flashVersion == 5;
            flash6Installed = flashVersion == 6;
            flash7Installed = flashVersion == 7;
            flash8Installed = flashVersion == 8;
            flash9Installed = flashVersion == 9;
            flash10Installed = flashVersion == 10;
            flash11Installed = flashVersion == 11;
        }
    }
    for (var i = 5; i <= maxVersion; i++) {
        if (eval("flash" + i + "Installed") == true) actualVersion = i;
    }
    if (actualVersion >= requiredVersion) {
        hasFlash = true;
    }
}