var _cii29jq = jQuery.noConflict();
var _cii29timeout = null;
var _cii29widthRegister = 0;
var _cii29heightRegister = 0;
var _cii29widthLogin = 0;
var _cii29heightLogin = 0;
var _cii29callBack = null;
var _cii29processOk = false;

function _cii29checkUserName(focus){
	_cii29checkField('username_cii29', 'Email', focus);
}
function _cii29checkPassword(focus){
	_cii29checkField('password_cii29', 'Contraseña', focus, true);
}
function _cii29checkName(focus){
	_cii29checkField('name_cii29', 'Nombre', focus);
}
function _cii29checkSurname(focus){
	_cii29checkField('surname_cii29', 'Apellido', focus);
}
function _cii29checkEmail(focus){
	_cii29checkField('email_cii29', 'Email (será tu nombre de usuario)', focus);
}
function _cii29checkPhone(focus){
	_cii29checkField('phone_cii29', 'Teléfono de contacto', focus);
}
function _cii29checkRegisterPassword(focus){
	_cii29checkField('password_register_cii29', 'Escoge una contraseña', focus, true);
}
function _cii29checkRewritePassword(focus){
	_cii29checkField('rewrite_password_cii29', 'Vuelve a escribir la contraseña', focus, true);
}
function _cii29checkField(id, text, focus, password){
	var value, type, node, blurFunction, focusFunction;
	
	if($(id)){
		type = focus ? 'password' : 'text';
		
		value = $(id).value;
		if(value == text && focus)
			value = "";
		else if(value == "" && !focus)
			value = text;
		else if(value != "" && value != text && !focus)
			type = 'password';

		if(password){
			try {
				$(id).type = type;
			} catch(e){
				if($('cont_'+id)){
					blurFunction = $(id).onblur;
					focusFunction = $(id).onfocus;
					
					node = document.createElement('input');
					node.setAttribute('type', type);
					node.setAttribute('name', id);
					node.setAttribute('id', id);
					node.className = 'input_text';
					$(id).onblur = null;
					$(id).onfocus = null;
					$('cont_'+id).removeChild($(id));
					$('cont_'+id).appendChild(node);
					node.setAttribute('onblur', blurFunction);
					node.setAttribute('onfocus', focusFunction);
					if(focus)
						setTimeout("$('"+id+"').focus()", 100);
				}
			}
		}
		$(id).value = value;
	}
}
function _cci143showLogin(linkRegister){
	_cii29cleanFieldsLoginForm(linkRegister);
	_cii29showForm('login_cii29', _cii29widthLogin, _cii29heightLogin);
}
function _cci143showRegister(){
	_cii29cleanFieldsRegisterForm();
	_cii29showForm('register_cii29', _cii29widthRegister, _cii29heightRegister);
}
function _cii29showForm(id, width, height){
	if($(id) && $('bg_info_cii29') && width > 0 && height > 0){
		height = (_cii29jq(window).height()-height)/2;
		width = (_cii29jq(window).width()-width)/2;
		
		_cii29jq('#'+id).css('top',  height);
		_cii29jq('#'+id).css('left', width);
		if($('bg_info_cii29').style.display == "none")
			_cii29jq('#bg_info_cii29').fadeIn(250);
		_cii29jq('#'+id).fadeIn(350);
	}
}
function _cii29showDisclaimer(open){
	if(open)
		_cii29showForm('window_disclaimer_cii29', _cii29jq('#window_disclaimer_cii29').width(), _cii29jq('#window_disclaimer_cii29').height());
	else
		_cii29changeFormRegister('disclaimer_cii29');
}
function _cii29closeDisclaimer(){
	_cii29hideForm('window_disclaimer_cii29', true);
}
function _cii29showTermsOfUse(open){
	if(open)
		_cii29showForm('window_terms_of_use_cii29', _cii29jq('#window_terms_of_use_cii29').width(), _cii29jq('#window_terms_of_use_cii29').height());
	else
		_cii29changeFormRegister('terms_of_use_cii29');
}
function _cii29closeTermsOfUse(){
	_cii29hideForm('window_terms_of_use_cii29', true);
}
function _cii29returnToRegisterForm(){
	var id = "disclaimer_cii29";
	if($('terms_of_use_cii29') && $('terms_of_use_cii29').style.display != "none")
		id = "terms_of_use_cii29";
	_cii29changeFormRegister(id, true);
}
function _cii29changeFormRegister(id, showForm){
	if($(id) && $('form_register_cii29')){
		if(showForm){
			_cii29jq('#'+id).fadeOut("fast", function(){
				_cii29jq('#form_register_cii29').fadeIn("fast");
			});
		} else {
			_cii29jq('#form_register_cii29').fadeOut("fast", function(){
				_cii29jq('#'+id).fadeIn("fast");
			});
		}
	}
}
function _cii29showRemember(){
	_cii29changeFormLogin('submit_remember_cii29', 'Recordar datos de acceso', 'Enviar');
}
function _cii29showLoginFields(){
	_cii29changeFormLogin('submit_login_cii29', 'Acceder a mi cuenta de usuario', 'Acceder');
}
function _cii29changeFormLogin(submitName, text, titleButton){
	var linkRegistered = false;
	if($('cont_register_link_cii29'))
		linkRegistered = ($('cont_register_link_cii29').style.display != "none");
	_cii29cleanFieldsLoginForm(linkRegistered);
	if($('cont_title_cii29')){
		$('cont_title_cii29').innerHTML = text;
		_cii29jq('#cont_title_cii29').fadeIn(250);
	}
	if($('cont_password_cii29') && $('cont_login_link_cii29') && $('cont_remember_link_cii29')){
		if($('cont_remember_logged_cii29'))
			_cii29jq('#cont_remember_link_cii29, #cont_login_link_cii29, #cont_password_cii29, #cont_remember_logged_cii29').toggle(250);
		else
			_cii29jq('#cont_remember_link_cii29, #cont_login_link_cii29, #cont_password_cii29').toggle(250);
	}
	if($('submit_login_cii29')){
		$('submit_login_cii29').name = submitName;
		$('submit_login_cii29').value = titleButton;
	}
}
function _cii29closeLoginForm(){
	_cii29hideForm('login_cii29', true);
}
function _cii29closeRegisterForm(){
	_cii29hideForm('register_cii29', true);
}
function _cii29hideForm(id, executeCallBack){
	if($(id) && $('bg_info_cii29'))
		_cii29jq('#bg_info_cii29, #'+id).fadeOut(250, function(){
		if(executeCallBack && typeof(_cii29callBack) == "function"){
			_cii29callBack(_cii29processOk);
			_cii29callBack = null;
		}
	});
}
function _cii29cleanFieldsLoginForm(linkRegister){
	if ($("username_cii29"))
		$("username_cii29").style.color = "";

	if ($("password_cii29"))
		$("password_cii29").style.color = "";

	$('message_login_cii29').style.display = "none";
	$('cont_login_form_cii29').style.display = "block";
	if($('cont_register_link_cii29'))
		$('cont_register_link_cii29').style.display = linkRegister ? "block" : "none";

}
function _cii29checkLoginForm(){
	var ok = true;
	var error = "";
	var regExp = /^[\w\-\.]+@[\w\.\-]+\.[a-z]{2,4}$/i;

	_cii29checkUserName(true);
	if (!regExp.test($("username_cii29").value)) {
		ok = false;
		error += " email";
		$("username_cii29").style.color = "#FF0000";
		_cii29checkUserName();
	} else
		$("username_cii29").style.color = "";

	_cii29checkPassword(true);
	if ($("password_cii29").value == "" && $('cont_password_cii29').style.display != "none") {
		ok = false;
		if(error != "")
			error += ',';
		error+= " contraseña";
		$("password_cii29").style.color = "#FF0000";
		_cii29checkPassword();
	} else
		$("password_cii29").style.color = "";
	
	if (error != ""){
		$('message_login_cii29').innerHTML = '<div class="form_ko">Vaya, parece que hay un error en los siguientes campos:'+error+'.</div>';
		$('message_login_cii29').style.display = "";
	} else
		$('message_login_cii29').style.display = "none";

	if(ok)
		_cii29sendLoginForm();
	return false;
}
function _cii29cleanFieldsRegisterForm(){
	if ($("name_cii29"))
		$("name_cii29").style.color = "";
	/*
	if($("surname_cii29"))
		$("surname_cii29").style.color = "";*/
	if($("email_cii29"))
		$("email_cii29").style.color = "";
	if ($("type_cii29"))
		$("type_cii29").style.color = "";
	if ($("conditions_cii29"))
		$("label_conditions_cii29").style.color = "";

	if ($("password_register_cii29"))
		$("password_register_cii29").style.color = "";
	if($("rewrite_password_cii29"))
		$("rewrite_password_cii29").style.color = "";
	
	$('message_register_cii29').style.display = "none";
	$('cont_register_form_cii29').style.display = "block";
}
function _cii29checkRegisterForm(){
	var ok = true;
	var error = "";
	var regExp = /^[\w\-\.]+@[\w\.\-]+\.[a-z]{2,4}$/i;

	_cii29checkName(true);
	if ($("name_cii29").value == "") {
		ok = false;
		error += " nombre";
		$("name_cii29").style.color = "#FF0000";
		_cii29checkName();
	} else
		$("name_cii29").style.color = "";
	
	/*
	_cii29checkSurname(true);
	if ($("surname_cii29").value == "") {
		ok = false;
		if(error != "")
			error += ',';
		error += " apellido";
		$("surname_cii29").style.color = "#FF0000";
		_cii29checkSurname();
	} else
		$("surname_cii29").style.color = "";*/

	_cii29checkEmail(true);
	if (!regExp.test($("email_cii29").value)) {
		ok = false;
		if(error != "")
			error += ',';
		error += " email/nombre de usuario";
		$("email_cii29").style.color = "#FF0000";
		_cii29checkEmail();
	} else
		$("email_cii29").style.color = "";
	
	_cii29checkPhone(true);
	if ($("phone_cii29").value != "" && $("phone_cii29").value.length < 9) {
		ok = false;
		if(error != "")
			error += ',';
		error += " teléfono de contacto";
		$("phone_cii29").style.color = "#FF0000";
	} else if($("phone_cii29").value != "")
		$("phone_cii29").style.color = "";
	_cii29checkPhone();
	
	if ($("type_cii29").value <= 0) {
		ok = false;
		if(error != "")
			error += ',';
		error += " tipo de usuario";
		$("type_cii29").style.color = "#FF0000";
	} else
		$("type_cii29").style.color = "";

	_cii29checkRegisterPassword(true);
	_cii29checkRewritePassword(true);
	if ($("password_register_cii29").value == "" || $("password_register_cii29").value.length < 5 || $("password_register_cii29").value != $("rewrite_password_cii29").value) {
		ok = false;
		if(error != "")
			error += ',';
		error += " contraseña (debe contener mínimo 5 caracteres)";
		$("password_register_cii29").style.color = "#FF0000";
		$("rewrite_password_cii29").style.color = "#FF0000";
		_cii29checkRegisterPassword();
		_cii29checkRewritePassword();
	} else {
		$("password_register_cii29").style.color = "";
		$("rewrite_password_cii29").style.color = "";
	}
	
	if (!$("conditions_cii29").checked) {
		ok = false;
		if(error != "")
			error += ',';
		error += " condiciones";
		$("label_conditions_cii29").style.color = "#FF0000";
	} else
		$("label_conditions_cii29").style.color = "";
	
	if (error != ""){
		$('message_register_cii29').innerHTML = '<div class="form_ko">Vaya, parece que hay un error en los siguientes campos:'+error+'.</div>';
		$('message_register_cii29').style.display = "";
	} else
		$('message_register_cii29').style.display = "none";
	
	if(ok){
		_cii29checkPhone(true);
		_cii29sendRegisterForm();
	}
	return false;
}
function _cii29sendLoginForm(){
	var linkRegistered = false;
	var refresh = false;
	
	if($('cont_register_link_cii29'))
		linkRegistered = ($('cont_register_link_cii29').style.display != "none");
	if($('submit_login_cii29'))
		refresh = $('submit_login_cii29').name == 'submit_login_cii29';
	_cii29cleanFieldsLoginForm(linkRegistered);
	
	_cii29sendForm('login_cii29', 'form_login_cii29', 'message_login_cii29', 'cont_login_form_cii29', refresh);
}

function _cii29refreshShoppingCart(callBack){
	var ajax = new Ajax.Request('http://www.deskidea.com/es/cesta.php', {
		onComplete: function(){
			updateShoppingCartInfo();
			if(typeof(callBack) == "function")
				callBack();
		}
	});
}

function _cii29refreshData(callBack){
	_cii29refreshShoppingCart(callBack);	var ajax = new Ajax.Updater('header_login', 'http://www.deskidea.com/es/families/0001/recambios_____.php', {
		parameters: {action_get_right_header_cci143: '1'},
		onComplete: function(){
			_cii29initSubmenus();
					}
	});
}
function _cii29sendRegisterForm(){
	_cii29cleanFieldsRegisterForm();
	_cii29sendForm('register_cii29', 'form_register_cii29', 'message_register_cii29', 'cont_register_form_cii29');
}
function _cii29sendForm(windowId, formId, messageId, contentId, refresh){
	if($(formId) && $(windowId)){
		_cii29jq('#'+windowId).fadeIn("fast");
		$(formId).request({
			onComplete: function(response){
				var content = "";

				_cii29processOk = false;
				if(response.status == 200){
					content = '<div class="form_ok">'+response.responseText+'</div>';
					_cii29processOk = true;
					document.fire('aj:completed');
					_cii29refreshData(function(){
						if(refresh){
							if(typeof(_cii29callBack) == "function")
								_cii29hideForm(windowId, true);
							else
								window.location.reload();
						} else {
							content = '<div class="form_ok">'+response.responseText+'</div>';
							if($(contentId))
								$(contentId).style.display = "none";
						}
					});
				} else
					content = '<div class="form_ko">'+response.responseText+'</div>';

				if(!refresh || response.status != 200){
					if($(messageId) && content != ""){
						$(messageId).innerHTML = content;
						$(messageId).style.display = "";
					}
					_cii29showForm(windowId, _cii29jq('#'+windowId).width(), _cii29jq('#'+windowId).height());
				}
			}
		});
	}
}
function _cii29showMenu(id){
	if(_cii29timeout != null)
		clearTimeout(_cii29timeout);
	if($('_cii29submenu'+id))
		_cii29jq('#_cii29submenu'+id+' > ul').slideDown("fast");
}
function _cii29hideMenu(id){
	if(_cii29timeout != null)
		clearTimeout(_cii29timeout);
	if($('_cii29submenu'+id))
		_cii29jq('#_cii29submenu'+id+' > ul').slideUp("fast");
}
function _cii29initSubmenus(){
		
	_cii29jq('#_cii29submenu0').hover(
		function(){
			if(_cii29timeout != null)
				clearTimeout(_cii29timeout);
			_cii29timeout = setTimeout('_cii29showMenu(0);', 200);
		},
		function(){_cii29hideMenu(0);}
	);

	}
function _cii29init(){
	_cii29initSubmenus();
	_cii29widthRegister = _cii29jq('#register_cii29').width();
	_cii29heightRegister = _cii29jq('#register_cii29').height();
	_cii29widthLogin = _cii29jq('#login_cii29').width();
	_cii29heightLogin = _cii29jq('#login_cii29').height();
	}
function _cii29showFormsToAccess(id){
	var ajax;

	if($(id)){
		ajax = new Ajax.Updater(id, 'http://www.deskidea.com/es/families/0001/recambios_____.php', {
			parameters: 'check_access_ajax_cii29=1',
			onComplete: function(){
				var width, height;

				width = _cii29jq('#select_form_cii29').width();
				height = _cii29jq('#select_form_cii29').height();
				_cii29showForm('select_form_cii29', width, height);
			}
		});
	}
}
function checkAccess(callBack, id, purchaseProcess, strict){
	var ajax;

	if((id && $(id)) || !purchaseProcess){
		if(!callBack)
			callBack = null;
		
		_cii29callBack = callBack;
		if(typeof(_cii29callBack) == "function"){
			ajax = new Ajax.Request('http://www.deskidea.com/es/families/0001/recambios_____.php', {
				parameters: 'check_login_cii29=1',
				onComplete: function(response){
					var ajax;
					
					if(response.status == 200)
						callBack(true);
					else if(strict)
						callBack(false);
					else {
						if(purchaseProcess)
							_cii29showFormsToAccess(id);
						else
							_cci143showLogin(true);
					}
				}
			});
		} else {
			if(purchaseProcess)
				_cii29showFormsToAccess(id);
			else
				_cci143showLogin(true);
		}
	}
}
function _cci143changeSelectForm(){
	_cii29jq('#select_form_cii29').fadeOut(250);
	_cci143showLogin();
}
function _cci143closeSelectForm(){
	_cii29hideForm('select_form_cii29', true);
}
function _cci143continue(){
	_cii29processOk = true;
	_cci143closeSelectForm();
}
function _cci143changeToRegisterForm(){
	if($('login_cii29') && $('register_cii29')){
		_cii29jq('#login_cii29').fadeOut(250, function(){
			_cci143showRegister();
		});
	}
}
function _cii29closeAllWindowfUse();
	_cii29closeDisclaimer();
	_cii29closeRegisterForm();
	_cii29closeLoginForm();
	_cci143closeSelectForm();
}
objectListen('load', window, _cii29init);


