var com_texunatech_services_popups = new Array();

function addPopup(idWindow){
    for(var i = 0; i < com_texunatech_services_popups.length; i++ ){
		var elt = com_texunatech_services_popups[i]
        if(elt == idWindow) return;
    }
    com_texunatech_services_popups.push(idWindow);
}

function removePopup(idWindow){
    var newArr = new Array() ;
	for(var i = 0; i < com_texunatech_services_popups.length; i++ ){
		var elt = com_texunatech_services_popups[i]
        if(elt != idWindow) newArr.push(elt);
    }
    com_texunatech_services_popups = newArr;
}

function showModalWindowByName(windowName, idWindow){
    var windowExists = document.getElementById(windowName);
    if(windowExists){
        return showModalWindow (idWindow);
    }
    return true;
}

function showModalWindow (idWindow) {
    var modalWindow = document.getElementById(idWindow);
	if (modalWindow) {
        addPopup(idWindow)
        var cName = modalWindow.className;
        cName = cName.replace(/(hide)/,'');
        modalWindow.className = cName;
        shadowWin();
        showSelects(modalWindow);
        return false;
    }
    return true;
}

function showSelects(modalWindow){
    var selectModalDiv = modalWindow.getElementsByTagName('select');
    for (var i=0; i<selectModalDiv.length;i++) {
        selectModalDiv[i].className = '';
    }
}

function closeModalWindow (idWindow) {
	var modalWindow = document.getElementById(idWindow);
	if (modalWindow) {
        modalWindow.className += ' hide';
        removePopup(idWindow)
        removeErrorMessages(modalWindow)
        if(com_texunatech_services_popups.length > 0){
           var popupId = com_texunatech_services_popups.pop();
           var popup = document.getElementById(popupId);
           if(popup){
               showSelects(popup)
               return;
           }
        }
        unShadowWin();
    }
}

function shadowWin(){
    var shadowWin = document.getElementById('blockDocument');
    if (shadowWin) {
        shadowWin.className = '';
    }
    var allSelect = document.getElementsByTagName('select');
    for (var i=0; i<allSelect.length;i++) {
        allSelect[i].className = 'hide';
    }
}

function unShadowWin(){
    var shadowWin = document.getElementById('blockDocument');
    if (shadowWin) {
        shadowWin.className = 'hide';
    }
    var allSelect = document.getElementsByTagName('select');
    for (var i=0; i<allSelect.length;i++) {
        allSelect[i].className = '';
    }
    com_texunatech_services_popups = new Array();
}

function removeErrorMessages(elt){
    var spans = elt.getElementsByTagName("span");
    for(var i = 0; i < spans.length; i ++){
        var span = spans[i];
        if(span.className == 'error'){
            span.parentNode.removeChild(span);
        }
    }

}

function externalLinks() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors[i];
if (anchor.getAttribute("href") &&
anchor.getAttribute("rel") == "external")
anchor.target = "_blank";
}
}
window.onload = externalLinks; 