// formulario en comun para todas las validaciones

function foco_en_ancla_presu(){
	document.location.href = "#datos_presu";		
}
function foco_en_ancla_cliente(){
	document.location.href = "#datos_cliente";		
}

function ver_tiraje(valor_horneado){
	if(valor_horneado){
		document.getElementById('div_tiraje').style.display='inline'
		document.getElementById('txt_tiraje').disabled=false
		document.getElementById('txt_tiraje').focus();
	}else{
		document.getElementById('div_tiraje').style.display='none'
		document.getElementById('txt_tiraje').disabled=true
	}
	return true;
}

function cambioRetiraTW() {

	retiraTW=document.getElementById('rdb_entrega_tw').checked;
	pordefecto=document.getElementById('txt_entregar_en_defecto');
	direccion=document.getElementById('txt_entregar_en');
	if (retiraTW) {
		pordefecto.value=direccion.value;
		direccion.value="Retira en TW";
		direccion.disabled=true;
	} else {
		direccion.value=pordefecto.value;
		direccion.disabled=false;
		direccion.focus();
	}
	return true;
}

function cambioPinza() {
	archivoListo=document.getElementById('rdb_pinza_listo').checked;
	pordefecto=document.getElementById('txt_pinza_defecto');
	pinza=document.getElementById('txt_pinza');
	if (archivoListo) {
		pordefecto.value=pinza.value;
		pinza.value="En pinza, listo para imprimir";
		pinza.disabled=true;
	} else {
		pinza.value=pordefecto.value;
		pinza.disabled=false;
		pinza.focus();
	}
}

function validadcion_datosCliente(){
	var formu  =  document.form1;		
	
	//CAMPOS------
	//txt_cod
	//txt_empresa
	//txt_nombre
	//txt_telefono
	//txt_mail
	
	//restablecer campos
	document.getElementById('txt_rsocial').className = 'campos';
	document.getElementById('txt_nombre').className = 'campos';
	document.getElementById('txt_telefono').className = 'campos';
	document.getElementById('txt_mail').className = 'campos';
	//
	
	// quito los espacios en blanco de adelante de todos los campos
	 while(formu.txt_rsocial.value.charAt(0) == " "){
		formu.txt_rsocial.value = formu.txt_rsocial.value.substring(1, formu.txt_rsocial.value.length);
	 }
	 while(formu.txt_nombre.value.charAt(0) == " "){
		formu.txt_nombre.value = formu.txt_nombre.value.substring(1, formu.txt_nombre.value.length);
	 }
	 while(formu.txt_telefono.value.charAt(0) == " "){
		formu.txt_telefono.value = formu.txt_telefono.value.substring(1, formu.txt_telefono.value.length);
	 }
	  while(formu.txt_mail.value.charAt(0) == " "){
		formu.txt_mail.value = formu.txt_mail.value.substring(1, formu.txt_mail.value.length);
	 }
	//
	
	//validaiones
	if(formu.txt_rsocial.value==""){
		document.getElementById('txt_rsocial').className = 'campo_error';
		puede_ingresar = false;
		if(enfoco==false){
			foco_en_ancla_cliente();
			enfoco = true;
		}
	}
	
	if(formu.txt_nombre.value==""){
		document.getElementById('txt_nombre').className = 'campo_error';
		puede_ingresar = false;
		if(enfoco==false){
			foco_en_ancla_cliente();
			enfoco = true;
		}
	}
	
	if(formu.txt_telefono.value==""){
		document.getElementById('txt_telefono').className = 'campo_error';
		puede_ingresar = false;
		if(enfoco==false){
			foco_en_ancla_cliente();
			enfoco = true;
		}
	}
	
	if(!emailCheck(formu.txt_mail.value)){
		document.getElementById('txt_mail').className = 'campo_error';
		puede_ingresar = false;
		if(enfoco==false){
			foco_en_ancla_cliente();
			enfoco = true;
		}
	}
	


}

/////////-------- CAMPOS COMUNES
function validar_campos_comunes(){

	var formu  =  document.form1;	

	//restablecer campos
	document.getElementById('txt_referencia').className = 'campos';
		
	 //***VALIDACIONES**//
	 if(formu.txt_referencia.value==""){
		document.getElementById('txt_referencia').className = 'campo_error';
		puede_ingresar = false;
		if(enfoco==false){
			foco_en_ancla_presu();	
			enfoco = true;
		}
	}
 
}
/////////--------FIN CAMPOS COMUNES


/////////-------- ARCHIVOS
function validar_archivos(){

	var formu  =  document.form1;	

	selecciono_archivo=false;
	
	for (i=1;i<11;i++) {
		archivo=formu.elements["archivo[" + i + "]"];
		selecciono_archivo=(selecciono_archivo || (archivo.value!="") );
		archivo.className = 'campos';
	}
	
	if(!selecciono_archivo){
		document.form1.elements["archivo[1]"].className = 'campo_error';
		puede_ingresar = false;
		if(enfoco==false){
			foco_en_ancla_presu();	
			enfoco = true;
		}
	}
		
 
}
/////////--------FIN ARCHIVOS

function validar_CTP(tipo_de_envio_PARAM){
	
	//CAMPOS--------
	//txt_referencia
	//txt_formato_ancho
	//txt_formato_alto
	//txt_calibre
	//txt_cantidad
	//chk_horneado
	//rbt_tipo_de_archivo
	//chk_armado_de_pliego
	//txt_forma_de_pago
	//txt_observaciones
	
	var formu  =  document.form1;	
	puede_ingresar = true;
	enfoco = false;
	
	if(formu.chk_horneado.checked){
		while(formu.txt_tiraje.value.charAt(0) == " "){
			formu.txt_tiraje.value = formu.txt_tiraje.value.substring(1, formu.txt_tiraje.value.length);
		}	
		 if(formu.txt_tiraje.value==""){
			document.getElementById('txt_tiraje').className = 'campo_error';
			document.getElementById('txt_tiraje').style.background = '#FFDFDF';
			puede_ingresar = false;
			if(enfoco==false){
				foco_en_ancla_presu();
				enfoco = true;
			}
		 }
	}
	

	
		document.getElementById('txt_pinza').className = 'campos';
		document.getElementById('txt_tinta_otra').className = 'campos';			
		document.getElementById('txt_tinta_otra').style.background = '#F7F7F7';
		document.getElementById('txt_entregar_en').className = 'campos';
		
		if(formu.txt_pinza.value==""){
			document.getElementById('txt_pinza').className = 'campo_error';
			puede_ingresar = false;
			if(enfoco==false){
				foco_en_ancla_presu();
				enfoco = true;
			}
		} else {

		}
		
		if(formu.txt_entregar_en.value==""){
			document.getElementById('txt_entregar_en').className = 'campo_error';
			puede_ingresar = false;
			if(enfoco==false){
				foco_en_ancla_presu();
				enfoco = true;
			}
		}

		///***VALIDAR TINTAS***//
		document.getElementById('Ttintas').bgColor = '#FFFFFF';			
		document.getElementById('Ttintas2').bgColor = '#FFFFFF';
		
		if((!formu.tintaC.checked)&&(!formu.tintaM.checked)&&(!formu.tintaY.checked)&&(!formu.tintaB.checked)&&(!formu.tinta_compuesta.checked)&&(!formu.tinta_otra.checked)){
			
			document.getElementById('Ttintas').bgColor = '#FFDFDF';			
			document.getElementById('Ttintas2').bgColor = '#FFDFDF';
						
			puede_ingresar = false;
			if(enfoco==false){
				foco_en_ancla_presu();
				enfoco = true;
			}
		}
		if(formu.tinta_otra.checked){
			
			while(formu.txt_tinta_otra.value.charAt(0) == " "){
			formu.txt_tinta_otra.value = formu.txt_tinta_otra.value.substring(1, formu.txt_tinta_otra.value.length);
			}	
			
			if(formu.txt_tinta_otra.value==""){
			document.getElementById('txt_tinta_otra').className = 'campo_error';				
			document.getElementById('txt_tinta_otra').style.background = '#FFDFDF';
			puede_ingresar = false;
				if(enfoco==false){
					foco_en_ancla_presu();
					enfoco = true;
				}
			}
		}

	
	validar_campos_comunes();
	
	validar_archivos();
	

	document.getElementById('cb_formato').className = 'campos';
	
	document.getElementById('FFormato1').bgColor = '#FFFFFF';			
	document.getElementById('FFormato2').bgColor = '#FFFFFF';

	if(formu.cb_formato.value==""){			
			 if(formu.txt_formato_ancho.value==""){
				document.getElementById('cb_formato').className = 'campo_error';
				document.getElementById('FFormato1').bgColor = '#FFDFDF';			
				document.getElementById('FFormato2').bgColor = '#FFDFDF';
				puede_ingresar = false;
				if(enfoco==false){
					foco_en_ancla_presu();
					enfoco = true;
				}
			}
		}
		if(formu.cb_formato.value=="Otro"){
			document.getElementById('txt_formato_ancho').className = 'campos';
			document.getElementById('txt_formato_alto').className = 'campos';
			  while(formu.txt_formato_ancho.value.charAt(0) == " "){
				formu.txt_formato_ancho.value = formu.txt_formato_ancho.value.substring(1, formu.txt_formato_ancho.value.length);
			 }
			  while(formu.txt_formato_alto.value.charAt(0) == " "){
				formu.txt_formato_alto.value = formu.txt_formato_alto.value.substring(1, formu.txt_formato_alto.value.length);
			 }
			 if(formu.txt_formato_ancho.value==""){
				document.getElementById('txt_formato_ancho').className = 'campo_error';
				puede_ingresar = false;
				if(enfoco==false){
					foco_en_ancla_presu();
					enfoco = true;
				}
			}
			 if(formu.txt_formato_alto.value==""){
				document.getElementById('txt_formato_alto').className = 'campo_error';
				puede_ingresar = false;
				if(enfoco==false){
					foco_en_ancla_presu();
					enfoco = true;
				}
			}
			 document.getElementById('txt_calibre').className = 'campos';
			 while(formu.txt_calibre.value.charAt(0) == " "){
				formu.txt_calibre.value = formu.txt_calibre.value.substring(1, formu.txt_calibre.value.length);
			 }	
			
			 if(formu.txt_calibre.value==""){
				document.getElementById('txt_calibre').className = 'campo_error';
				puede_ingresar = false;
				if(enfoco==false){
					foco_en_ancla_presu();
					enfoco = true;
				}
			} 	 
		}
	////////////////

	if(puede_ingresar){
		
		if(tipo_de_envio_PARAM=="Presupuesto"){		
		
			ver_div_enviando();
			enviar_form_presupuesto("CTP");
			
		}else if(tipo_de_envio_PARAM=="Envio"){		
			document.getElementById('txt_entregar_en').disabled=false;
			document.getElementById('txt_pinza').disabled=false;
			document.getElementById('form1').submit();
			
		}
	}
	return
	
}
function validar_CTF(tipo_de_envio_PARAM){
	var formu  =  document.form1;		
	puede_ingresar = true;
	enfoco = false;
	//CAMPOS--------
	//txt_referencia
	//txt_formato_ancho
	//txt_formato_alto
	//txt_calibre
	//txt_cantidad
	//chk_horneado
	//rbt_tipo_de_archivo
	//chk_armado_de_pliego
	//txt_forma_de_pago
	//txt_observaciones
	
	

	//	document.getElementById('txt_linea').className = 'campos';		
		document.getElementById('txt_tinta_otra').className = 'campos';			
		document.getElementById('txt_tinta_otra').style.background = '#F7F7F7';
		document.getElementById('txt_entregar_en').className = 'campos';
		
		while(formu.txt_entregar_en.value.charAt(0) == " "){
			formu.txt_entregar_en.value = formu.txt_entregar_en.value.substring(1, formu.txt_entregar_en.value.length);
		 }
		
		if(formu.txt_entregar_en.value==""){
			document.getElementById('txt_entregar_en').className = 'campo_error';
			puede_ingresar = false;
			if(enfoco==false){
				foco_en_ancla_presu();
				enfoco = true;
			}
		}
		

		
		///***VALIDAR TINTAS***//
		document.getElementById('Ttintas').bgColor = '#FFFFFF';			
		document.getElementById('Ttintas2').bgColor = '#FFFFFF';
		
		if((!formu.tintaC.checked)&&(!formu.tintaM.checked)&&(!formu.tintaY.checked)&&(!formu.tintaB.checked)&&(!formu.tinta_compuesta.checked)&&(!formu.tinta_otra.checked)){
			document.getElementById('Ttintas').bgColor = '#FFDFDF';			
			document.getElementById('Ttintas2').bgColor = '#FFDFDF';
						
			puede_ingresar = false;
			if(enfoco==false){
				foco_en_ancla_presu();
				enfoco = true;
			}
		}
		if(formu.tinta_otra.checked){				
			
			while(formu.txt_tinta_otra.value.charAt(0) == " "){
			formu.txt_tinta_otra.value = formu.txt_tinta_otra.value.substring(1, formu.txt_tinta_otra.value.length);
			}	
			
			if(formu.txt_tinta_otra.value==""){
			document.getElementById('txt_tinta_otra').className = 'campo_error';				
			document.getElementById('txt_tinta_otra').style.background = '#FFDFDF';
			puede_ingresar = false;
				if(enfoco==false){
					foco_en_ancla_presu();
					enfoco = true;
				}
			}
		}
	

	validar_campos_comunes();	
	validar_archivos();

	document.getElementById('txt_formato_alto').className = 'campos';
	  while(formu.txt_formato_alto.value.charAt(0) == " "){
		formu.txt_formato_alto.value = formu.txt_formato_alto.value.substring(1, formu.txt_formato_alto.value.length);
	 }
	 if(formu.txt_formato_alto.value==""){
		document.getElementById('txt_formato_alto').className = 'campo_error';
		puede_ingresar = false;
		if(enfoco==false){
			foco_en_ancla_presu();
			enfoco = true;
		}
	}

	
	if(puede_ingresar){
		document.getElementById('txt_entregar_en').disabled=false;
		document.getElementById('form1').submit();
			
	}
	return
	
}


function validar_pruebas_color(tipo_de_envio_PARAM){
	var formu  =  document.form1;	
	puede_ingresar = true;
	enfoco = false;
	//CAMPOS
	//txt_referencia
	//txt_formato_alto
	//txt_cantidad
	//rdb_papel
	//rdb_media
	//rdb_entrega
	//txt_forma_de_pago
	//txt_observaciones


	validar_campos_comunes();
	/* cantidad pasa a ser no requerido (23 mayo 2009)
	document.getElementById('txt_cantidad').className = 'campos';
	 while(formu.txt_cantidad.value.charAt(0) == " "){
		formu.txt_cantidad.value = formu.txt_cantidad.value.substring(1, formu.txt_cantidad.value.length);
	 }
	if(formu.txt_cantidad.value==""){
		document.getElementById('txt_cantidad').className = 'campo_error';
		puede_ingresar = false;
		if(enfoco==false){
			foco_en_ancla_presu();
			enfoco = true;
		}
	}
	*/
	////******///////
	document.getElementById('txt_formato_alto').className = 'campos';

	 if(formu.txt_formato_alto.value==""){
		document.getElementById('txt_formato_alto').className = 'campo_error';
		puede_ingresar = false;
		if(enfoco==false){
			foco_en_ancla_presu();
			enfoco = true;
		}
	}
	
	if(tipo_de_envio_PARAM=="Envio"){		
		document.getElementById('txt_entregar_en').className = 'campos';
		
		while(formu.txt_entregar_en.value.charAt(0) == " "){
			formu.txt_entregar_en.value = formu.txt_entregar_en.value.substring(1, formu.txt_entregar_en.value.length);
		 }
		
		if(formu.txt_entregar_en.value==""){
			document.getElementById('txt_entregar_en').className = 'campo_error';
			puede_ingresar = false;
			if(enfoco==false){
				foco_en_ancla_presu();
				enfoco = true;
			}
		}
	}

	////////

	if(puede_ingresar){
		
		if(tipo_de_envio_PARAM=="Presupuesto"){		
		
			ver_div_enviando();
			enviar_form_presupuesto("Pruebas color");
			
		}else if(tipo_de_envio_PARAM=="Envio"){		
			document.getElementById('txt_entregar_en').disabled=false;
			document.getElementById('form1').submit();
			
		}
	}
	return
 }
 

function validar_soporte(){
	var formu  =  document.form1;	
	puede_ingresar = true;
	
	document.getElementById('txt_nombre').className = 'campos';
	document.getElementById('txt_mail').className = 'campos';
	document.getElementById('txt_observaciones').className = 'campos';
	
	while(formu.txt_nombre.value.charAt(0) == " "){
		formu.txt_nombre.value = formu.txt_nombre.value.substring(1, formu.txt_nombre.value.length);
	 }
	while(formu.txt_mail.value.charAt(0) == " "){
		formu.txt_mail.value = formu.txt_mail.value.substring(1, formu.txt_mail.value.length);
	 }
	while(formu.txt_observaciones.value.charAt(0) == " "){
		formu.txt_observaciones.value = formu.txt_observaciones.value.substring(1, formu.txt_observaciones.value.length);
	 }
	
	if(formu.txt_nombre.value==""){
		document.getElementById('txt_nombre').className = 'campo_error';
		puede_ingresar = false;		
	}
	if(!emailCheck(formu.txt_mail.value)){
		document.getElementById('txt_mail').className = 'campo_error';
		puede_ingresar = false;		
	}
	if(formu.txt_observaciones.value==""){
		document.getElementById('txt_observaciones').className = 'campo_error';
		puede_ingresar = false;		
	}
	
	if(puede_ingresar){
		ver_div_enviando();
		enviar_form_presupuesto('Soporte');
	}
	return
	
	
}


 
function validarTintas(valor){
		
	var dom = document.getElementById;
	var estado;
	estado = false;
	if(valor=="Compuesta"){
		document.form1.tintaC.checked = estado;
		document.form1.tintaY.checked = estado;
		document.form1.tintaM.checked = estado;
		document.form1.tintaB.checked = estado;
		document.form1.tinta_otra.checked = estado;
		document.form1.tintasCMYK.checked = estado;
		document.getElementById('tintaC').disabled = estado;
		document.getElementById('tintaY').disabled = estado;
		document.getElementById('tintaM').disabled = estado;
		document.getElementById('tintaB').disabled = estado;
	}else if(valor=="CMYK"){
		if(document.form1.tintasCMYK.checked == true){
			estado = true;
		}
		document.form1.tintaC.checked = estado;
		document.form1.tintaY.checked = estado;
		document.form1.tintaM.checked = estado;
		document.form1.tintaB.checked = estado;
		document.getElementById('tintaC').disabled = estado;
		document.getElementById('tintaY').disabled = estado;
		document.getElementById('tintaM').disabled = estado;
		document.getElementById('tintaB').disabled = estado;
		document.form1.tinta_compuesta.checked = false;
	}else{
		document.form1.tinta_compuesta.checked = false;
	}
	
	
	if(document.form1.tinta_otra.checked==true){
		
	 	document.getElementById('txt_tinta_otra').style.background = '#f4f4f4';
	 	document.getElementById('txt_tinta_otra').disabled = false;
	}else{
		document.getElementById('txt_tinta_otra').style.background = '#666666';
		document.getElementById('txt_tinta_otra').disabled = true;
		}

	return;
}



function IsNumeric2(sText, campo)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         document.getElementById(campo).value = '';
         }
      }   
   }
   
function mayor20CTF(sText, campo){
	if(sText.length>=2)	{
		if(sText<20){
			document.getElementById(campo).value = '20';
		}
	}
}

function mayor20Proof(sText, campo){
	if(sText.length>=2)	{
		if(sText<20){
			document.getElementById(campo).value = '20';
		}
	}
}

function validar_USUNuevo(){
	enfoco = false;
	puede_ingresar = true ;
	validadcion_datosCliente()
		if(puede_ingresar){
			ver_div_enviando();
			enviar_form_usuario();
		}
		
}







///////////////////////////////////
// JavaScript Document
/////**********MAILS*********//////////////
function emailCheck (emailStr) {
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
var matchArray=emailStr.match(emailPat)

if (matchArray==null) {
   // alert("Email address seems incorrect (check @ and .'s)")
	return false
}

var user=matchArray[1]

var domain=matchArray[2]

if (user.match(userPat)==null) {
         //  alert("The username doesn't seem to be valid.")
      return false
 }
      
var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
       for (var i=1;i<=4;i++) {
         	 if (IPArray[i]>255) {
        	 	//   alert("Destination IP address is invalid!")
      	 		 return false
   			 }
		}
	return true
}


var domainArray=domain.match(domainPat)
if (domainArray==null) {
    	//alert("The domain name doesn't seem to be valid.")
	return false
}
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
//alert("The address must end in a three-letter domain, or two letter country.")
}
if (len<2) {
  var errStr="This address is missing a hostname!"
//  alert(errStr)
  return false
}
return true;
}  	                        	    		        	           
        	   
/////*********FIN * MAILS*********//////////////
///////************FECHAS**************///////////
function IsNumeric(valor) 
{ 
var log=valor.length; var sw="S"; 
for (x=0; x<log; x++) 
{ v1=valor.substr(x,1); 
v2 = parseInt(v1); 
//Compruebo si es un valor numérico 
if (isNaN(v2)) { sw= "N";} 
} 
if (sw=="S") {return true;} else {return false; } 
} 

var primerslap=false; 
var segundoslap=false; 

function formateafecha(fecha) 
{ 
var long = fecha.length; 
var dia; 
var mes; 
var ano; 

if ((long>=2) && (primerslap==false)) { dia=fecha.substr(0,2); 
if ((IsNumeric(dia)==true) && (dia<=31) && (dia!="00")) { fecha=fecha.substr(0,2)+"/"+fecha.substr(3,7); primerslap=true; } 
else { fecha=""; primerslap=false;} 
} 
else 
{ dia=fecha.substr(0,1); 
if (IsNumeric(dia)==false) 
{fecha="";} 
if ((long<=2) && (primerslap=true)) {fecha=fecha.substr(0,1); primerslap=false; } 
} 
if ((long>=5) && (segundoslap==false)) 
{ mes=fecha.substr(3,2); 
if ((IsNumeric(mes)==true) &&(mes<=12) && (mes!="00")) { fecha=fecha.substr(0,5)+"/"+fecha.substr(6,4); segundoslap=true; } 
else { fecha=fecha.substr(0,3);; segundoslap=false;} 
} 
else { if ((long<=5) && (segundoslap=true)) { fecha=fecha.substr(0,4); segundoslap=false; } } 
if (long>=7) 
{ ano=fecha.substr(6,4); 
if (IsNumeric(ano)==false) { fecha=fecha.substr(0,6); } 
else { if (long==10){ if ((ano==0) || (ano<2006) || (ano>2009)) { fecha=fecha.substr(0,6); } } } 
} 

if (long>=10) 
{ 
	fecha=fecha.substr(0,10); 
	dia=fecha.substr(0,2); 
	mes=fecha.substr(3,2); 
	ano=fecha.substr(6,4); 
	// Año no viciesto y es febrero y el dia es mayor a 28 
		if ( (ano%4 != 0) && (mes ==02) && (dia > 28) ) { fecha=fecha.substr(0,2)+"/"; } 
		} 
	return (fecha); 
} 
///////////************FIN FECHAS*************/////////////////
