var clearJobDesc = false;
function clearDemoJob() {
	if (!clearJobDesc) $('#mensaje').val('');
	clearJobDesc = true;
}

function validateRegister(form) {
	var pass1 = form.pass1.value;
	var pass2 = form.pass2.value;
	var email = form.email.value;
	var name = form.name.value;
	var phone = form.phone.value;
	var address = form.address.value;
	var country = form.country.value;
	var mensaje = form.mensaje.value;
	var acceptprivacy = form.acceptprivacy;
	var acceptfreetrial = form.acceptfreetrial;
    var imagen = form.img1.value;
    var gender = form.gender.value;
    var birthday = form.birthday.value;
    var birthmonth = form.birthmonth.value;
    var birthyear = form.birthyear.value;

	var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
	
	if(!email.match(emailRegex)) {
		inlineMsg('email','Ingrese un email v&aacute;lido',2);
		return false;
	}
	if(pass1 == "" || pass1.length < 6) {
		inlineMsg('pass1','Ingrese una contrase&ntilde;a de al menos 6 caracteres',2);
		return false;
	}
	if(pass1 != pass2) {
		inlineMsg('pass2','La contrase&ntilde;a y su verificacion no coinciden',2);
		return false;
	}
	
	if(name == "" || name.length < 3) {
		inlineMsg('name','Ingrese un nombre',2);
		return false;
	}
    
    if(birthday == '31' && birthmonth == '12' && birthyear == '2010') {
		inlineMsg('birthyear','Debe seleccionar su fecha de nacimiento',2);
		return false;
	}
    
    if(gender == "") {
		inlineMsg('gender1','Debe seleccionar su género',2);
		return false;
	}
    
    
	if(country == "") {
		inlineMsg('country','Seleccione el pais',2);
		return false;
	}
    
    if(imagen.length<2) {
		inlineMsg('img1','Elija una imágen para comenzar con su trabajo de ejemplo',2);
		return false;
	}
    
	if (clearJobDesc == false) {
		inlineMsg('mensaje','Ingrese una descripcion',2);
		return false;	
	}
	if(mensaje.length<6) {
		inlineMsg('mensaje','Ingrese una descripcion',2);
		return false;
	}
    
    
    
	/*
	if(phone == "" || phone.length < 4) {
		inlineMsg('phone','Ingrese un tel&eacute;fono',2);
		return false;
	}

	if(address == "" || address.length < 4) {
		inlineMsg('address','Ingrese una direcci&oacute;n',2);
		return false;
	}
	*/
	
	if(!acceptprivacy.checked) {
		inlineMsg('acceptprivacy','Debe aceptar las condiciones de uso y privacidad',2);
		return false;
	}
	
	if(!acceptfreetrial.checked) {
		inlineMsg('acceptfreetrial','Debe aceptar las condiciones de la prueba gratis',2);
		return false;
	}
	
	return true;
}


function validateRegisterWork(form) {
	var pass1 = form.pass1.value;
	var pass2 = form.pass2.value;
	var email = form.email.value;
	var name = form.name.value;
	var phone = form.phone.value;
	var address = form.address.value;
	var country = form.country.value;
	var mensaje = form.mensaje.value;
	var acceptprivacy = form.acceptprivacy;
	var acceptfreetrial = form.acceptfreetrial;
	var cv = form.cv.value;
	var demo1 = form.demo1.value;

	var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
	
	
	if(!email.match(emailRegex)) {
		inlineMsg('email','Ingrese un email v&aacute;lido',2);
		return false;
	}
	if(pass1 == "" || pass1.length < 6) {
		inlineMsg('pass1','Ingrese una contrase&ntilde;a de al menos 6 caracteres',2);
		return false;
	}
	if(pass1 != pass2) {
		inlineMsg('pass2','La contrase&ntilde;a y su verificacion no coinciden',2);
		return false;
	}
	
	if(name == "" || name.length < 3) {
		inlineMsg('name','Ingrese un nombre',2);
		return false;
	}
	if(country == "") {
		inlineMsg('country','Seleccione el pais',2);
		return false;
	}
	if (clearJobDesc == false) {
		inlineMsg('mensaje','Ingrese una descripcion',2);
		return false;	
	}
	if(mensaje.length<6) {
		inlineMsg('mensaje','Ingrese una descripcion',2);
		return false;
	}
	
	
	
	if(cv == "") {
		inlineMsg('cv','Agregue el CV',2);
		return false;
	}
	
	if(demo1 == "") {
		inlineMsg('demo1','Agregue al menos 1 ejemplo',2);
		return false;
	}
	
	if(!acceptprivacy.checked) {
		inlineMsg('acceptprivacy','Debe aceptar las condiciones de uso y privacidad',2);
		return false;
	}
	
	if(!acceptfreetrial.checked) {
		inlineMsg('acceptfreetrial','Debe aceptar las condiciones de la prueba gratis',2);
		return false;
	}
	
	return true;
}



function validatePerfil(form) {
	var pass1 = form.pass1.value;
	var pass2 = form.pass2.value;
	var email = form.email.value;

	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
	
	if(!email.match(emailRegex)) {
		inlineMsg('email','Ingrese un email v&aacute;lido',2);
		return false;
	}
	if (pass1 != "") {
		if(pass1 == "" || pass1.length < 6) {
			inlineMsg('pass1','Ingrese una contrase&ntilde;a de al menos 6 caracteres',2);
			return false;
		}
		if(pass1 != pass2) {
			inlineMsg('pass2','La contrase&ntilde;a y su verificacion no coinciden',2);
			return false;
		}
	}
	return true;
}

function validateContacto(form) {
	var nombre = form.nombre.value;
	var email = form.email.value;
	var mensaje = form.mensaje.value;

	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	
	if(nombre == "" || nombre.length < 4) {
		inlineMsg('nombre','Ingrese un nombre',2);
		return false;
	}
	if(!email.match(emailRegex)) {
		inlineMsg('email','Ingrese un email v&aacute;lido',2);
		return false;
	}
	if(mensaje == "" || mensaje.length < 6) {
		inlineMsg('mensaje','Ingrese un mensaje',2);
		return false;
	}
	
	return true;
}

function validateSearch(form) {
	var query = form.query.value;
	
	if(query == "" || query.length < 3) {
		inlineMsg('query','Ingrese una b&uacute;squeda de al menos 3 caracteres',2);
		return false;
	}
	
	return true;
}


function validateConfirm(form) {
	var title = form.title.value;
	var descr = form.descr.value;
	
	if(title == "" || title.length < 3) {
		inlineMsg('title','Ingrese un titulo',2);
		return false;
	}
	if(descr == "" || descr.length < 3) {
		inlineMsg('titledescr','Ingrese comentarios que describan lo que desea obtener como resultado',2);
		return false;
	}
	
	return true;
}

function validateLogin(form) {
	var email = form.email.value;
	var password = form.password.value;
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	
	if(email.length < 4) {
		inlineMsg('email','Ingresa tu nombre de usuario (email)',2);
		return false;
	}
	if(password == "" || password.length < 4) {
		inlineMsg('password','Ingrese la contrase&ntilde;a',2);
		return false;
	}
	
	return true;
}

function checkapplyservicesfiles() {
	/* Check services */
	if (!$('#camera-basic').attr('checked') && !$('#camera-pro').attr('checked') && !$('#video-basic').attr('checked') && !$('#video-pro').attr('checked') && !$('#illustration-basic').attr('checked') && !$('#illustration-pro').attr('checked')) {
		alert('Selecciona al menos un servicio.');
		return false;
	}
	
	/* Check files */
	var allids = $('#filesids').val();
	var arrids = allids.split(',');
	
	var flag = false;
	$.each(arrids, function(index, value) { 
		if ($('#file'+value).attr('checked')) flag = true;
	});
	
	if (!flag) {
		alert('Marque al menos un archivo para aplicar el servicio.');
		return false;	
	}
	
	return true;
}

function checkdeletefile() {
	return confirm('Esta seguro de eliminar los archivos seleccionados?');
}

function checkdeletejob() {
	return confirm('Esta seguro de cancelar el trabajo?');
}

function checkacceptprop() {
	return confirm('Esta seguro de aceptar la propuesta?');
}
function checkrechazarprop() {
	return confirm('Esta seguro de rechazar la propuesta?');
}

function validateRemember(form) {
	var email = form.email.value;
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	
	if(!email.match(emailRegex)) {
		inlineMsg('email','Ingrese un email v&aacute;lido',2);
		return false;
	}
	
	return true;
}

function validateLogin2(form) {
	var username2 = form.username.value;
	var password2 = form.password.value;
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	
	if(username2.length < 4) {
		inlineMsg('username','Ingresa tu nombre de usuario (email)',2);
		return false;
	}
	if(password2 == "" || password2.length < 4) {
		inlineMsg('password','Ingrese la contrase&ntilde;a',2);
		return false;
	}
	
	return true;
}

function validateArchivo(form) {
	var descr = form.descr.value;
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	
	if(descr.length < 3) {
		inlineMsg('descr','Ingresa un nombre de trabajo o pedido para el archivo',2);
		return false;
	}
	
	return true;
}

function showprogressbar() {
	$('#progressdiv').fadeIn();
}

function showStartNowHome() {
	$('#startnowhome').fadeIn();
}

function hideStartNowHome() {
	$('#startnowhome').fadeOut();
}

function showTour() {
	$('#takeatour').fadeIn();
}

function hideTour() {
	$('#takeatour').fadeOut();
}

function submitSearch() {
	var query = document.forms["searchform"].query.value;
	
	if(query == "" || query.length < 3) {
		inlineMsg('query','Ingrese una b&uacute;squeda de al menos 3 caracteres',2);
	} else {
		document.forms['searchform'].submit();  	
	}
}

function updateEmailForm() {
	if ($('#username').val() == 'email...') {
		$('#username').val('');
	}
}
function updateEmailForm2() {
	if ($('#username').val() == '') {
		$('#username').val('email...');
	}
}

function showcontactinfo(toshow, title) {
	$('#contactdata').hide();
	$('#contactdistribuitors').hide();
	$('#'+toshow).show();
	$('#contacttitle').text(title);
	$('#contactype').val(title);
}

function showsendfiles() {
	$('#fileuploader').fadeIn();
}

var lastselected = "1";
function updatepriceshome(service) {
	$('#servicelink'+lastselected).css("background","url("+base_url+"system/application/images/bg_prices.gif) 2px 3px no-repeat");
	$('#servicelink'+service).css("background","url("+base_url+"system/application/images/bg_prices2.gif) 2px 3px no-repeat");
	
	$('#serviceblock'+lastselected).hide();
	$('#serviceblock'+service).show();
	lastselected = service;
}

var lasthome = "1";
function changepriceshome(iddiv) {
	$('#priceshome'+lasthome).hide();
	$('#priceshome'+iddiv).show();
	
	$('#rangelink'+lasthome).css("background","url("+base_url+"system/application/images/bg_prices.gif) 2px 3px no-repeat");
	$('#rangelink'+iddiv).css("background","url("+base_url+"system/application/images/bg_prices2.gif) 2px 3px no-repeat");
	
	lasthome = iddiv;
}


var counter = 1;
function addSlot() {
	counter++;
	html = '<p><label for="mensaje" class="left">Archivo '+counter+':</label> <input type="file" name="file'+counter+'" id="file'+counter+'" /></p>';
	$('#uploaddiv').append(html);
	$('#filesquantity').val(counter);
}

var rowsids = Array();
function checkrow(id) {
	if (!rowsids[id]) {
		rowsids[id] = true;
		$('#file'+id).attr('checked', true);
	} else {
		rowsids[id] = false;
		$('#file'+id).attr('checked', false);
	}
		
}

var counterservices = 0;
function checkfileservice(divcontainer, bgimg, toset, tounset) {
	if ($('#'+tounset).attr('checked')) {
		$('#'+tounset).attr('checked', false);
	}
	if ($('#'+toset).attr('checked') || $('#'+tounset).attr('checked')) {
		$('#'+divcontainer).css("background","url("+base_url+"system/application/images/"+bgimg+"2.gif) no-repeat top left");
	} else {
		$('#'+divcontainer).css("background","url("+base_url+"system/application/images/"+bgimg+".gif) no-repeat top left");
	}
}

var imgbase = base_url+"system/application/images/";
jQuery.preloadImages = function() {
  for(var i = 0; i<arguments.length; i++) {
    jQuery("<img>").attr("src", arguments[i]);
  }
}
$.preloadImages(imgbase+"bg_prices2.gif",imgbase+"bg_tablahover.gif",imgbase+"bg_tablahover2.gif",imgbase+"icon_down2.gif",imgbase+"icon-service-illustration2.gif",imgbase+"icon-service-photo2.gif",imgbase+"icon-service-video2.gif",imgbase+"delete2_en.gif",imgbase+"delete2_es");
