var isIE = (navigator.appName.indexOf ('Microsoft') != -1);

function iframeGoto (id, url) {
    if (isIE) document.frames[id].location = url;
    else document.getElementById (id).src = url;
}

function reloadPage(msg) {
    self.location = self.location.href.concat(msg);
}

function submitformpromo() {
	
	var _ok = wbValidateFormL(document.getElementById('fcontacto'),'es');
	
	if(_ok) {
		var f = document.getElementById('fcontacto');
		var _id = f.id.value;
		var _inicio_dia = f.inicio_dia.value;
		var _inicio_mes = f.inicio_mes.value;
		var _inicio_anno = f.inicio_anno.value;
		var _fin_dia = f.fin_dia.value;
		var _fin_mes = f.fin_mes.value;
		var _fin_anno = f.fin_anno.value;
		var _titulo = f.titulo.value;
		var _texto = f.texto.value;
        var _link = f.link.value;
        var _file = f.msguploadedfile.value;
        var _filetype = f.msguploadedfiletype.value;
        	
        $.ajax({
           url: "sendPromo.php",
           type: "POST",
           dataType: "text",
           data: {id: _id, inicio_dia: _inicio_dia, inicio_mes: _inicio_mes, inicio_anno: _inicio_anno, 
           		fin_dia: _fin_dia, fin_mes: _fin_mes, fin_anno: _fin_anno, 
           		titulo: _titulo, texto: _texto, link: _link, 
           		tempfileid: _file, tempfiletype: _filetype},	           		
           error: function(req, err, obj) {
            alert(err);
           },
           success: function(txt) {
            if (txt == "OK") {
                alert ('Envío de promo realizado.');
                reloadPage('?command=ok');
            } else {
            	alert('Error ' + txt);
            	if (txt == "00") alert('Ha ocurrido un error al enviar su promoción. por favor, inténtelo más tarde.');
            	else  alert(txt);
            	reloadPage('?command=err');
            }
           }
        });            
        
        
	}
	return(false);
}


$(document).ready(function (){
	$("#formsubmit").click(submitformpromo);  
    $("#msgfile").change(uploadfile);
   
}
);

function uploadfile (event) {
    var msgfile = event.target;
    var frm = msgfile.form;
   	
    if (msgfile.value != '') {
        document.getElementById ('div_msgfile').style.display = 'none';
        document.getElementById ('div_uploading').style.display = 'block';
        document.getElementById ('formsubmit').disabled = true;
        frm.submit();
    }
}

function fileuploaded (filename, fileid, filetype) {
    var frm = document.getElementById ('fcontacto');
    frm.msguploadedfile.value = fileid;
    
    frm.msguploadedfiletype.value = filetype;
    var dv = document.getElementById ('div_uploadedfile');
    dv.innerHTML = filename;
    dv.style.display = 'block';
    document.getElementById ('div_msgfile').style.display = 'block';
    document.getElementById ('div_uploading').style.display = 'none';
    document.getElementById ('formsubmit').disabled = false;
    frm.msgfile.value = '';
}
