﻿/*
	Show and close the modal window.
*/

/*
	G L O B A L   V A R I A B L E S
*/

var _modalWindowOpenedForUrl = "";

/*
Called from links to open the modal window and go to https.
*/
function RequestShowModal(title, url) {
    if (location.protocol != "http:") {
        ShowModalWindow(title, url);
    }
    else {
        var request = "/modal/show?title=" + escape(title);
        request += "&url=" + escape(url);
        var current = location.href;
        request += "&referrer=" + escape(current);

        location = request;
		
    }
}

/*
This function shows the modal background and iframe forground HTML elements
in order to simulate a modal window.  The url parameter is used to populate
the iframe with content and to launch the wizard that the iframe is to contain.
*/
function ShowModalWindow(title, url) {
//alert("wearehere");
	var elem;
	elem = document.getElementById('modalBkgrnd');
	elem.style['display'] = 'block';
	
	//append close function to modal background
	elem.onclick = function () { ConfirmToQuit(); };

	document.getElementsByTagName('html')[0].style['overflow'] = 'hidden';

	_modalWindowOpenedForUrl = url;
	
	elem = document.getElementById('modalIFrame');
	elem.src = url;

	elem = document.getElementById('modalTitle');
	elem.innerHTML = title;

	elem = document.getElementById('modalContent');
	elem.style['display'] = 'block';

	SetTitleColor(_modalWindowOpenedForUrl);
}

function GetModalTitle(url) {
    
    $.ajax({
        async: false,
        url: "/Purchase/GetTitle",
        cache: false,
        data: "servcode=" + url.split("servcode")[1].replace("=", ''),
        success: function (data) {
            return data;
        }
    });
}

function ReturnFromLoginToOriginalIntention() {
	if (_modalWindowOpenedForUrl.search(/modallogin/i) == -1) {
		elem = document.getElementById('modalIFrame');
		elem.src = _modalWindowOpenedForUrl;		
	}
	else {
		CloseModalWindow();
	}
}

/*
	Close the currently opened modal window.
*/
function CloseModalWindow(url) {

    if (parent.document.getElementById('modalIFrame') != null)
        parent.document.getElementById('modalIFrame').src = '';
    else {
        if (arguments.length == 1) {
            parent.location = url;
        }
        return;
    }
    if (arguments.length == 1) {
        parent.location = url;
    }
    else {
        parent.document.getElementById('modalContent').style['display'] = 'none';
        parent.document.getElementById('modalBkgrnd').style['display'] = 'none';
        //this is for not reloading the page but to make the page load with a get.
        parent.location = parent.location ;
    }
}

/*
	Close the modal user message within the opened modal window.
*/
function CloseModalUserMessage() {
	document.getElementById('userMsgPopup').style['display'] = 'none';
	document.getElementById('userMsgPopupBkgrnd').style['display'] = 'none';
}

/*
	Show and close the comparison chart.
*/

function ShowStaticContentModal(foregroundElementID) {

	var elem;
	elem = document.getElementById('modalBkgrnd');
	elem.style['display'] = 'block';
	
	//append close function to modal background
	elem.onclick = function () { CloseStaticContentModal(foregroundElementID); };

	elem = document.getElementById(foregroundElementID);
	elem.style['display'] = 'block';
	
}

function CloseStaticContentModal(foregroundElementID) {
	document.getElementById(foregroundElementID).style['display'] = 'none';
	document.getElementById('modalBkgrnd').style['display'] = 'none';
}

/*Sets the color of the Product Title*/
function SetTitleColor(location) {    
    var color = "#21578a";
    if (location.toLowerCase().indexOf("ad1") != -1) {
        color = "#e05206";
    }
    if (location.toLowerCase().indexOf("ado") != -1) {
        color = "#009648";
    }
    if (location.toLowerCase().indexOf("pmr") != -1) {
        color = "#00549f";
    }
    if (location.toLowerCase().indexOf("pm3") != -1) {
        color = "#00549f";
    }
    if (location.toLowerCase().indexOf("aqe") != -1) {
        color = "#d0103a";
    }
    if (location.toLowerCase().indexOf("aqf") != -1) {
        color = "#d0103a";
    }
    if (location.toLowerCase().indexOf("thp") != -1) {
        color = "#857363";
    }
    if (location.toLowerCase().indexOf("thq") != -1) {
        color = "#857363";
    }
    if (location.toLowerCase().indexOf("thr") != -1) {
        color = "#857363";
    }
    if (location.toLowerCase().indexOf("tht") != -1) {
        color = "#857363";
    }
    if (location.toLowerCase().indexOf("thu") != -1) {
        color = "#857363";
    }
    if (location.toLowerCase().indexOf("rrc") != -1) {
        color = "#a47cc9";
    }
    if (location.toLowerCase().indexOf("pra") != -1) {
        color = "#005b82";
    }
    if (location.toLowerCase().indexOf("prb") != -1) {
        color = "#005b82";
    }
    if (location.toLowerCase().indexOf("prc") != -1) {
        color = "#005b82";
    }
    if (location.toLowerCase().indexOf("pre") != -1) {
        color = "#005b82";
    }
    if (location.toLowerCase().indexOf("pri") != -1) {
        color = "#005b82";
    }
    if (location.toLowerCase().indexOf("prp") != -1) {
        color = "#005b82";
    }
    if (location.toLowerCase().indexOf("prs") != -1) {
        color = "#005b82";
    }
    if (location.toLowerCase().indexOf("pru") != -1) {
        color = "#005b82";
    }       
    document.getElementById("modalTitle").style.color = color;
}

function CloseModalYesNoMessage() {
    document.getElementById('userYesNoMsgPopup').style['display'] = 'none';
    document.getElementById('userYesNoMsgPopupBkgrnd').style['display'] = 'none';
}

function ConfirmToQuit() {
    var f;
    for (var i = 0; i < frames.length; i++) {
        try
        {
            if (frames[i].frameElement.id == "modalIFrame") {
                f = frames[i];
                break;
            }
        }
        catch(err)
        {}
    }
    if (f.location.href.toLowerCase().indexOf("/purchase/") > -1 || 
        f.location.href.toLowerCase().indexOf("/account/edit") > -1 ||
        f.location.href.toLowerCase().indexOf("/reportaudit/") > -1 ) {
        try {
            f.document.getElementById('userYesNoMsgPopup').style['display'] = 'block';
            f.document.getElementById('userYesNoMsgPopupBkgrnd').style['display'] = 'block';
            //f.document.getElementById('prname').innerHTML = parent.document.getElementById("modalTitle").firstChild.nodeValue;
            f.document.getElementById('nocancel').focus();
        } catch (e) {
            CloseModalWindow();
        }        
    }
    else {
        CloseModalWindow();
    }
}

