// JavaScript Document

function login()
{
	if(isEmpty('username'))
		return error('Por favor escriba su Nombre de Usuario','username');
	if(isEmpty('password'))
		return error('Por favor escriba su Contraseña','password');
	return true;
}
function retrieve()
{
	if(isEmpty('email'))
		return error('Por favor escriba el email','email');
	if(!isEmail('email'))
		return error('Por favor escriba un email correcto','email');
	return true;
}
function check()
{
	if(isEmpty('mname'))
		return error('Por favor escriba el Nombre de la Empresa','mname');
	if(isEmpty('street'))
		return error('Por favor escriba el nombre de la calle','street');
		
	if(isEmpty('number'))
		return error('Por favor escriba el número de la calle','number');
	if(isEmpty('area'))
		return error('Por favor escriba la colonia','area');
		
	if(isEmpty('municipality'))
		return error('Por favor escriba el municipio o delegación','municipality');
	if(isEmpty('zip'))
		return error('Por favor escriba el código postal','zip');
	if(!isZip('zip'))
		return error('Por favor escriba un código postal correcto','zip');
	
	if(isEmpty('city'))
		return error('Por favor escriba la ciudad','city');
	if(isEmpty('state'))
		return error('Por favor seleccione un estado','state');
	
	if(isEmpty('between1'))
		return error('Por favor escriba entre que calles se ubica','between1');
	if(isEmpty('between2'))
		return error('Por favor escriba entre que calles se ubica','between2');
	
	if(isEmpty('phone'))
		return error('Por favor escriba el número de teléfono','phone');
	if(!isPhone('phone'))
		return error('Por favor escriba un teléfono correcto','phone');
	if(!isEmpty('fax') && !isPhone('fax'))
		return error('Por favor escriba un número de fax correcto','fax');
	
	if(isEmpty('email'))
		return error('Por favor escriba el email','email');
	if(!isEmail('email'))
		return error('Por favor escriba un email correcto','email');
	
	if(isEmpty('contact'))
		return error('Por favor escriba el Representante del Stand','contact');
	//if(!isEmail('contact'))
		//return error('Por favor escriba un email correcto','contact');
	if(isEmpty('between2'))
		return error('Por favor escriba entre que calles se ubica','between2');
	return true;
}
function checknu()
{
	if(isEmpty('uname'))
		return error('Por favor escriba su(s) nombre(s)','uname');
	if(isEmpty('surname'))
		return error('Por favor escriba sus apellidos','surname');
	if(isEmpty('email'))
		return error('Por favor escriba su email','email');
	if(!isEmail('email'))
		return error('Por favor escriba un email correcto','email');
	if(isEmpty('username'))
		return error('Por favor escriba el username deseado','username');
	if(isEmpty('password'))
		return error('Por favor escriba el password deseado','password');
	return true;
}
function checksearch()
{
	if(isEmpty('keyword'))
		return error('Por favor escriba el o las palabras clave','keyword');
	return true;
}
function isEmpty(fieldName)
{
	var f = document.getElementById(fieldName);
	if(f == null || f.value == null || f.value.length == 0)
		return true;
	return Boolean(/^\s*$/.test(f.value));
}
function error(message, fieldName)
{
	var f = document.getElementById(fieldName);
	f.style.backgroundColor = '#FFFF88';
	f.onkeydown = function(){ f.style.backgroundColor=''; };
	f.onchange  = function(){ f.style.backgroundColor=''; };
	f.focus();
	alert(message);
	return false;
}
function isEmail(fieldName)
{
    var f = document.getElementById(fieldName);
	if(f == null || f.value == null || f.value.length == 0)
		return false;
	//return Boolean(/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/.test(email));
	  return Boolean(/^([a-z]|\d)+(\.?([a-z]|\d)+)?@([a-z]|\d){2,}(\.[a-z]{2,3})(\.[a-z]{2})?$/i.test(f.value));
}
function isZip(fieldName)
{
	var f = document.getElementById(fieldName);
	if(f == null || f.value == null || f.value.length == 0)
		return false;
	return Boolean(/^\d{1,9}$/.test(f.value));
}
function isPhone(fieldName)
{
	var f = document.getElementById(fieldName);
	if(f == null || f.value == null || f.value.length == 0)
		return false;
	return Boolean(/^[\(\)\-\d]{5,}$/.test(f.value));
}
function isImage(str)
{
	return Boolean(/\.(jpg|jpeg|gif)$/i.test(str));
}
///

var cc = 0;
var cm = 5;
function displayFilename(j)
{
	var fd = document.getElementById('filesDisplay');
	var d = document.createElement('div');
	var v = j.value.split('\\');
	d.className = 'fileitem';
	d.innerHTML = v[v.length-1];
	fd.appendChild(d);
	var e = document.createElement('span');
	e.className = 'dropFile';
	e.innerHTML = '&nbsp;Eliminar';
	e.onclick = function(){ drop(j,d); };
	d.appendChild(e);
}
function addFile(fp)
{
	if(!isImage(fp.value))
	{
		alert('Solo se permiten archivos de imágenes .jpg, .jpeg o .gif');
		return;
	}
	if(cc >= cm)
	{
		alert('Solo se permiten 5 imágenes máximo');
		return;
	}
	displayFilename(fp);
	var fh = document.getElementById('fileHolder');
	var i = document.createElement('input');
	i.setAttribute('type','file');
	i.setAttribute('name','myFile[]');
	i.className = 'inputFile';
	i.onclick = function(){ return chkcc(); };
	i.onchange = function(){ addFile(i); };
	i.onkeydown = function(){ return false; };
	fh.appendChild(i);
	cc++;
	if(cc >= cm)
		document.getElementById('addbutton').disabled = true;
}
function drop(f,g)
{
	f.parentNode.removeChild(f);
	g.parentNode.removeChild(g);
	cc--;
	document.getElementById('addbutton').disabled = false;
}
function chkcc()
{
	if(cc >= cm)
		return false;
	return true;
}






