function eliminar(preg,URL) {
	if(confirm(preg)) {
		location = URL;
	}
}

function $(id) {
	return document.getElementById(id);	
}


function popup(URL,w,h,vars) {
	
    var left = (screen.width - w) / 2;
    var top  = (screen.height - h) / 2
	
	window.open(URL,"","left="+left+",top=" + top + ",width="+w+",height="+h+vars);
	
}

function post(url, vars){
	var xml = null;
	try {
		xml = new ActiveXObject('Microsoft.XMLHTTP');
	}catch(exception){
		xml = new XMLHttpRequest();
	}
	xml.open('POST',url,false);
	xml.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xml.send (vars);
	if(xml.status == 404) alert('Url no valida');
	return xml.responseText;
}

//###############################################################################################################
//#										    	LOCALIDADES
//###############################################################################################################

function get_localidades_editar(idp) {
	
	var data = post("../inc/ajax.php" , "act=get_localidades_editar&idp="+idp);
	
	if(data != '') {
		$("localidades").innerHTML = data;
	}	
	
}

function cancelar_editar_localidad() {

	var data = post("../inc/ajax.php" , "act=get_formulario_agregar_localidad");
	
	if(data != '') {
		$("formulario_localidades").innerHTML = data;
	}	
	
}

function get_formulario_editar_localidades(idl) {

	
	var data = post("../inc/ajax.php" , "act=get_formulario_editar_localidad&idl="+idl);
	
	if(data != '') {
		$("formulario_localidades").innerHTML = data;
	}		
	
}

function get_localidades(idp) {
	
	var vars = "act=get_localidades_sel&idp=" + idp;

	var data = post("../inc/ajax.php",vars);
	
	if(data != '') {
		
		$("localidades").innerHTML = data;
		
	}
	
}

//###############################################################################################################
//#										    	TARIFAS
//###############################################################################################################

function get_localidades_tarifas(idp) {

	var data = post("../inc/ajax.php","act=get_localidades_table&idp=" + idp);
	
	if(data != '') {
		$("localidades").innerHTML = data;
	}
	
	
}

function save_tarifa(idt , idl) {
	
	if(idt == '') {
		idt = 0;
	}
	
	var data = post("../inc/ajax.php","act=save_localidad_tarifa&idt="+idt+"&idl=" + idl);

	
}


//###############################################################################################################
//#										    	PEDIDOS
//###############################################################################################################

function actualizar_pedidos(id) {

	var vars = "act=actualizar_pedido&idp=" + id;
	var data = post("../inc/ajax.php",vars);
	
	if(data != '') {
		$('pedido_actualizado').innerHTML = data;
	}
	else {
		alert(data);	
	}
	
}


function get_limpieza(id) {
	
	var data = post("../inc/ajax.php" , "act=get_limpiezas&idpe=" + id);
	
	if(data != '') {
		$("limpiezas").innerHTML = data;
	}
	
}

//###############################################################################################################
//#										    	 PRECIOS
//###############################################################################################################

function actualizar_formula(id , idp ) {
	
	var pc    = parseFloat($("pc_"+id).value);
	var merma = parseFloat($("merma_"+id).value);
	var iva   = parseFloat($("iva_"+id).value);
	var peso  = parseFloat($("peso_"+id).value);
	
	var l  = $("l_"+id).value;
	var c  = $("c_"+id).value;
	var cp = $("cp_"+id).value;
	var m  = $("m_"+id).value;
	var mp = $("mp_"+id).value;
	var b  = $("b_"+id).value;
	
	var P = ((pc + (pc * iva)/100)*merma) + (pc + (pc * iva)/100);
	
	$("precio_coste_"+id).innerHTML = number_format(P,2,',','.');
	
	var data = post("../inc/ajax.php","act=actualizar_pvp_admin&idp=" + idp + "&merma="+merma+"&pc="+pc+"&iva="+iva+"&peso="+peso+"&l="+l+"&c="+c+"&cp="+cp+"&m="+m+"&mp="+mp+"&b="+b );	
	
	var tarifas_precios = data.split("||");	
	
	$("disp_"+id+"_l").innerHTML  = number_format(tarifas_precios[0],2,',','.');
	$("disp_"+id+"_c").innerHTML  = number_format(tarifas_precios[1],2,',','.');
	$("disp_"+id+"_cp").innerHTML = number_format(tarifas_precios[2],2,',','.');
	$("disp_"+id+"_m").innerHTML  = number_format(tarifas_precios[3],2,',','.');
	$("disp_"+id+"_mp").innerHTML = number_format(tarifas_precios[4],2,',','.');
	$("disp_"+id+"_b").innerHTML  = number_format(tarifas_precios[5],2,',','.');
	
}

function change_disp(id,sec) {
	
	if($("disp_" + id + "_" + sec)) {
		var clase = $("disp_" + id + "_" + sec).getAttribute("class");
		
		if(clase == "disp_verde") {
			$("disp_" + id + "_" + sec).setAttribute("class","disp_rojo");
			$("s_disp_" + id + "_" + sec).value = 0;
		}
		else {
			$("disp_" + id + "_" + sec).setAttribute("class","disp_verde");
			$("s_disp_" + id + "_" + sec).value = 1;
		}
	}
	
}

//###############################################################################################################
//#										    	PRODUCTOS
//###############################################################################################################

function cheque_tipo_magnitud() {

	if(($("unidad").value == 0 && $("magnitud").value == 0) || ($("unidad").value == 1 && $("magnitud").value == 1)|| ($("unidad").value == 0 && $("magnitud").value == 1)) {
		$("peso").readOnly = true;
		$("peso").style.backgroundColor = "#ccc;";
		$("peso").value = 1000;		
	}
	else {
		$("peso").readOnly = false;
		$("peso").style.backgroundColor = "#F2F2F2;";
	}
	
	if($("unidad").value == 1 && $("magnitud").value == 1) {
		$("plus_envase_div").style.display = "block";	
	}
	else {
		$("plus_envase_div").style.display = "none";	
		$("plus_envase").value = '';	
	}
	
	
}

function get_des_caract() {

	var len = $('descripcion').value.length;
	$('limit_desc').innerHTML = len;
	
}

//###############################################################################################################
//#										    	VARIAS
//###############################################################################################################


//BY Joksnet 
function number_format( number, decimals, dec_point, thousands_sep )
{
    number = Math.round( number * Math.pow(10, decimals) ) / Math.pow(10, decimals);
 
    var str_number = number + '';
    var arr_int = str_number.split(".");
    var str_int = '';
    var str_decimals = '';
    var str_ext = '';
 
    if ( !arr_int[0] ) arr_int[0] = '0';
    if ( !arr_int[1] ) arr_int[1] = '';
 
    if ( arr_int[1].length < decimals )
    {
        str_decimals = arr_int[1];
 
        for ( var i = arr_int[1].length + 1; i <= decimals; i++ )
        {
            str_decimals += '0';
        }
 
        arr_int[1] = str_decimals;
    }
 
    if ( thousands_sep != '' && arr_int[0].length > 3 )
    {
        str_int = arr_int[0];
        arr_int[0] = '';
 
        for ( var j = 3; j < str_int.length; j += 3 )
        {
            str_ext = str_int.slice( str_int.length - j, str_int.length - j + 3 );
            arr_int[0] = thousands_sep + str_ext +  arr_int[0] + '';
        }
 
        str_first = str_int.substr( 0, ( str_int.length % 3 == 0 ) ? 3 : ( str_int.length % 3 ) );
        arr_int[0] = str_first + arr_int[0];
    }
 
    return arr_int[0] + dec_point + arr_int[1];
}

function isMail(id)
{
	var s = document.getElementById(id);
	var filter=/^[A-Za-z][A-Za-z0-9_.-]*@[A-Za-z0-9_.-]+\.[A-Za-z0-9]+[A-za-z]$/;
	if (s.length == 0 ) return true;
	
	if (filter.test(s.value))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function html_decode(data)
{
	var span = document.createElement('span');
	span.innerHTML = data;
	return span.innerHTML;
}

function enviar_contacto()
{
	var msg = '';
	if ($('nombre').getValue().trim().length == 0)
	{
		msg += 'Debe completar el campo nombre.\n';
	}
	if ($('apellido').getValue().trim().length == 0)
	{
		msg += 'Debe completar el campo apellido.\n';
	}
	
	if ($('email').getValue().trim().length == 0)
	{
		msg += html_decode('Debe ingresar la direcci&oacute;n de email.\n');
	}
	else if(!isMail('email'))
	{
		msg += html_decode('Debe ingresar una direcci&oacute;n de email v&aacute;lida.\n');
	}
	if ($('mensaje').getValue().trim().length == 0)
	{
		msg += 'Debe ingresar el mensaje';
	}
	
	if ('' != msg)
	{
		alert(msg);
	}
	else
	{
		$('form_contacto').submit();
	}
}

function viewPic(img){
	picfile = new Image();
	picfile.src =(img);
	fileCheck(img);
}

function fileCheck(img){
	if( (picfile.width!=0) && (picfile.height!=0) ){
		makeWindow(img);
	}else{
		funzione="fileCheck('"+img+"')";
		intervallo=setTimeout(funzione,50);
	}
}

function makeWindow(img){
	ht = picfile.height;
	wd = picfile.width;

	var args= "height=" + ht + ",innerHeight=" + ht;
	args += ",width=" + wd + ",innerWidth=" + wd;
	if (window.screen){
		var avht = screen.availHeight;
		var avwd = screen.availWidth;
		var xcen = (avwd - wd) / 2;
		var ycen = (avht - ht) / 2;
		args += ",left=" + xcen + ",screenX=" + xcen;
		args += ",top=" + ycen + ",screenY=" + ycen + ",resizable=yes";
	}
	popwin=window.open("","_blank",args)
	popwin.document.open()
	popwin.document.write('<html><head><title>'+img+'</title></head><body bgcolor=white scroll=no topmargin=0 leftmargin=0 rightmargin=0 bottomargin=0 marginheight=0 marginwidth=0><div style="position: absolute; top:0px;left:0px"><a href="javascript:window.close()"><img src="'+img+'" width="'+wd+'" height="'+ht+'" border="0"></a></div></body></html>')
	popwin.document.close()
}
