$(document).ready(function(){
	var is_valid_user = false;
	showObject('usercheck_loading',false);
	$('#reg_username').blur(function() {
		check_username();
	}); //end user checking
	$('#reg_password').blur(function() {
		check_password();
	});
	$('#confirmpassword').blur(function() {
		check_confirm_password();
	});
	$('#email').blur(function() {
		check_email();
	});
	$('#confirmemail').blur(function() {
		check_confirm_email();
	});
	$('#name').blur(function() {
		check_real_name();
	});
	$('#country').blur(function() {
		check_country();
	});
	$('#country').change(function() {
		check_country();
	});
	$('#male').blur(function() {
		check_gender();
	});
	$('#male').change(function() {
		check_gender();
	});
	$('#female').blur(function() {
		check_gender();
	});
	$('#female').change(function() {
		check_gender();
	});
	$('#month').blur(function() {
		check_dob();
	});
	$('#month').change(function() {
		check_dob();
	});
	$('#day').blur(function() {
		check_dob();
	});
	$('#day').change(function() {
		check_dob();
	});
	$('#year').blur(function() {
		check_dob();
	});
	$('#year').change(function() {
		check_dob();
	});
	$('#notlisted').change(function() {
		changeSelection();
	});
	$('.captcha').blur(function() {
		check_captcha();
	});
	$('#form').submit(function() {
		$('#alertme').fadeOut('slow');
		
		if(check_username() && check_password() && check_confirm_password() && check_email() && check_confirm_email() &&
			 check_real_name() && check_country() && check_gender() && check_dob() && check_cellphone() && check_tos()) {
				$('#signup').attr('disabled','disabled');
				$('#signup').attr('value','Registering...');
		 }
		 else {
				$('#alertme').addClass('alert');
				$('#alertme').html('There is slight problem in creating your account. Please check below');
				$('#alertme').fadeIn('slow');
				return false;
		 }
	});
	$('#settings').submit(function() {
		if(check_password() && check_confirm_password() && check_real_name() && check_country() && check_gender() && check_dob() && check_cellphone()) {
				$('#update').attr('disabled','disabled');
				$('#update').attr('value','Updating...');
		 }
		 else {
				$('#alertme').addClass('alert');
				$('#alertme').html('There is slight problem in updating your account. Please check below');
				$('#alertme').fadeIn('slow');
				return false;
		 }
	});
});
function check_username() {
	user_name = $('#reg_username').val();
	is_valid_user = false;
	 if(user_name.length < 4) {
		 showResult('usercheck',1);
	 }
	 else {
		 showObject('usercheck',false);
		 showObject('usercheck_loading',true);
		 $.ajax({
		 	url: ajax_url,
  		cache: false,
			type: "POST",
			async:false,
			data: 'action=check_user&username='+user_name,
		  success: function(response){
				showObject('usercheck_loading',false);
				response = escape(response);
				if(response == 1) {
					showResult('usercheck',1);
				}
				else{
					showResult('usercheck',0);
					is_valid_user = true; //global variable
				}
		  }
			});
	 }
	 return is_valid_user;
}

function check_password() {
	password = $('#reg_password').val();
	if(password.length < 4) {
		showResult('passwordcheck',1);
		return false;
	}
	else {
		showResult('passwordcheck',0);
		if($('#confirmpassword').val() != '' && $('#confirmpassword').val()  == password) {
			showResult('confirmpasswordcheck',0);
		}
	}
	return true;
}
function check_confirm_password() {
	password = $('#reg_password').val();
	confirmpassword = $('#confirmpassword').val();
	if(confirmpassword == '' || confirmpassword.length < 4 || password != confirmpassword) {
		showResult('confirmpasswordcheck',1);
		return false;
	}
	else {
		showResult('confirmpasswordcheck',0);
		return true;
	}
}
function check_email () {
	email = $('#email').val();
	if(email == '' || checkEmailAddress(email) == false) {
		showResult('emailcheck',1);
	}
	else {
		showResult('emailcheck',0);
		if($('#confirmemail').val() != '' && $('#confirmemail').val()  == email) {
			showResult('confirmemailcheck',0);
		}
		return true;
	}
	return false;
}

function check_confirm_email() {
	email = $('#email').val();
	confirmemail = $('#confirmemail').val();
	if(confirmemail == '' || checkEmailAddress(confirmemail) == false || email != confirmemail) {
		showResult('confirmemailcheck',1);
		return false;
	}
	else {
		showResult('confirmemailcheck',0);
		return true;
	}
}

function check_real_name() {
	name = $('#name').val();
	if(name.length < 3) {
		showResult('realnamecheck',1);
		return false;
	}
	else {
		showResult('realnamecheck',0);
		return true;
	}
}

function check_country() {
	country = $('#country').val();
	if(country == '') {
		showResult('countrycheck',1);
		return false;
	}
	else {
		showResult('countrycheck',0);
		return true;
	}
}

function check_gender() {
	if($('#male').attr('checked')==false && $('#female').attr('checked')==false) {
		showResult('gendercheck',1);
		return false;
	}
	else {
		showResult('gendercheck',0);
		return true;
	}
}

function check_dob() {
	month = $('#month').val();
	day = $('#day').val();
	year = $('#year').val();
	if(month == 0 || day == 0 || year == 0) {
		showResult('dobcheck',1);
		return false;
	}
	else {
		dob = day + '/' + month + '/' + year;
		dobcheck = dob.match(/^\d\d?\/\d\d?\/\d\d\d\d$/);
		if(dobcheck) {
			showResult('dobcheck',0);
			return true;
		}
		else {
			showResult('dobcheck',1);
			return false;
		}
	}
}

function check_cellphone() {
	if($('#notlisted').attr('checked') == false) {
		if($('#phone_brand').val() == 0 || $('#mobile').val() == 0) {
			showResult('mobilecheck',1);
			return false;
		}
	}
	showResult('mobilecheck',0);
	return true;
}

function check_captcha() {
	if($('.captcha').val() == '' || $('.captcha').val().length < 4) {
		showResult('captchacheck',1);
		return false;
	}
	else {
		showResult('captchacheck',0);
		return true;
	}
}

function check_tos() {
	$('#tosalert').hide();
	if($('#tos').attr('checked') == false) {
		$('#tosalert').addClass('alert');
		$('#tosalert').html('You must agree to our tos before you signup');
		$('#tosalert').fadeIn('slow');
		return false;
	}
	else {
		$('#tosalert').fadeOut('slow');
		return true;
	}
}

function showObject(obj,show) {
	if(show) {
		$('#'+obj).css('display','inline-block');
	}
	else {
		$('#'+obj).css('display','none');
	}
}
function showResult(id, response) {
	if(response == 1){
		$('#'+id).removeClass('check');
		$('#'+id).addClass('cross');
		showObject(id,true);
	}
	else {
		$('#'+id).removeClass('cross');
		$('#'+id).addClass('check');
		showObject(id,true);
	}
}
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
function prepareInputsForHints() {
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
		// test to see if the hint span exists first
		if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
			// the span exists!  on focus, show the hint
			inputs[i].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			// when the cursor moves away from the field, hide the hint
			inputs[i].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
	// repeat the same tests as above for selects
	var selects = document.getElementsByTagName("select");
	for (var k=0; k<selects.length; k++){
		if (selects[k].parentNode.getElementsByTagName("span")[0]) {
			selects[k].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			selects[k].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
}
addLoadEvent(prepareInputsForHints);

function checkEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

function updateCellPhone() {
	pcid = $('#phone_brand').val();
	$('#mobile >option').remove();
	if(pcid==0) {
		$("select#mobile").html('<option value="0">Select model</option>');
	}
	else {
		$("select#mobile").html('<option value="0">Loading...</option>');
	}
	$('#mobile').attr('disabled','disabled');
	
	posturl = site_url+"/ajax/cellphone-handler.php";
	$.getJSON(posturl, {ck: 'cell',pcid: escape(pcid)}, updateMobiles);
}
function updateMobiles(data) {
	
	var values = '';
	$('#mobile >option').remove();
	for (var i = 0; i < data.length; i++) {
		if(data[i] != null) {
			values += '<option value="'+data[i].optionValue+'">'+data[i].optionDisplay+'</option>';
		}
  }
	if(values != '') {
		$("select#mobile").html(values);
		$('#mobile').attr('disabled','');
	}
	else {
		$("select#mobile").html('<option value="0">Failed to load mobiles. Try again..</option>');
	}
}
function changeSelection() {
	if($('#notlisted').attr('checked') == true) {
		$('#phone_brand').attr('disabled','disabled');
		$('#mobile').attr('disabled','disabled');
	}
	else {
		$('#phone_brand').attr('disabled','');
		if($('#mobile option').length <=1) {
			$('#mobile').attr('disabled','disabled');
		}
		else {
			$('#mobile').attr('disabled','');
		}
	}
}
function SelectOptions(id, selectValue){
 selectName = document.getElementById(id);
 for (i=0;i<selectName.options.length;i++){
	 if (selectValue==selectName[i].value) {
		 selectName[i].selected = true;
		}
	}
}