var x;
x=$(document);
x.ready(init);

function init(){
	$("#btn_register").click(newRegister);
	$("#txt_password").keydown(checkPass);
	$("#txt_re_password").keydown(checkPass);
	$("#p_error_pass").keydown(checkPass);
}

var odata;
var api_expose;
var api_overlay;
function r(){
	api_overlay = $("#btn_terms").overlay({api: true,closeOnClick:false,onBeforeLoad:onBeforeLoad});
	var wrap = $("#_wrap");
	if (wrap.is(":empty"))wrap.load("terms.php");
}
$(r);

function onBeforeLoad(){
  api_expose = $("#_wrap").expose({api: true,closeOnClick:false}); 
  api_expose.load() 
}
function aceptarTerminos(b){
	api_expose.close();
	api_overlay.close();
	if(b){
		$("#chkTerms").attr("checked","checked");
		isTerminos=true;
	}else{
		$("#chkTerms").attr("checked",false);
	}
}
function newRegister(){
	var f_name = $("#txt_first_name").attr("value");
	var l_name = $("#txt_last_name").attr("value");
	var email = $("#txt_email").attr("value");
	var psw_1 = $("#txt_password").attr("value");
	var psw_2 = $("#txt_re_password").attr("value");
	var invite_id = $("#inviteid").attr("value");
	
	odata = {};
	odata.first_name = f_name;
	odata.last_name = l_name;
	odata.email = email;
	odata.password = psw_1;
	
	//isEmpty
	//if()
	if(!$("#chkTerms").attr("checked")){
		$.prompt('If you would like to register, please review and accept our Terms of Use',{buttons:{"Accept":true},prefix:'cleanblue'}); 
		return;
	}
	
	if(!checkNull(f_name)){
		$.prompt('Please enter your first name.',{buttons:{"Accept":true},prefix:'cleanblue'}); 
		return;	
	}
	if(!checkNull(l_name)){
		$.prompt('Please enter your last name.',{buttons:{"Accept":true},prefix:'cleanblue'}); 
		return;	
	}
	
	if(!checkeMail(email))return;
	
	if(!checkNull(psw_1)){
		$.prompt('Please enter a password.',{buttons:{"Accept":true},prefix:'cleanblue'}); 
		return;	
	}
	if(psw_1.length<6){
		$.prompt('Password should be at least 6 characters.',{buttons:{"Accept":true},prefix:'cleanblue'});
		return;	 
	}
	if(psw_1!=psw_2){
		$.prompt('Passwords do not match.',{buttons:{"Accept":true},prefix:'cleanblue'});
		return;	 
	}
	if(!$("#chkTerms").attr("checked")){
		$.prompt('If you would like to register, please review and accept our Terms of Use',{buttons:{"Accept":true},prefix:'cleanblue'}); 
		return;
	}
	/*
	if(!checkNull(psw_2)){
		$.prompt('Please confirm your password.',{buttons:{"Accept":true},prefix:'cleanblue'}); 
		return;	
	}*/

	if(invite_id==undefined || invite_id==null || invite_id=="" || invite_id=="undefined"){
		odata.invitation_code = $("#invitation_code").attr("value");
		if(!checkNull(odata.invitation_code)){
			$.prompt('Please enter your invitation code.',{buttons:{"Accept":true},prefix:'cleanblue'}); 
			return;	
		}
	}else{
		odata.inviteid = invite_id;
	}

	odata.action = "check_mail";
	$.post("process.php",odata,onCheckMail,"json"); 

}
function checkNull(v){
	if(v.length==0)return false;
	var p = 0;
	for(var i=0;i<v.length;i++){
		if (v.charAt(i) == " ") {
		 p++;
	   }
	}
	if(p==v.length)return false;
	return true;
}
function onCheckMail(resp){
	if(resp.status== "OK"){
		odata.action = "register";
		$.post("process.php",odata,onRegisterOK,"json");
		
	}else if(resp.status== "MAIL_IS_REQUIRED"){
		alert("e mail required!"); 
	
	}else{
		alert("This email is being used in HUBkap!"); 
	}
}
function onRegisterOK(resp){
	if(resp.status== "REGISTER_COMPLETE"){
		//HUBkap : ON_REGISTER_COMPLETE
		//HUBkap : ON_LOGIN_SUCCESS
		$("#top").attr("innerHTML","<p1>Welcome to Hubkap</p1><p></p>");
		$("#form").attr("innerHTML",'<font size="+0"><a href="http://beta.hubkap.com/">Start using HUBkap now!</a></font>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;');
		$("#bot2").attr("innerHTML","");	
	}else if(resp.status== "INVALID_INVITATION_CODE"){
		alert("INVALID_INVITATION_CODE"); 
	}else{
		
	}		
}

/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
var nav4 = window.Event ? true : false;
document.oncontextmenu = function(){return false} 
var interval;
var isCtrl=false;
function acceptPass(evt){
	var key = nav4 ? evt.which : evt.keyCode; 
	var k = String.fromCharCode(key);
	if(key==8 || key==0)return true;
	if(isCtrl)return false;//_@/=
	var filter6=/^[A-Za-z\\_@=1023456789\\s\xF1\xD1]+$/;
	return (filter6.test(k));
	
}
function checkPass(){
	window.clearInterval(interval);
	interval = window.setInterval("comparate()", 500*1);
}
function comparate(){
	$("#txt_password").keydown(checkPass);
	var pass1 =$("#txt_password").attr("value");
	var pass2 = $("#txt_re_password").attr("value");
	
	if(pass1.length>=6){
		if(pass1!=pass2)$("#p_error_pass").text(" Passwords do not match.");
		else $("#p_error_pass").text("");
	}else{
		$("#p_error_pass").text(" Password should be at least 6 characters");
	}
	window.clearInterval(interval);
}
function onKeyUpValidate(evt,id,type){
	
	var key = nav4 ? evt.which : evt.keyCode;  
	if (key == 17)isCtrl = false;
	var value = $("#"+id).attr("value");
	$("#"+id).attr("value",cleanString(value,type))
}

function cleanString(s,type){
	var ncad="";
	var permitidos="";
	if(type=="NUMBER"){
		permitidos = "0123456789";
	}
	if(type=="NAMES"){
		permitidos = "‡Ž’—œçƒêîò„–abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
	}
	if(type=="MAIL"){
		permitidos = "abcdefghijklmnopqrstuvwxyz@._-0123456789";
	}
	if(type=="MAIL2"){
		permitidos = "abcdefghijklmnopqrstuvwxyz@._,-0123456789";
	}
	if(type=="PASS"){
		permitidos = "_@/=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	}
	if(type=="NAMESNUMBER"){
		permitidos = ",.;‡Ž’—œçƒêîò„–abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";
	}
	 
	for(var i=0;i<s.length;i++){
		var j = false;
		for(var p=0;p<permitidos.length;p++){
			if(permitidos.charAt(p)==s.charAt(i)){
				j=true;	
			}
		}
		if(j)ncad+=s.charAt(i);
	}
	return ncad;
}


 
function isEmpty(aTextField){
	
	var re = /\s/g; //Match any white space including space, tab, form-feed, etc.
	var str = aTextField.replace(re, "");
	
	if(str.length == 0) {
		return true;
	}else{
		return false;
	}
} 

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		  $.prompt('Please enter a valid email.',{buttons:{"Accept":true},prefix:'cleanblue'}); 
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   $.prompt('Please enter a valid email.',{buttons:{"Accept":true},prefix:'cleanblue'}); 
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   $.prompt('Please enter a valid email.',{buttons:{"Accept":true},prefix:'cleanblue'}); 
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   $.prompt('Please enter a valid email.',{buttons:{"Accept":true},prefix:'cleanblue'}); 
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		   $.prompt('Please enter a valid email.',{buttons:{"Accept":true},prefix:'cleanblue'}); 
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    $.prompt('Please enter a valid email.',{buttons:{"Accept":true},prefix:'cleanblue'}); 
		    return false
		 }

	 return true					
}

function validateMail(email){
	if ((email==null)||(email=="")){
		$.prompt('Please enter an email.',{buttons:{"Accept":true},prefix:'cleanblue'}); 
		return false
	}
	if (!echeck(email)){
		return false
	}
	return true
 }

function checkeMail(mail){
    var str=mail;
    var filtro=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (filtro.test(str))
        resultado=true
    else{
		$.prompt('Please enter a valid email.',{buttons:{"Accept":true},prefix:'cleanblue'}); 
        resultado=false
    }
    return (resultado);
}

