//global da lista
var probList;

function ChangeErrorStyle()
{
    $("span.errorGeneral").each(function() {
        if ($(this).css("display") == "inline")
            $(this).css("display", "block");
    });
}

$(document).ready(ChangeErrorStyle);

$(function() {	
    ChangeErrorStyle();
    $("input[@type=image]").click(ChangeErrorStyle);
    $("input[@type=text]").change(ChangeErrorStyle);
    
    $.meta.setType("elem", "script");
    probList = $("div SPAN.data").data().theList;
    $.meta.setType("");
        
    //coloca a combo:
    select_prob = $(".combobox_container .combobox_header INPUT[@name$=comboTipoProblema]",$(".combobox_list").parents("div")).parents(".combobox_container");
    
    lista = $(".combobox_list", select_prob);
    input_text = $("div.combobox_header INPUT[@type=text]",select_prob);
    input_value = $("div.combobox_header INPUT[@type=hidden]",select_prob);
    
    input_value.val('');
    input_text.val(''); 
    lista.empty();
    
    
    for(theID in probList) 
   {
        if(isValidNumber(theID)) 
            lista.append('<a href="#" data="'+probList[theID].probID+'">'+probList[theID].probTitle+'</a><br/>');
    }
   
    input_text.val('Tipo de Pedido');
    input_value.val(0);
    select_prob.get(0).rebuild();
        
    try {
        var idProblema = $("INPUT[@type^=hidden][@id$=idProblema]",this).val();
        if(GetQueryString('id') == null && (idProblema==null || idProblema==""))
            input_text.val('Tipo de Pedido');
        else
            input_text.val(probList[0].probTitle);
            
        doTheChange(0);
    } 
    catch(e) 
    {
    }
    
    $("#ctl00_MainContentPlaceHolder_botSubmit").css("width", "69px");
    
    ToggleDadosPedido();
});

function GetQueryString(parameter) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    
    for (i=0;i<gy.length;i++) {
        ft = gy[i].split("=");
        if (ft[0] == parameter) {
            return ft[1];
        }
    }
}

function replaceQueryString(url,param,value) {
    var re = new RegExp("([?|&])" + param + "=.*?(&|$)","i");
    if (url.match(re))
        return url.replace(re,'$1' + param + "=" + value + '$2');
    else if (url.indexOf("?") == -1)
        return url + '?' + param + "=" + value;
    else
        return url + '&' + param + "=" + value;
}

function change_prob(text,value,container){
	for(theID in probList) if(probList[theID].probID == value) break;
	
	window.location.href=replaceQueryString(window.location.href,"id",probList[theID].probID);
	doTheChange(theID);
}

function doTheChange(whichID){
	//vars dos elementos:
	theDesc = probList[whichID].descricao;
	theData = probList[whichID].dados;
	theId = probList[whichID].probID;

	//muda o html:
	$(".descricaoProb").val(theDesc);
	$("input[@jQueryId=dadosPedido]").val(theData);
	$("input[@jQueryId=idTipoProblema]").val(theId);
}

function ToggleDadosPedido()
{
    if ($("input[@jQueryId=dadosPedido]").val() != "")
    {
        $("div.dadosDoPedido > label").css("display", "block");
        $("input[@jQueryId=dadosPedido]").css("display", "block");
    }
    else
    {
        $("div.dadosDoPedido > label").css("display", "none");
        $("input[@jQueryId=dadosPedido]").css("display", "none");
    }
}
$("input[@jQueryId=dadosPedido]").bind("change", ToggleDadosPedido);

function PopUpDivWithMessage()
{           
    var elements = new Array('backgroundFilterWindow','popupWindowMessage');
   
    // For the two elements to display or hide
    for (var i = 0; i< 2; i++){
        
        var div;        
        
        if(document.getElementById)
            // Standard way to get element
            div = document.getElementById(elements[i]); 
        else if(document.all) 
            // Get the element in old IE's 
            div = document.all[elements[i]];
       
        // if the style.display value is blank we try to check it out here 
        if(div.style.display == '' && div.offsetWidth != undefined && div.offsetHeight != undefined)
        {
            div.style.display = (div.offsetWidth != 0 && div.offsetHeight != 0)? 'block':'none'; 
        }
        
        // If the element is hidden ('none') then it will display it ('block') 
        // else
        // If the element is displayed ('block') then it will hide it ('none').
        div.style.display = (div.style.display == '' || div.style.display == 'block')? 'none':'block';
    }
}
