var gral_error = 0;
var valor_inicial = new Array();
var j_ct_lbls = 1;
var cn_ch_fr = 0;
var objs = new Object();
var objs_name = new Array();
var count_rows = 0;
window.addEvent("load", function(){
	if ($('next')) {
		$('next').addEvent("click", function(e){
			e.preventDefault();
			var answer = "";
			if ($("answer_a").checked == true) {
				answer = "a";
			}
			else {
				answer = "b";
			}
			//window.location.href = base_url + this.get("href") + "/" + answer;
			window.location = base_url + this.get("href") + "/" + answer;
		});
	}
	if($("frmLogin")){
		create_labels("frmLogin");
		
		$("frmLogin").addEvent("submit", function(e){
			e.stop();
			e.preventDefault();
			error = 0;
			if($("firstname").value.trim() == "" || $("firstname").value.trim() == "Nombre"){
				hightlight($("firstname"));
				error = 1;
			}
			if($("lastname").value.trim() == "" || $("lastname").value.trim() == "Apellido"){
				hightlight($("lastname"));
				error = 1;
			}
			if($("num_client").value.trim() == "" || $("num_client").value.trim() == "Nº Gestión"){
				hightlight($("num_client"));
				error = 1;
			}
			if(error == 0){
				$("frmLogin").submit();
			}
		});
	}
	if ($('invitarlos')) {

		create_labels("content_invitation");

		$("frm").addEvent("submit", function(e){
			e.stop();
			e.preventDefault();
			string = "";
			for(i=1; i<=$("cn_ch_fr").value; i++){
				if ($("email_" + i)) {
					if ($("fullname_" + i)) {
						if (validate($("email_" + i), $("fullname_" + i), $("phone_"+i), $("prefix_"+i)) == 0) {
							count_rows++;
							string += $("fullname_" + i).value + "," + $("email_" + i).value + "," + $("prefix_" + i).value + "," + $("phone_" + i).value + ";";
						}
					}
				}
			}
			$("cp_inf").value = string;
			gral_error = 0;
			for(var clave in objs){
				if(objs[clave] == 1){
					gral_error = 1;
				}
			}
			if(gral_error == 0 && count_rows > 0){
				$("frm").submit();
			}else{
				if(count_rows == 0){
					hightlight($("fullname_1"));
					hightlight($("email_1"));
					hightlight($("prefix_1"));
					hightlight($("phone_1"));
				}
			}
		});
	}
});
function validate(obj1, obj2, obj3, obj4){
	if(obj2.value.trim() != "" && obj2.value.trim() != "Nombre y Apellido"){
		if(obj1.value.trim() == "" || obj1.value.trim() == "E-mail"){
			hightlight(obj1);
			objs[obj1.id] = 1;
			return 1;
		}else{
			if (obj1.value.indexOf('@') < 1 || obj1.value.lastIndexOf('.') < obj1.value.indexOf('@') + 2) {
				hightlight(obj1);
				objs[obj1.id] = 1;
				return 1;
			}else{
				reset(obj1);
				objs[obj1.id] = 0;
			}
		}
	}
	if(obj1.value.trim() != "" && obj1.value.trim() != "E-mail"){
		if(obj2.value.trim() == "" || obj2.value.trim() == "Nombre y Apellido"){
			hightlight(obj2);
			objs[obj2.id] = 1;
			return 1;
		}else{
			if(obj3.value.trim() == "" || obj3.value.trim() == "Teléfono"){
				hightlight(obj3);
				objs[obj3.id] = 1;
				return 1;
			}else{
				if(obj4.value.trim() == "" || obj4.value.trim() == "Prefijo"){
					hightlight(obj4);
					objs[obj4.id] = 1;
					return 1;
				}else{
					reset(obj3);
					objs[obj3.id] = 0;
					
					reset(obj2);
					objs[obj2.id] = 0;
					
					reset(obj4);
					objs[obj4.id] = 0;
					return 0;
				}
			}
		}
	}
	if((obj1.value.trim() != "" && obj1.value.trim() != "E-mail") && (obj2.value.trim() != "" && obj2.value.trim() != "Nombre y Apellido")){
		return 2;
	}
}
function reset(obj){
	obj.setStyle('border-color', '#ECECEC');
	obj.setStyle('background-color', '#F6F6F6');
}
function hightlight(obj){
	new Fx.Tween(obj.id, {
			duration: 1500,
			transition: Fx.Transitions.linear,
			onComplete: function(){
				new Fx.Tween(obj.id, {
					duration: 1500,
					transition: Fx.Transitions.linear,
					onComplete: function(){
						new Fx.Tween(obj.id, {
							duration: 1000,
							transition: Fx.Transitions.linear
						}).start('border-color', '#ECECEC', '#F36969');
					}
				}).start('background-color', '#F3A7A1', '#F6F6F6');
			}
		}).start('background-color', '#F6F6F6', '#F3A7A1');
}

function create_labels(obj){
	$$("#"+obj+" input").each(function(element, i){
		element.setAttribute("tabindex",j_ct_lbls);
		valor_inicial[element.id] = element.value;
		objs[element.id] = 0; 
		if(element.type == "text"){
			valor_inicial[element.id] = element.value;
			$(element.id).addEvent("blur", function(){
				 if(element.value.trim() == ''){
				 	element.value=valor_inicial[element.id];
				 }
			});
			$(element.id).addEvent("focus", function(){
				if(element.value.trim() == valor_inicial[element.id]  || element.value.trim() == valor_inicial[element.id]+"*"){
					element.value='';
				}
			});
		}
		j_ct_lbls++;
	});
}

var cntbl = 4;
function add_friend(){
	if (cntbl < 13) {
		if (cntbl == 12) {
			$("add_friends").setStyle("display", "none");
		}
		var obj = document.createElement("table");
		obj.setAttribute('id', 'child_' + cntbl);
		obj.setAttribute('cellpadding', '0');
		obj.setAttribute('cellspacing', '0');
		$("content_invitation").appendChild(obj);
		
		$(obj.id).className = 'table_invitation';
		
		
		var obj_tbody = document.createElement("tbody");
		obj.appendChild(obj_tbody);
		
		var obj_tr = document.createElement("tr");
		obj_tbody.appendChild(obj_tr);
		
		var obj_td = document.createElement("td");
		obj_td.setAttribute('id', 'td_c1_' + cntbl);
		obj_tr.appendChild(obj_td);
		
		$(obj_td.id).setStyle('width', '173px');
		
		var obj_input = document.createElement("input");
		obj_input.setAttribute("type", "text");
		obj_input.setAttribute("name", "fullname_" + cntbl);
		obj_input.setAttribute("id", "fullname_" + cntbl);
		obj_td.appendChild(obj_input);
		
		$(obj_input.id).className = "fullname";
		$(obj_input.id).value = "Nombre y Apellido";
		
		obj_td = document.createElement("td");
		obj_td.setAttribute('id', 'td_c2_' + cntbl);
		obj_tr.appendChild(obj_td);
		
		$(obj_td.id).setStyle('width', '173px');
		
		obj_input = document.createElement("input");
		obj_input.setAttribute("type", "text");
		obj_input.setAttribute("name", "email_" + cntbl);
		obj_input.setAttribute("id", "email_" + cntbl);
		obj_td.appendChild(obj_input);
		
		$(obj_input.id).className = "email";
		$(obj_input.id).value = "E-mail";
		
		obj_td = document.createElement("td");
		obj_td.setAttribute('id', 'td_c3_' + cntbl);
		obj_tr.appendChild(obj_td);
		
		$(obj_td.id).setStyle('width', '190px');
		
		obj_input = document.createElement("input");
		obj_input.setAttribute("type", "text");
		obj_input.setAttribute("name", "prefix_" + cntbl);
		obj_input.setAttribute("id", "prefix_" + cntbl);
		obj_td.appendChild(obj_input);
		
		$(obj_input.id).className = "prefix";
		$(obj_input.id).value = "Prefijo";
		
		obj_input = document.createElement("input");
		obj_input.setAttribute("type", "text");
		obj_input.setAttribute("name", "phone_" + cntbl);
		obj_input.setAttribute("id", "phone_" + cntbl);
		obj_td.appendChild(obj_input);
		
		$(obj_input.id).className = "phone";
		$(obj_input.id).value = "Teléfono";
		
		create_labels(obj.id);
		
		$("cn_ch_fr").value = cntbl;
		cntbl++;
	}else{
		$("add_friends").setStyle("display", "none");
	}
		
}
function change_status_friend(){
	if($("chk_recibir").checked == false){
		$("link_amigo").set("href", "amigos/preguntas/h/1/_/false");
		$("link_amiga").set("href", "amigos/preguntas/m/1/_/false");
	}else{
		
		$("link_amigo").set("href", "amigos/preguntas/h/1/_/true");
		$("link_amiga").set("href", "amigos/preguntas/m/1/_/true");
	}
}

