
$(function(){
	$("#registration-popup input").bind("keydown",function(e){
		if (e.keyCode == 13){
			registerOY();
		}
	});
});

function popRegistration(){

	$("#popup-overlay").click(function(){
		hideRegistration();
	});
	
	$("#registration-popup").center();
	
	$("#popup-overlay").show();
	$("#registration-popup").show();
	
}

function hideRegistration(){
	$("#popup-overlay").hide();
	$("#registration-popup").hide();	
}

function clearErrors(){
	$("#registration-content input ~ .error").html("");
	$("#registration-error").html("");
}

function registerOY(successURL){
	
	clearErrors();
	
	regShowLoading();
	
	cnp = $("#registration-cnp").val();
	msisdn = $("#registration-msisdn").val();
	
	errors = false;
	if (typeof cnp != 'string' || !cnp.length){
		$("#registration-cnp ~ .error").html("Acest camp este obligatoriu");
		errors = true;
	} else {
		res = checkCNP(cnp);
		if (res !== true){
			errors = true;
			$("#registration-cnp ~ .error").html(res);
		}		
	}
	if (typeof msisdn != 'string' || !msisdn.length){
		$("#registration-msisdn ~ .error").html("Acest camp este obligatoriu");
		errors = true;
	} else {
		res = checkMSISDN(msisdn);
		if (res !== true){
			errors = true;
			$("#registration-msisdn ~ .error").html(res);
		} else {
			msisdn = "0"+msisdn.match(/^(40|0)?(7\d{8})$/)[2];
		}				
	}
	
	if (errors){
		regShowButton();
		return;
	}
	
	
	$.ajax({
		url : 'registerOY.php',
		data : {
			cnp: cnp,
			msisdn: msisdn
		},
		dataType : 'json',
		type: 'post',
		error: function (req, status, err){
			$("#registration-error").html("Eroare la inscriere, va rugam reincercati");
			regShowButton();
		},
		success: function (data, status, req){
			regShowButton();
			if (data.error == true){
				$("#registration-error").html(data.global);				
				$("#registration-msisdn ~ .error").html(data.msisdn);
				$("#registration-cnp ~ .error").html(data.cnp);
			} else {							
				
				if (typeof successURL == 'undefined' || !successURL){
					
					if (typeof successRegistrationURL == 'undefined' || !successRegistrationURL){
					
						window.location.href = window.location.href;
						
					} else {
						
						self.location.href = successRegistrationURL;
						
					}
					
				} else {
					
					self.location.href = successURL;
					
				}
				
			}
		}
	});
}

function regShowLoading(){
	$("#registration-button").hide();	
	$("#registration-loading").show();
}

function regShowButton(){
	$("#registration-loading").hide();
	$("#registration-button").show();
}

function checkCNP(cnp){
	
	// basic regex check
	if (!/^[1-2][0-9]{2}[0-1][0-9][0-3][0-9]{7}$/.exec(cnp)){
		return "CNP-ul introdus nu este valid.";		
	}
	
	key = "279146358279";
	
	// extract CNP parts
	cs = cnp.substr(12,13);
	
	// compute checsum
	sum = 0;
	for (i=0;i<12;i++){
		sum+= (key.substr(i,1)*cnp.substr(i,1));
	}
	checksum = sum % 11;
	if (checksum == 10){
		checksum = 1;
	}
	
	if (checksum != cs ){
		return "CNP-ul introdus nu este valid.";		
	}
	
	return true;
}

function checkMSISDN(msisdn){
	
	if (typeof msisdn != 'string' || !msisdn.length){
		return "Numarul de telefon introdus nu este valid.";
	}
	
	if (!/^(40|0)?(7\d{8})$/.exec(msisdn)){
		return "Numarul de telefon introdus nu este valid.";
	}
	
	return true;
	
}
