var ie  =  (navigator.appName == "Microsoft Internet Explorer")
var Texto

function trim( str ) { 
	return str.replace(/^\s+|\s+$/, ''); 
};

function items(){

	try{
		if (chkfrm = frm){
			ChkMessages()
			frm.promo.value = trim(frm.promo.value)
		}
	}catch(e){
	
	}
}

function ChkMessages(T, Message){
	
	if (T){ 
		if (T == 'Err'){
			obj = document.getElementById('ErrorMsg')
		}else{
			obj = document.getElementById('SuccessMsg')
		}
		
		if(obj){
			obj.innerHTML = Message
		}
	}	

	if (obj = document.getElementById('ErrorMsg')){
		if(trim(obj.innerHTML) != ''){
			obj.className = obj.id ;
		}
	}
	if (obj = document.getElementById('SuccessMsg')){if(trim(obj.innerHTML) != ''){obj.className = obj.id  ; }}

}


function ValidaRegistro(){
	for (i = 0; i < frm.elements.length -1; i++ ){
		objVr = frm.elements[i]
		
		if (frm.elements[i].className == 'RegField') {if (!  vVacio(objVr)) return false}
		else if (frm.elements[i].className == 'RegField_Tel') {if (!  vMPhone(objVr)) return false}
		else if (frm.elements[i].className == 'RegField_Mail') {if (!  vMEmail(objVr, '')) return false}
		else if (frm.elements[i].className == 'RegField_File') {if (!  vFile(objVr, '')) return false}
		else if (frm.elements[i].className == 'RegField_URL') {if (!  vURL(objVr, '')) return false}

	}
	
	if (! frm.acuerdo.checked){
		ChkMessages('Err','Para pertenecer al programa debe de otorgar el 10% de descuento en alguno de sus productos o servicios' )
		MarcaCampo(document.getElementById('lblacuerdo'),1)
		document.getElementById('lblacuerdo').style.color = "red"
		document.getElementById('lblacuerdo').style.border = "solid red 1px"
		
		return false
	}

	return true
}

function vMEmail(campo, nombre){
	if (! nombre){nombre = noName(campo)}
	
	if (vVacio(campo)){
		if(!vEmail(campo)){ return false}
	}else{ 
		return false
	};
	return true
}
function vMPhone(campo, nombre){
	if (! nombre){nombre = noName(campo)}
	
	if (vVacio(campo)){
		if(!vPhone(campo)){ return false}
	}else{ 
		return false
	};
	return true
}

function vVacio(campo, nombre){
	if (! nombre){nombre = noName(campo)}
	
	if (trim(campo.value) == ''){
		ChkMessages('Err', 'Por favor indique el "' + nombre  + '"')
		campo.focus();
		MarcaCampo(campo,1)
		return false
	}else{
		MarcaCampo(campo)
	}
	return true
}

function vEmail(campo, nombre){
	var valor = true;  
	var patroncorreo = /^.+\@.+\..+$/;
	campo.value = campo.value.replace(" ","").replace("_","z");
	
	if (! nombre){nombre = noName(campo)}
	
	if (!(campo.value.match(patroncorreo))) {
		ChkMessages('Err', 'Por favor utilice un correo electr&oacute;nico v&aacute;lido  ' )
		campo.focus();
		valor = false;
		MarcaCampo(campo,1)
	}else{
		MarcaCampo(campo)
	}
	
	return valor;
}	 

function vPhone(campo, nombre){
  var valor = true;  
  var patron = /^((((_*)(\d{2})(_*)(\s*)){0,1})(\d{3})){0,1}(-*)(\d{3})(-*)(\d{4})$/;
  
  campo.value = campo.value.replace(" ","");
  var val = campo.value.replace("(","_").replace(")","_")
	val = val.replace(/([^0-9])/gi,"")
	
	tmpval = ''
	switch(true){
		case val.length == 12:
			tmpval += '(' + val.substr(0,2) + ') ' 
			val = val.substr(2,10)
		case val.length == 10:
			tmpval += val.substr(0,3) + '-' 
			val = val.substr(3,7)
		case val.length == 7:
			tmpval += val.substr(0,3) + '-' + val.substr(3,4) 	
		break						
	}

  var val = tmpval.replace("(","_").replace(")","_")

  if (! nombre){nombre = noName(campo)}
  
    if (!(val.match(patron))) {
        ChkMessages('Err', 'Por favor utilice solamente los formatos permitidos. Ejem.- 998-881-0000 &oacute; (52) 998-881-0000 ' )
        campo.focus();
        valor = false;
		MarcaCampo(campo,1)
	}else{


		campo.value = tmpval
		MarcaCampo(campo)
	}
  return valor;
}

function vFile(campo, nombre){
	var valor = true;  

	if (! nombre){nombre = noName(campo)}

		ext = campo.value
		ext = ext.substring(ext.length -3 ,ext.length);
		ext = ext.toLowerCase();
		
		if(! in_Array(ext, ("jpg;gif").split(";"))){
			ChkMessages('Err',  'Por favor utilice solamente los formatos permitidos (*.gif &oacute; *.jpg) con un tama&ntilde;o maximo de 2 Mb. ' )
			campo.focus()
			MarcaCampo(campo,1)
			valor = false
		}

	return valor
}

function vURL(campo, nombre){
	var valor = true;  
	
	if (trim(campo.value) != ''){
		var patron = /^[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)( [a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$/;
			
		if (! nombre){nombre = noName(campo)}
		
		campo.value = campo.value.replace('http://','')
		
		if (!(campo.value.match(patron))) {
			ChkMessages('Err', 'Por favor utilice un formato de ruta valido (www.su_sitio.com) ' )
			campo.focus();
			valor = false;
			MarcaCampo(campo,1)
		}else{
			MarcaCampo(campo)
		}
	}


	return valor;
}


function MarcaCampo(campo, On){
	
	if (On == 1){
		campo.style.backgroundColor = "#FEF9D8";
	}else{
		campo.style.backgroundColor = "";
	}
	
}

function noName(campo){

	elm = campo.parentNode.cloneNode(true)
	
	
	Rtrn = (ie)? elm.innerText:elm.textContent;
	
	return trim(Rtrn.replace('*',''))
}


function playsound(soundfile){
	hItems = document.getElementById("hiddenItems")
	try {
		if (soundfile){
			soundfile = "include/alert.wav"

			if (_soundeffect = document.getElementById("soundeffect")){
				_soundeffect.src="" 
				_soundeffect.src=soundfile

			}
		}else{
			//obj = document.getElementById("hiddenItems")//document.documentElement.getElementsByTagName("head")[0]
			
			//hItems.innerHTML = '<embed mastersound  hidden="true" id="soundeffect" loop="1" volume="70" src="" type="application/x-mplayer2"></embed>'
			
			/*bgsound = Head.appendChild(document.createElement("bgsound"))
			bgsound.setAttribute("src","")
			bgsound.setAttribute("id","soundeffect")
			bgsound.setAttribute("loop","1")
			bgsound.setAttribute("autostart","true")	*/
		}
		
	}catch(e){
		alert(e.message)			
	}
}

function in_Array(val, arr){
	for(xVal in arr){
		if (val == arr[xVal]){ return true;}
	}
	return false;
}

function txtAreaMax(obj){
	var NotKeys = new Array (8,9,16,32,36,37,38,39,40,46);

	if (! in_Array(event.keyCode, NotKeys)){
		obj.value = obj.value.substr(0,2000)
	}
}

function chgLang(lng, btn){
	frmid = "frmhdn"
	URL = document.location
	lng = (lng == 'EN')? 'ES':'EN';
	
	hItems = document.getElementById("hiddenItems");
		

	Doc = newDoc("<form/>")
	root = Doc.documentElement
		root.setAttribute("id", frmid)
		root.setAttribute("name", frmid)
		root.setAttribute("action",URL)
		root.setAttribute("method","post")
			
		input = root.appendChild(Doc.createElement("input"))
			input.setAttribute("name","idioma")
			input.setAttribute("value",lng)

		
	hItems.innerHTML = root.xml


	document.getElementById(frmid).submit()	
	
}

function navAct(num){

	switch(num){
		case 5:
			num = 1
			break;
		case 0:
			num = 4
			break;
		default : 
			if (!num) num = 1
			break;
	}
	

	obj = document.getElementById("navnum"); 
	obj.value = num
	
	img = document.getElementById("imgTicket");
	img.src = "images/BILLETE_" + num + ".jpg"
}

function navjs(Ticket){
	textos()
	switch(Ticket){
		case 5:
			Ticket = 1
			break;
		case 0:
			Ticket = 4
			break;
		default : 
			if (!Ticket)Ticket = 1
			break;
	}


	nav = document.getElementById("navBar"); 
	Bill = document.getElementById("bill"); 
	
	Doc = newDoc("<div/>")
	root = Doc.documentElement

	a = root.appendChild(Doc.createElement("a"))
		a.setAttribute("href","javascript:navAct(parseInt(document.getElementById('navnum').value) - 1)")
		a.appendChild(Doc.createTextNode("<"))


	select =  Doc.documentElement

	select = root.appendChild(Doc.createElement("select"))
		select.setAttribute("id","navnum")
		select.setAttribute("name","navnum")
		select.setAttribute("onChange","navjs(this.value)")
		for (i = 1; i <= 4; i++){
			option = select.appendChild(Doc.createElement("option"))
			option.setAttribute("value",i)
			option.appendChild(Doc.createTextNode(i))
		}
	a = root.appendChild(Doc.createElement("a"))
		a.setAttribute("href","javascript:navAct(parseInt(document.getElementById('navnum').value) + 1)")
		a.appendChild(Doc.createTextNode(">"))
		
	btn = root.appendChild(Doc.createElement("input"))
		btn.setAttribute("name","print")
		btn.setAttribute("value", Texto['imprimir'])
		btn.setAttribute("type","button")
		btn.setAttribute("onClick","Imprimir('images/BILLETE_" + Ticket + ".jpg')")
		
	img = Doc.createElement("img")
		img.setAttribute("id","imgTicket")
		img.setAttribute("width","900")
		img.setAttribute("src","images/BILLETE_" + Ticket + ".jpg")

	if (ie){
		nav.innerHTML = root.xml
		Bill.innerHTML = img.xml
	}else{
		nav.innerHTML = XMLstring(root,'')	
		Bill.innerHTML = XMLstring(img,'')
	}

	document.getElementById("navnum").value = Ticket

}

function Imprimir(imagen) {
   newWindow = window.open("","Imagenes","width=900,height=379,left=100,top=60");
   newWindow.document.open();
   newWindow.document.write('<html><head><style>body, html{margin:0px;}</style></head><body onload="window.print()"><img width=900 src="'+ imagen +'"/></body></html>');  
   newWindow.document.close();
   newWindow.focus();
}


function textos(){

	if (!(Texto)){
	
		var ruta =  window.location.href.split("#")[0] + '?cmd=xml:js'	
	
		var data = xmlDoc(ruta)	

		Texto = new Array()
		Root = data.documentElement
			
		for (iRow = 0; iRow <= Root.childNodes.length -1; iRow++ ){
			
			if (ie){
				val = Root.childNodes[iRow].text 
			}else{
				val = Root.childNodes[iRow].textContent 
			}
			Texto[Root.childNodes[iRow].nodeName] = val		
		}
	}
}
