var errors = 0;
var msisdn_se,msisdn_used_se,email_se,email_used_se,nick_se,nick_exists,pass1_se,pass2_se,terms_se,ip_limit,msisdn_s_limit,msisdn_blist,msisdn_g_limit;


function getElement(e,f){
    if(document.layers){
        f=(f)?f:self;
        if(f.document.layers[e]) {
            return f.document.layers[e];
        }
        for(W=0;i<f.document.layers.length;W++) {
            return(getElement(e,fdocument.layers[W]));
        }
    }
    if(document.all) {
        return document.all[e];
    }
    return document.getElementById(e);
}


function redraw(){
	var randnom = Math.random();
	getElement('simage').src = 'secure_image.php?reg=' + getElement('reg').value + '&' + randnom;
}


function verify_s2() {
	if (getElement('vercode'))
	{
		if(getElement('vercode').value)
			document.step2.submit();
		else
			alert('Please enter the verification code received in the text message.');
	}
	else
	{
		document.step2.submit();
	}
}

function onload_s2(){
	if (getElement('vercode'))
		getElement('vercode').focus();
}


function mark_error(){
	errors = 1;
	for (var i = 0; i < arguments.length; i++){
		getElement(arguments[i]).className = 'err';
	}
}


function ummark_error(){
	errors = 0;
	for (var i = 0; i < arguments.length; i++){
		getElement(arguments[i]).className = '';
	}
}


function cancel_reg(){
	document.location.href="../index.php";
}


function login(sid,location){
	var url = 'http://www.renzoo.co.uk/index.php?sid=' + sid;
	if(location)
		url += '&' + location;
	document.location.href = url;
}

function back_s1(){
	document.step2.action = '?step=1'
	document.step2.submit();
}

function valid_email(str){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(str.match(emailExp))
		return true;
	else
		return false;
}

function valid_user(str){
	var emailExp = /[^A-Z0-9-_\.]/i;
	if(str.match(emailExp))
		return false;
	else
		return true;
}

function chk_pass_len(str){
	if ((str.length < 4) || (str.length > 8))
		return false;
	else
		return true;
}

function valid_mob(str){
	var emailExp =  /^[1-9][0-9]+$/;
	if(str.match(emailExp))
		return true;
	else
		return false;
}

function verify_s1(){
	
	errors = 0;
	
	fill_err('msisdn_err','');
	fill_err('email_err','');
	fill_err('nick_err','');
	fill_err('terms_err','');
	fill_err('pass_err','');
	
	ummark_error('msisdn','email','nick','terms','pass1','pass2');
	
	if(getElement('msisdn').value.length > 0 && !valid_mob(trim(getElement('msisdn').value))){
		fill_err('msisdn_err','Invalid mobile phone number. Please use international format that includes country code and without leading zero.');
		mark_error('msisdn');
	}
	else if(msisdn_used_se){
		fill_err('msisdn_err','Mobile phone already registered with Renzoo.');
		mark_error('msisdn');
	}
	else if(msisdn_s_limit){
		fill_err('msisdn_err','You cannot change mobile phone more than twice during the registration process.');
		mark_error('msisdn');
	}	
	else if(msisdn_blist){
		fill_err('msisdn_err','Mobile phone number is on our black list! Please contact support at <a href="mailto:support@renzoo.co.uk">support@renzoo.co.uk</a> for more information.');
		mark_error('msisdn');
	}
	else if(msisdn_g_limit){
		fill_err('msisdn_err','You or somebody else already tried today to register this phone number. Please try tomorrow or register another phone number.');
		mark_error('msisdn');
	}
		

	if(getElement('email').value.length == 0){
		fill_err('email_err','Email is required!');
		mark_error('email');
	}
	else if(email_se || !valid_email(trim(getElement('email').value))){
		fill_err('email_err','Invalid email address.');
		mark_error('email');
	}
	else if(email_used_se){
		fill_err('email_err','Email already registered with Renzoo.');
		mark_error('email');
	}
	
	if(nick_se || !valid_user(trim(getElement('nick').value))){
		fill_err('nick_err','Nick can contain capital and small letters, numbers, period (.) and underscore (_).');
		mark_error('nick');
	}
	else if(nick_exists){
		fill_err('nick_err','Nick name already exists.');
		mark_error('nick');
	}
	if(getElement('nick').value.length == 0){
		fill_err('nick_err','Please choose your nick name.');
		mark_error('nick');
	}
	
	if(terms_se || getElement('terms').checked == false){
		fill_err('terms_err','You must agree with Terms of Service to continue registration');
		mark_error('terms');
	}
	
	if(getElement('pass1').value != getElement('pass2').value){
		fill_err('pass_err','Entered new passwords do not match!');
		mark_error('pass1','pass2');
	}
	if(!chk_pass_len(getElement('pass1').value)){
		fill_err('pass_err','Invalid password length.');
		mark_error('pass1','pass2');
	}
	if(pass1_se || pass2_se || getElement('pass1').value.length == 0){
		fill_err('pass_err','Passwords do not match!');
		mark_error('pass1','pass2');
	}
	
	ip_limit=msisdn_s_limit=msisdn_blist=msisdn_g_limit=msisdn_se=msisdn_used_se=email_se=email_used_se=nick_se=nick_exists=pass1_se=pass2_se=terms_se=0;
    
	if(!errors == 0){
		alert('Please correct marked data.');
		return false;
	}
	
	return true;	
}

function submit_s1(s){
	if(verify_s1())
		document.step1.submit();
}



function trim(s){
	return s.replace(/^\s+|\s+$/, '');
}

function fill_err(elem,content){
	getElement(elem).innerHTML = content;
}

function opwin(url,name,prms){
	var dim = 'height=630,width=600';
	if(url == 'pp')
		window.open('../privacy_policy.php','pp',dim);
	else if(url == 'up')
		window.open('../use_policy.php','up',dim);
	else if(url == 'ab')
		window.open('../about.php','ab',dim);
	else if(url == 'op')
		window.open('../online_payment.php','op',dim);
	else if(url == 'ts')
		window.open('../terms_of_service.php','ts',dim);
	else
		window.open(url,name,prms);	
}

function gotopage_nosession(url_query)
{
	var mainUrl = 'http://www.renzoo.co.uk/index.php?';
	document.location.href = mainUrl + url_query;
}

function main_menu(what, arg1)
{
		if (what == 'mobile_email_alerts' || what == 'payment' || what == 'features')
		gotopage_nosession("page=show_page_ex," + what + '&nologin=1');
}
