﻿var secure_url_prefix = '';
var logger_user = 0;

function getRegSuffix() {
	if (logger_user == 0) return '&reg=1';
	else return '';
}

function setSecureUrlPrefix(sBase, sPath) {
	secure_url_prefix = sBase;
	if ((sPath) && (sPath != ''))
		secure_url_prefix = secure_url_prefix + sPath + '/';
}

function addHostingToOrder(id) {
	document.InmediaFrm.action = secure_url_prefix + 'alojamiento_paso_1.html?event=td_add_hosting_to_order&hosting_id=' + id;
	document.InmediaFrm.submit();
}

function hosting_step_2() {
	if (!CheckStep1()) return false;
	document.InmediaFrm.action = secure_url_prefix + 'alojamiento_paso_2.html?event=td_set_hosting_domain';
	document.InmediaFrm.submit();
}


function goToStep3_hosting() {
	if (checkContactData()) {
		//document.InmediaFrm.action = 'comprar_paso_3.html?event=td_order_set_contact_data';
		document.InmediaFrm.action = secure_url_prefix + 'alojamiento_paso_3.html?event=td_order_set_contact_data' + getRegSuffix();;
		document.InmediaFrm.submit();
	}
}

function periodicity_pay() {
var oElem = document.InmediaFrm.typePay;
	for (var i=0; i<oElem.length; i++) {
		if (oElem[i].checked) {
			document.InmediaFrm.action = secure_url_prefix + 'alojamiento_paso_3.html?event=td_type_pay';
			document.InmediaFrm.submit();
			break;
		}
	}
}

function apply_coupon_hosting(){
	if(document.InmediaFrm.elements["coupon"].value==""){ 
		alert("Debe rellenar el campo cupón");
	}else{
		document.InmediaFrm.action = secure_url_prefix + 'alojamiento_paso_3.html?event=td_apply_coupon_hosting';
		document.InmediaFrm.submit();
	}
}



function CheckStep1(){
	var bHasDomain = false;
	var bNotHasDomain = false;
	
	var oElem = document.InmediaFrm.rb_has_domain;
	for (var i = 0; i < oElem.length; i++) {
		if (oElem[i].checked) {
			if (oElem[i].value == '1') 	bHasDomain = true;
			else 						bNotHasDomain = true;
			break;
		}
	}
	
	var bHasSelectedDomain = false;
	
	oElem = document.getElementById('registeredUser');
	var bIsLogged = (!oElem || oElem.style.display == 'none');
	
	if (bIsLogged) {
		oElem = document.InmediaFrm.rb_sel_domain;
		if (oElem.length) {
			for (var j = 0; j < oElem.length; j++) {
				if (oElem[j].checked) {
					bHasSelectedDomain = true;
					break;
				}
			}
		} else {
			bHasSelectedDomain = oElem.checked;
		}
	}
	
	if (bHasDomain && !bIsLogged) {
		alert('Regístrese antes de continuar con la compra de su alojamiento.');
		document.InmediaFrm.user_login.focus();
		//document.InmediaFrm.user_login.style.backgraund-color = '#ffc';
		document.InmediaFrm.user_login.className = 'error';
		return false;
	}
	if (bHasDomain && bIsLogged && !bHasSelectedDomain) {
		alert('Seleccione a que dominio quiere asociar el plan de hosting.');
		return false;
	}
	if (bNotHasDomain && document.InmediaFrm.domain_name.value == false) {
		alert('Introduzca el nombre del dominio al que se asociará el alojamiento que está contratando.');
		document.InmediaFrm.domain_name.focus();
		//document.InmediaFrm.domain_name.style.backgraund-color = '#ffc';
		document.InmediaFrm.domain_name.className = 'error';
		return false;
	}
	
	return true;
}

function hosting_loginKeyUp(e, step) {
	var keynum;
	if(window.event) { keynum = e.keyCode; }  
	else if(e.which)  { keynum = e.which; }  
	if(keynum == 13) {
		hosting_login(step);
	}
}

function hosting_login(step) {
	
	var user = document.getElementsByName('user_login')[0];
	var pass = document.getElementsByName('user_password')[0];
	
	if (user && user.value.length > 0 && pass && pass.value.length > 0) {
		document.InmediaFrm.action = secure_url_prefix + 'alojamiento_paso_'+step+'.html?event=td_login_hosting';
		document.InmediaFrm.submit();
	} else {
		alert('Introduzca el usuario y la contraseña.');
	}
}

function hosting_logoff() {
	document.InmediaFrm.action = secure_url_prefix + 'alojamiento_paso_1.html?event=td_order_logoff';
	document.InmediaFrm.submit();
}


function hosting_saveUserData() {
	if (checkUserData()) {
		document.InmediaFrm.action = secure_url_prefix + 'alojamiento_paso_2.html?event=td_order_set_new_contact_data' + getRegSuffix();;
		document.InmediaFrm.submit();
	}
}

function hosting_exit() {
	document.InmediaFrm.action = 'hosting_alojamiento_web.html?event=td_order_clean_lines';
	document.InmediaFrm.submit();
}


function change_rb_has_domain() {
	var oElem = document.InmediaFrm.rb_has_domain;
	for (var i = 0; i < oElem.length; i++) {
		if (oElem[i].checked) {
			if (oElem[i].value == '1') 	show_UserWithDomain();
			else 						show_NotUserWithDomain();
			break;
		}
	}
}

function show_UserWithDomain() {
	document.getElementById('UserWithDomain').style.display = '';
	document.getElementById('NotUserWithDomain').style.display = 'none';
	//document.getElementById('boton').style.display = 'none';
}

function show_NotUserWithDomain() {
	document.getElementById('NotUserWithDomain').style.display = '';
	document.getElementById('UserWithDomain').style.display = 'none';
	//document.getElementById('boton').style.display = '';
}
/*

*/