// JavaScript Document
function browserIsIE() {
    return navigator.appName == 'Microsoft Internet Explorer';
}

function languageShowDropdown() {
    var languageSelector = jQuery('.language-selector .select-language');
    var dropdownTab = jQuery('.language-dropdown .dropdown-tab');
    var dropdownContent = jQuery('.language-dropdown .dropdown-content');

    var tabLeft = languageSelector.offset().left - 18;
    var tabTop = languageSelector.offset().top - 12;

    var contentLeft = jQuery('#logo').offset().left - 8;
    var contentTop = tabTop + 29;

    dropdownTab.css('left', tabLeft);
    dropdownTab.css('top', tabTop);
    dropdownTab.show();

    dropdownContent.css('left', contentLeft);
    dropdownContent.css('top', contentTop);
    dropdownContent.show();
}

function languageHideDropdown() {
    jQuery('.language-dropdown .dropdown-tab').hide();
    jQuery('.language-dropdown .dropdown-content').hide();
}

function primnavShowDropdown(linkElement) {
    linkElement.parent().siblings().find('.primnav-open-tab').hide();
    linkElement.parent().siblings().find('.primnav-open-tab').removeClass('primnav-open-tab');
    linkElement.parent().siblings().find('.primnav-open-content').hide();
    linkElement.parent().siblings().find('.primnav-open-content').removeClass('primnav-open-content');

    var tabLeft = linkElement.offset().left - 18;
    var tabTop = linkElement.offset().top - 12;

    var contentLeft = tabLeft;
    var contentTop = tabTop + 29;

    if (linkElement.parent().hasClass('opens-left')) {

        var tabWidth = linkElement.next().width() + 18;

        if (linkElement.parent().hasClass('dropdown-onecol')) {
            contentLeft -= 180; // col and spacer width
        }
        else if (linkElement.parent().hasClass('dropdown-twocol')) {
            contentLeft -= 404; // col and spacer width
        }
        else if (linkElement.parent().hasClass('dropdown-threecol')) {
            contentLeft -= 628; // col and spacer width
        }
        else if (linkElement.parent().hasClass('dropdown-fourcol')) {
            contentLeft -= 852; // col and spacer width
        }

        contentLeft -= 36; // left and right space

        contentLeft += tabWidth;
    }

    // position the tab
    linkElement.next().css('left', tabLeft);
    linkElement.next().css('top', tabTop);

    linkElement.next().addClass('primnav-open-tab');

    linkElement.next().show();

    // position the content
    linkElement.next().next().css('left', contentLeft);
    linkElement.next().next().css('top', contentTop);

    linkElement.next().next().addClass('primnav-open-content');

    linkElement.next().next().show();
}

function primnavHideDropdown() {
    jQuery('.primnav-open-tab').hide();
    jQuery('.primnav-open-tab').removeClass('primnav-open-tab');

    jQuery('.primnav-open-content').hide();
    jQuery('.primnav-open-content').removeClass('primnav-open-content');
}

function moreAreaToggle(linkElement) {
    linkElement.parent().parent().parent().find('.toggle').toggle();
    linkElement.parent().parent().find('.see-more').toggle();
    linkElement.parent().parent().find('.collapse').toggle();
}

function portletToggle(linkElement) {
    linkElement.parent().parent().parent().find('.toggle').toggle();
    if (linkElement.hasClass('lnk-toggle-open')) {
        linkElement.removeClass('lnk-toggle-open');
        linkElement.addClass('lnk-toggle-close');
    }
    else {
        linkElement.removeClass('lnk-toggle-close');
        linkElement.addClass('lnk-toggle-open');
    }
}

function setPreferredDevice(deviceName) {

    jQuery.cookie('preferredDevice', deviceName, { path: '/', domain: '.siemens-enterprise.com' });

    location.href = location.href;
    // location.reload()
}

var rsSiteVirtualFolder = "";

jQuery(document).ready(function () {
    

rsSiteVirtualFolder = document.getElementById("rsSiteVirtualFolder").value;

if(rsSiteVirtualFolder=='/de/'){
jQuery('div.language-selector').css('padding-left','150px');

}

    jQuery('a').focus(function () {
        jQuery(this).blur();
    });



	//alert(jQuery('a').attr('href'));
	
   jQuery('.tab-wrapper .tabs a').click(function () {
        // remove active class from all tabs
        jQuery(this).siblings().removeClass('active');
        // set clicked tab active
        jQuery(this).addClass('active');
        // set all tabContent elements to display:none
        jQuery(this).parent().next().children().each(function () {
            jQuery(this).css('display', 'none');
        });
        // set the corresponding (by index) tabContent element to display:block
        jQuery(this).parent().next().children().eq(jQuery(this).index()).css('display', 'block');
    });

    // set active primnav entry
    if (jQuery('ul.prim-nav').length) {
        jQuery('ul.prim-nav li a.rssectionpage').each(function (index) {
            if (jQuery(this).text() == jQuery('#rsCurrentSectionPage').val())
                jQuery(this).parent('li').addClass("active");
        });
    }

    // toggle for forum portlets
    jQuery('li.forum div.header a').click(function () {
        jQuery(this).toggleClass('lnk-toggle-open');
        jQuery(this).toggleClass('lnk-toggle-close');
        jQuery(this).parent().next().toggle();
    });

});

function mobileGalleryPrev(linkElement) {
    var targetItem = linkElement.parent().parent().prev();
    if (targetItem.length == 0) {
        targetItem = linkElement.parent().parent().parent().find('.item').last();
    }
    targetItem.siblings().hide();
    targetItem.show();
}
function mobileGalleryNext(linkElement) {
    var targetItem = linkElement.parent().parent().next();
    if (targetItem.length == 0) {
        targetItem = linkElement.parent().parent().parent().find('.item').first();
    }
    targetItem.siblings().hide();
    targetItem.show();
}


// code for searchbox
function submitSearch() {
    document.getElementById("cx").value = document.getElementById("mycx").value;
    var siteSearchParam = document.getElementById("mysitesearchparam").value;
    if (document.getElementById("searchValue").value != "") {
        //alert("This is a Test-search")
        var virtualFolder = rsSiteVirtualFolder;
        document.getElementById("q").value = siteSearchParam + document.getElementById("searchValue").value;
        if (virtualFolder != "/") document.searchForm.action = rsSiteVirtualFolder + "search.aspx";
        document.searchForm.submit();
    }
}
//fake submit on enter
function whichButtonGoo(event) {
    if (event == null) event = window.event;
    if (event.keyCode == 13) {
        document.getElementById("searchSubmit").click();
    }
}
function addEventGoo(elm, evType, fn, useCapture) {
    if (elm == null) return false;
    if (elm.addEventListener) {
        elm.addEventListener(evType, fn, useCapture);
        return true;
    }
    else {
        var r = elm.attachEvent('on' + evType, fn);
        return r;
    }
}

function addListenersGoo(e) {
    var buttonGoo = document.getElementById("searchValue");
    addEventGoo(buttonGoo, 'keyup', whichButtonGoo, false);
}

window.onload = addListenersGoo;
//addEventGoo(window, 'load', addListenersGoo, false);

// END code searchbox


// START Download-Protection
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;
}

function interpretRequest() {
    switch (request.readyState) {
        // readyState = 4 && request.status = 200 => ok      
        case 4:
            if (request.status != 200) {
                //alert("error: "+request.status);
            } else {
                alert('request OK');
            }
            break;
        default:
            break;
    }
}

function showDLPForm(name, width, height, optionalText, myself, filename, extension, myfile, sitename, campaign, currlanguage) {

    var cookieData = readCookie("SEN.Internet.LinkTracker1");
    var myDLpage = "";
    if (currlanguage == "zh-CN") {
        myDLpage = "/dlprotect/Internet2010/Internet2010.LinkProtection_cn.aspx";
    }
    if (currlanguage == "fr-BE") {
        myDLpage = "/dlprotect/Internet2010/Internet2010.LinkProtection_be.aspx";
    }
    if (currlanguage == "pt-BR") {
        myDLpage = "/dlprotect/Internet2010/Internet2010.LinkProtection_br.aspx";
    }
    if (currlanguage == "es-ES") {
        myDLpage = "/dlprotect/Internet2010/Internet2010.LinkProtection_es.aspx";
    } else {
        myDLpage = "/dlprotect/Internet2010/Internet2010.LinkProtection_en.aspx";
    }

    if (cookieData != null) {
        // generate request
        if (window.XMLHttpRequest) {
            request = new XMLHttpRequest(); // Mozilla, Safari, Opera
        } else if (window.ActiveXObject) {
            try {
                request = new ActiveXObject('Msxml2.XMLHTTP'); // IE 5
            } catch (e) {
                try {
                    request = new ActiveXObject('Microsoft.XMLHTTP'); // IE 6
                } catch (e) { }
            }
        }
        // check request exists
        if (!request) {
            return false;
        } else {
            var url = "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" + "&Campaign_ID=" + campaign + "&" + cookieData + "&" + "00N200000012nKp=" + filename + "." + extension + "&" + "00N200000012kqs=" + sitename + "&" + "oid=00D200000001kVs" + "&" + "retURL=http://enterprise.siemens.com/open/chk.aspx";
            top.document.getElementById("salesforceSubmit").src = url;
        }
    } else {
        myself.href = "#";
        myself.target = "_self";
        jQuery.prettyPhoto.open(myDLpage + '?iframe=true&width=500&height=375', '', '', '');
        return false;
    }
}
// END Download-Protection



//START PST value for Salesforce forms
function PSTCookieInit() {
    l = window.location.search;
    if (l != "") {
        if (l.indexOf("sc_pst=") != -1) {
            var PSTValue = getUrlVars()["sc_pst"];
            jQuery.cookie('PSTCookie', PSTValue, { path: '/' });  // set cookie
        }
    }
    //alert("PSTCookie: " + jQuery.cookie('PSTCookie'))
}
function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
function PSTField2SaleforceForm() {
    if (jQuery.cookie('PSTCookie') != null) {
        //alert("PSTCookie: " + jQuery.cookie('PSTCookie'))    
        jQuery("input[name*='oid']").each(function () {
            $(this).parent().prepend('<input id="00N200000012lNm" name="00N200000012lNm" type="hidden" value="' + jQuery.cookie('PSTCookie') + '">');
        });
    }
}
//END 

//START IDG Mailing
function IDGMailingInit() {
    var checkloc = new Array();
    checkloc[0] = "/us/products/competitive-upgrade.aspx";
    checkloc[1] = "/us/products/unified-communications-collaboration/openscape-web-collaboration.aspx";
    checkloc[2] = "/us/products/unified-communications-collaboration/openscape-web-collaboration/webtrial.aspx";

    checkloc[3] = "/de/products/unified-communications-collaboration/openscape-web-collab.aspx";
    checkloc[4] = "/de/products/unified-communications-collaboration/openscape-web-collab/webtrial.aspx";
    checkloc[5] = "/de/products/unified-communications-collaboration/openscape-web-collab/competitive-upgrade.aspx";

    var myloc = document.location.pathname;
    var IDGCheck = false;

    for (i = 0; i <= checkloc.length; i++) {
        if (myloc == checkloc[i]) {
            if (window.location.search.indexOf("sc_idgmail=true") != -1) {
                jQuery.cookie('IDGMailing', 'true', { path: '/' });  // set cookie
                break;
            }
        }
    }
    //alert("IDGMailing: " + jQuery.cookie('IDGMailing'))
}
//END

//accordion toggle function
function toggleContent(obj) {
    jQuery(obj).parent('div').siblings().find('.accordionLable').removeClass('accordionActive');
    jQuery(obj).toggleClass('accordionActive');
    jQuery(obj).parent('div').siblings().find('.accordionContent').slideUp();
    jQuery(obj).next('div').slideToggle();
}

jQuery(document).ready(function () {
    IDGMailingInit();
    PSTCookieInit();
    PSTField2SaleforceForm();
});

//mailer AJAX function

function myMailer(a, r, s, rep) {
    var rsAjaxController = '/layouts/Internet2010/Internet2010_AJAX_Controller.aspx';
    var action = a;
    var recipient = r;
    var subject = s;
    var name = jQuery("input#name_txt").val();
    var email = jQuery("input#email_txt").val();
    var question = jQuery("textarea#question_txt").val();
    var hasError = false;
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    var reply = "<h3>" + rep + "</h3>";

    var dataString = "action=" + action + "&recipient=" + recipient + "&subject=" + subject + "&reply=" + reply + "&question=" + question + "&name=" + name + "&email=" + email;
    //alert(dataString);
    if (name == '') {
        hasError = true;
        jQuery("input#name_txt").addClass('mymailerror');
    }
    if (question == '') {
        hasError = true;
        jQuery("textarea#question_txt").addClass('mymailerror');
    }
    if (email == '') {
        hasError = true;
        jQuery("input#email_txt").addClass('mymailerror');
    }
    if (!emailReg.test(email)) {
        jQuery("input#email_txt").addClass('mymailerror');
        if (jQuery('#mailerror').length == 0) {
            jQuery("#mymailerrors").after('<span class="mailerror" id="mailerror">Invalid mail address!</span>');
        }
        hasError = true;
    }

    if (hasError == true) {
        jQuery('#mymailerrors').show();
    }

    else {
        jQuery.ajax({
            type: "POST",
            url: rsAjaxController,
            data: dataString,
            success: function () {
                jQuery('#mymailform')[0].innerHTML = reply;
            }
        });
        jQuery('#mymailerrors').hide();
        jQuery('#mailerror').hide();
    }
    //alert(hasError);
    return false;
}


