var formCityDef="Bielsko";
var formCementaryPrefix="formCementary";
var formCementaries=["Bielsko"];//,"Kozy","Mazancowice"];
var formOther="Other";
var formSelect="Select";

function GUICementaryShowAll(selectVal,textBool)
{
    for (var c in formCementaries)
        docEl(formCementaryPrefix+formCementaries[c]).style.display="none";
    if (selectVal!="")
    {
        docEl(formCementaryPrefix+selectVal).style.display="inline";
        if (docEl(formCementaryPrefix+selectVal).value==formOther)
            docEl(formCementaryPrefix+selectVal).value=formSelect;        
    }
    docEl("formCementaryText").style.display=textBool?"inline":"none";    
}

function GUICementaryShow(val) //No Other field possible
{
    GUICementaryShowAll(val,false);
}

function GUICementaryText()
{   
    GUICementaryShowAll("",true);    
}
function GUICementaryTextShowHide(textBool)
{
    docEl("formCementaryText").style.display=textBool?"inline":"none";    
}

function GUICementaryHide()
{   
    GUICementaryShowAll("",false);
}


function GUICityTextShowHide(textBool)
{
    docEl("formCityText").style.display=textBool?"inline":"none";    
}

function GUICityChangeHandler(val)
{
    if (val==formSelect)
    {
        GUICityTextShowHide(false);
        GUICementaryHide();
    }
    else if (val==formOther)
    {
        GUICityTextShowHide(true);
        GUICementaryText();
    }
    else
    {
        GUICityTextShowHide(false);
        GUICementaryShow(val);
    }
}

//on Cementary DDB select change
function GUICementaryChangeHandler(val)
{
    GUICementaryTextShowHide(val==formOther); //show or hide additional text field
}

function GUIGetCity()
{
    if (docEl("formCity").value==formSelect) return "";
    if (docEl("formCity").value==formOther) 
        return textTrim(docEl("formCityText").value);
    return docEl("formCity").value;
}

function GUIGetCementary()
{
    var sc=null;
    
    for (var c in formCementaries)
    {
        var sc2=docEl(formCementaryPrefix+formCementaries[c]);
        if (sc2.style.display!="none") 
        {
            sc=sc2;
            break;
        }
    }
    

    if (sc==null)
        return textTrim(docEl("formCementaryText").value);
    
    if (sc.value==formSelect) return "";
    if (sc.value==formOther) 
        return textTrim(docEl("formCementaryText").value);
    return sc.value;
}
function GUIGetName(){   return textTrim(docEl("formName").value);}
function GUIGetSurname(){   return textTrim(docEl("formSurname").value);}
function GUIGetDeathDate(){   return textTrim(docEl("formDeathDate").value);}
function GUIGetGravePlace(){   return textTrim(docEl("formGravePlace").value);}
function GUIGetProduct(){   return textTrim(docEl("formProduct").value);}
function GUIGetProductAdd(){   return textTrim(docEl("formProductAdd").value);}
function GUIGetPhone(){   return textTrim(docEl("formPhone").value);}
function GUIGetMail(){   return textTrim(docEl("formMail").value);}

function GUISetAlert(a)
{
    docEl("labelName").className=a[0]?"alert":"";
    docEl("labelSurname").className=a[1]?"alert":"";
    docEl("labelDeathDate").className=a[2]?"alert":"";
    docEl("labelCity").className=a[3]?"alert":"";
    docEl("labelCementary").className=a[4]?"alert":"";
    docEl("labelGravePlace").className=a[5]?"alert":"";
    docEl("labelContact").className=(a[8] && a[9])?"alert":"";
}

function GUISubmit()
{    
    var formRes=[GUIGetName(),GUIGetSurname(),GUIGetDeathDate(),GUIGetCity(),GUIGetCementary(),
           GUIGetGravePlace(),GUIGetProduct(),GUIGetProductAdd(),GUIGetPhone(),GUIGetMail()];
    
    var formEmpty=new Array();
       
    var isEmpty=false;
    
    for (var f in formRes)
    {
        formEmpty[f]=(formRes[f]=="");
        if (f<6 && formRes[f]=="") isEmpty=true;
        if (formRes[8]=="" && formRes[9]=="") isEmpty=true;
    }

    GUISetAlert(formEmpty);
    if (isEmpty)
    {
        var msg="Proszę wypełnić pola obowiązkowe (zaznaczone na czerwono).";
        if (formEmpty[8] && formEmpty[9]) msg+=" Proszę podać kontakt (telefon lub pocztę)";
        alert(msg);
    }
    
    
    
//    alert("City :"+GUIGetCity()+"     Cementary: "+GUIGetCementary());
    return false;
}

function GUIInit()
{
    var gm=docEl("formCity");
    if (typeof gm!='undefined' && gm!=null)
    {
        gm.value=formCityDef;
        GUICityChangeHandler(formCityDef);
    }
}

/* INIT */
function init()
{
    GUIInit();
}
    
