var domain_url = 'http://swfradio.com';
//var domain_url = 'http://localhost/swfradio';

document.writeln('<script src="'+domain_url+'/js/JsHttpRequest.js" charset="utf-8"></script>');

function SWF_ShowForm() {
    owl = document.createElement('div');
    owl.setAttribute('id', 'swf-form-overlay');

    f = document.createElement('div');
    f.setAttribute('id', 'swf-form');

    //Hide on click      
    owl.onclick = function(){
        document.body.removeChild(owl);
        document.body.removeChild(f);
    }

    document.body.appendChild(owl);
    document.body.appendChild(f);
    
    JsHttpRequest.query(
        domain_url+"/ajax/addradio.php?action=get_form",    // backend address
        {}, // parameters
        function(result, errors) {
            f.innerHTML = result;
        }
    );
} 

function SWF_CloseForm() {
    owl = document.getElementById("swf-form-overlay");
    f = document.getElementById("swf-form");
    
    document.body.removeChild(owl);
    document.body.removeChild(f);
}

function SWF_DoneForm() {
    oLoading.style.display = "none";    
    oMessage.style.display = "block";
    oForm.reset();
    
    setTimeout(SWF_CloseForm, 500);
}

function SWF_SendForm() {
    oForm      = document.getElementById("idFormAddRadio");
    
    charset = (window.document.charset != "undefined") ? window.document.charset : window.document.characterSet;
    
    oName        = document.getElementById("idName");
    oCountry     = document.getElementById("idCountry");
    oCity        = document.getElementById("idCity");
    oFrequency   = document.getElementById("idFrequency");
    oUrlSite     = document.getElementById("idUrlSite");
    oDescription = document.getElementById("idDescription");
    oImg         = document.getElementById("idImg");
    oLogoLink    = document.getElementById("idLogoLink");
    
    oUrlStream   = document.getElementsByName("url_stream[]");
    
    oContactName = document.getElementById("idContactName");
    oEmail       = document.getElementById("idEmail");
    oIcq         = document.getElementById("idIcq");

    oLoading   = document.getElementById("idLoading");
    oMessage   = document.getElementById("idMessage");
    oError     = document.getElementById("idError");
    
    oBtnSend   = document.getElementById("idBtnSend");

    oError.style.display   = "none";
    oMessage.style.display = "none";
    oLoading.style.display = "block";
    oBtnSend.disabled = true;
    
    
    
    if (!oName.value || !oCountry.value || !oCity.value || !oFrequency.value || 
        !oUrlSite.value || !oDescription.value || !oContactName.value || !oEmail.value || !oIcq.value || !oUrlStream[0].value) {
        oLoading.style.display = "none";
        oError.style.display = "block";
        oBtnSend.disabled = false;
    } else {
        var req = new JsHttpRequest();
        req.open(null, domain_url+"/ajax/addradio.php?action=save&charset="+charset, true);
        req.send( { q: oForm } );
        
        setTimeout(SWF_DoneForm, 500);
    }
}

function SWF_AddField() {
    oType = document.getElementById('idType');
    HtmlType = oType.cloneNode(true);
    
    
    var div = document.createElement("div");
    div.innerHTML = "<label>&nbsp;</label><input type='text' name='url_stream[]' class='form-input-stream' />";
    div.innerHTML += "<label>&nbsp;</label><input type='text' name='quality_stream[]' class='form-input-quality' />";
    div.innerHTML += "<label>&nbsp;</label>";
    div.appendChild(HtmlType);
    
    document.getElementById("idStreams").appendChild(div);
    return false;
}
