/* estandar de nombres

	showXxx : mostrar
	getXxxx : obtener un dato
	postXxxx: enviar
	xxxxCallBack: on return
*/
var _logged = 0;
var _id_seccion = 0;
var _id_promocion = 0;
var _lastHTML = '';
var _showOf = 0;

function showLogin(){

	setDiv('d_main',_login);

}

function doLogin(){

	setDiv('d_msg','Verificando....');
	
	var user= getValue('e_user');
	var pass= getValue('e_pass');
	
	if(user=='admin' && pass=='654321'){
		_logged = 1;
		showPanel();
		
		setDiv('m_login','<a href="javascript: doLogout();" class="gris">Logout</a> | <a href="javascript: showPanel();" class="gris">Panel</a>');
	}else{
		
		setDiv('d_msg','Los datos ingresados son erroneos, por favor verifique los mismos.');
	}
	
}

function showPanel(){
		setDiv('d_main',_panelControl);
		getSecciones();
}


function doLogout(){

	_logged=0;
	setDiv('m_login','<a href="javascript: showLogin();" class="gris">Login</a>');
	setDiv('d_main',_ofertasSemana);
	
}

function newSeccion(){
	_id_seccion =0;
	setDiv('d_secciones',_formSeccion);

}

function newOferta(){
	_id_oferta =0;
	setDiv('d_oferta',_formOferta);

}

function getSecciones(){

	setDiv('d_secciones','cargando...');
	sendAjax('get.php','action=getSecciones', getSeccionesCallBack);


}

function getSeccionesCallBack(){

	if(_ajax.readyState == 4){	

		var res = _ajax.responseText;			
		setDiv('d_secciones',res);
	
	}
 

}

function postSeccion(){
	
	
	
	var param = 'action=seccion';
	param +='&id_seccion='+_id_seccion;
	param +='&descripcion='+encodeURIComponent(getValue('e_descripcion_se'));
	param +='&tipo='+getValue('c_tipo');
	
	var checked = 0;
	if(document.getElementById('ch_activa_se').checked){
		checked = 1;
	}
	param +='&activa='+checked;
	
	setDiv('d_secciones','guardando...');
	
	sendAjax('post.php',param, postSeccionCallBack);


}

function postSeccionCallBack(){

	if(_ajax.readyState == 4){	

		getSecciones();
		
	
	}
 

}

function editSeccion(id){

	_id_seccion =id;
	setDiv('d_secciones',_formSeccion);
	setDiv('d_msg','cargando...');
	sendAjax('get.php','action=getSeccion&id_seccion='+id, editSeccionCallBack);

}

function editSeccionCallBack(){

	
	if(_ajax.readyState == 4){
		setDiv('d_msg','');
		var res = _ajax.responseText;
		var	res = res.split('|');
		setValue('e_descripcion_se',res[0]);
		setValue('c_tipo',res[1]);		
		if(res[2]=='0'){
			document.getElementById('ch_activa_se').checked = false;
		}
		setDiv('d_oferta','<a href="javascript: getOfertas();">Ver Ofertas</a>');
		
	}
	
}

function hideOfertas(){

	_id_oferta = 0;
	setDiv('d_oferta','<a href="javascript: getOfertas();">Ver Ofertas</a>');
	
}

function getOfertas(){
	sendAjax('get.php','action=getOfertas&id_seccion='+_id_seccion, getOfertasCallBack);

}

function getOfertasCallBack(){

	if(_ajax.readyState == 4){	
		var res = _ajax.responseText;
		var opt = '<a href="javascript: hideOfertas();">Ocultar Ofertas</a>';
		_lastHTML = opt+res;
		setDiv('d_oferta',_lastHTML);
		
	
	}
 

}

function getMenu(){
	sendAjax('get.php','action=getMenu', getMenuCallBack);

}

function getMenuCallBack(){

	if(_ajax.readyState == 4){	
		var res = _ajax.responseText;
		setDiv('d_menu',res);
		if (_showOf==1){
			getOfertasSemana();
		}
	
	}
 

}


function showSeccion(id){
	setDiv('d_main','cargando...');
	sendAjax('get.php','action=showSeccion&id_seccion='+id, showSeccionCallBack);

}

function showSeccionCallBack(){

	if(_ajax.readyState == 4){	
		var res = _ajax.responseText;
		_lastHTML = _ajax.responseText;
		setDiv('d_main',res);
		
	
	}
 

}

function cancelOferta(){
	setDiv('d_oferta',_lastHTML);
}


function postOferta(){
		
	var param = 'action=oferta';
	param +='&id_oferta='+_id_oferta;
	param +='&id_seccion='+_id_seccion;
	param +='&descripcion='+encodeURIComponent(getValue('e_descripcion'));
	param +='&condiciones='+encodeURIComponent(getValue('e_condiciones'));
	param +='&items='+encodeURIComponent(getValue('e_items'));
	param +='&titulo='+encodeURIComponent(getValue('e_titulo'));
	param +='&costo='+encodeURIComponent(getValue('e_costo'));
	param +='&desde='+getValue('e_desde');
	param +='&hasta='+getValue('e_hasta');
	
	
	var checked = 0;
	if(document.getElementById('ch_activa').checked){
		checked = 1;
	}
	param +='&activa='+checked;
	
	checked = 0;
	if(document.getElementById('ch_of_semana').checked){
		checked = 1;
	}
	param +='&of_semana='+checked;
	
	setDiv('d_oferta','guardando...');
	
	sendAjax('post.php',param, postOfertaCallBack);


}

function postOfertaCallBack(){

	if(_ajax.readyState == 4){	
		
		setDiv('d_oferta','actualizando...');	
		getOfertas();
		
	
	}
 

}

function editOferta(id){

  
	_id_oferta =id;
	setDiv('d_oferta',_formOferta);
	setDiv('d_msg','cargando...');
	sendAjax('get.php','action=getOferta&id_oferta='+id, editOfertaCallBack);

}

function editOfertaCallBack(){

	
	if((_ajax.readyState == 4) && (_ajax.status==200) ){
	    
		setDiv('d_msg','');
		var res = _ajax.responseText;
		var	res = res.split('|');
		setValue('e_titulo',res[0]);
		setValue('e_costo',res[1]);
		setValue('e_desde',res[2]);
		setValue('e_hasta',res[3]);
		setValue('e_descripcion',res[4]);
		setValue('e_condiciones',res[7]);
		setValue('e_items',res[8]);
				
		if(res[5]=='0'){
			document.getElementById('ch_activa').checked = false;
		}
		if(res[6]=='0'){
			document.getElementById('ch_of_semana').checked = false;
		}
		
		setDiv('d_img','<a href="javascript:checkImg();">Ver Imagen</a>');
		
		
		
	}
	
}

function checkImg(){
	
	sendAjax('get.php','action=checkImg&id_oferta='+_id_oferta, checkImgCallBack);
	

}

function checkImgCallBack(){

	
	if(_ajax.readyState == 4 ){
	
		if(_ajax.status==200){
			var res = _ajax.responseText;
			setDiv('d_img',res);
		}
	}
}

function LoadImg(){
	var _html = '<img src="images/indicator.gif">';	
	document.getElementById('d_img').innerHTML = _html;
}
function LoadImgReady(id){
    var time = new Date();
	var _html = '<img class="cImg" src="thumb.php?id_imagen='+id+'&time='+time+'"><br><a href="javascript:subImg('+_id_oferta+');">Eliminar Imagen</a>';	
	//alert(_html);
	document.getElementById('d_img').innerHTML = _html;
}
function upLoadFile(id){
	window.opener.LoadImg();
	//submit
	document.f_img.submit();     
	window.opener.LoadImgReady(id);
	//window.close();
}
function showLoadImg(id){
	window.open('loadImg.php?id_oferta='+id,'qq','width=300,height=150,toolbar=0,resizable=0');
}

function subImg(id){

sendAjax('get.php','action=subImg&id_oferta='+_id_oferta, subImgCallBack);
	

}

function subImgCallBack(){

	
	if(_ajax.readyState == 4 ){
	
		if(_ajax.status==200){
			setDiv('d_img',"<a href='javascript:showLoadImg("+_id_oferta+");'>Cargar una Imagen</a>");
		}
	}
}


function getOfertasSemana(){

  sendAjax('get.php','action=getOfertasSemana', getOfertasSemanaCallBack);
	

}

function getOfertasSemanaCallBack(){

	
	if(_ajax.readyState == 4 ){
	
		if(_ajax.status==200){
			
			_lastHTML = _ajax.responseText;
			setDiv('d_main',_ajax.responseText);
		}
	}
}

function showOferta(id){

 setDiv('d_main','cargando...');
  sendAjax('get.php','action=showOferta&id_oferta='+id, showOfertaCallBack);
	

}

function showOfertaCallBack(){

	
	if(_ajax.readyState == 4 ){
	
		if(_ajax.status==200){
			
			//setDiv('d_agencia','');	
			setDiv('d_main',_ajax.responseText);
		}
	}
}

function back(_div){

	setDiv(_div, _lastHTML);
}

