//alert(2);
function makeEmpty()
{
	document.frmregister.userName.value="";
	document.frmregister.userPassword.value="";
	document.frmregister.rePassword.value="";
	document.frmregister.userEmail.value="";

	document.frmregister.firstName.value="";
	document.frmregister.lastName.value="";
	document.frmregister.company.value="";
	document.frmregister.jobTitle.value="";
	document.frmregister.address1.value="";

	document.frmregister.city.value="";
	document.frmregister.state.value="";
	document.frmregister.zip.value="";

	document.frmregister.sec_code.value="";
	document.frmregister.sess_code.value="";
}


function validate(form)
{

    var illegalChars = /[\W_]/;
	var username=form.userName.value;
	var userpwd=form.userPassword.value;
	var repwd=form.rePassword.value;
	var useremail=form.userEmail.value;

	var firstname=form.firstName.value;
	var lastname=form.lastName.value;
	var company_name=form.company.value;
	var job_title=form.jobTitle.value;
	var addr1=form.address1.value;

	var city_name=form.city.value;
	var state_name=form.state.value;
	var zip_code =form.zip.value;
	var Sec_Code=form.sec_code.value;
	var Sess_Code=form.sess_code.value;


	if(trim(username)==0)
	{
		inlineMsg('userName','Please enter username.',3);
   		return false;
	}

	if(trim(username)!="")
	{
		if(username.length<4)
		 {
		    inlineMsg('userName','Username must have at least 4 alphanumeric characters.',3);
   		    return false;
		 }
		 if(username.length>16)
		 {
		    inlineMsg('userName','Username should not have more than 16 alphanumeric characters  .',3);
   		    return false;
		 }


	}
	if (illegalChars.test(username)) {
		//alert("The new  password contains illegal characters.");
		inlineMsg('userName','Username contains illegal characters.',3);
		return false;
    }
	if(userpwd==0)
	{
		inlineMsg('userPassword','Please enter password.',3);
   		return false;
	}
	if(trim(userpwd)!="")
	{
		if(userpwd.length<6)
		 {
		    inlineMsg('userPassword','Password must have at least 6 characters.',3);
   		    return false;
		 }
		 if(userpwd.length>16)
		 {
		    inlineMsg('userPassword','Password should not have more than 16 characters  .',3);
   		    return false;
		 }


	}
	if (illegalChars.test(userpwd)) {
		//alert("The new  password contains illegal characters.");
		inlineMsg('userPassword','The new  password contains illegal characters.',3);
		return false;
    }
	if(repwd==0)
	{
	  inlineMsg('rePassword','Please reenter password.',3);
   		return false;
	}
	if(trim(repwd)!="")
	{
	  if(userpwd!=repwd)
	  {
	    inlineMsg('rePassword','Password and confirm password do not match.',3);
   		 return false;
	  }
	}

	if(useremail==0)
	{
		inlineMsg('userEmail','Please enter email.',3);
   		return false;
	}
	/*
	if(useremail!="")
     {
       rx=new RegExp("^[a-z0-9_\\-]+(\\.[_a-z0-9\\-]+)*@([_a-z0-9\\-]+\\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)\$");
		if(!rx.test(useremail))
		{
			inlineMsg('userEmail','Please enter valid email address.',3);
   		      return false;
		}
	 }
	 */


	if(firstname==0)
	{
		inlineMsg('firstName','Please enter first name.',3);
   		return false;
	}

	if(lastname==0)
	{
		inlineMsg('lastName','Please enter last name.',3);
   		return false;
	}

	if(company_name==0)
	{
		inlineMsg('company','Please enter company or stage name.',3);
   		return false;
	}

	if(addr1==0)
	{
		inlineMsg('address1','Please enter address.',3);
   		return false;
	}




	if(city_name==0)
	{
		inlineMsg('city','Please enter city.',3);
   		return false;
	}

	if(city_name!="")
	{
	  //rx=new RegExp("^[a-zA-z]+$"); for only alphabets
	  rx=new RegExp("^[a-zA-Z '-]*$");
		if(!rx.test(city_name))
		{
			inlineMsg('city','This field accepts alphabetic characters.',3);
   		      return false;
		}

	}

	 if(form.county.value==0){
		inlineMsg('county','Please select county.',3);
   		return false;
	}
	 if(form.region.value==0){
		inlineMsg('county','Please select region.',3);
   		return false;
	}
	 if(zip_code==0){
		inlineMsg('zip','Please enter zip code.',3);
   		return false;
	}


	if(zip_code!=""){
		 //alert(2);
			if(isNaN(zip_code)){
				inlineMsg('zip','This field accepts only numbers.',3);
				return false;
			}
	 }

	if(Sec_Code==0)
	 {
	   inlineMsg('sec_code','Please enter security code.',3);
   		return false;
	 }
   if(Sec_Code!="")
	 {
	  if(Sec_Code!=Sess_Code){
	   inlineMsg('sec_code','Entered security code is wrong.',3);
   		return false;
		}
	 }
	//return true;
}
function fileValidation(fld,id)
{
	var filename='file_'+id;
	if(!/(\.bmp|\.gif|\.jpg|\.jpeg|\.png)$/i.test(fld.value)) {
		inlineMsg('file_'+id,'Invalid File Type.',3);
		document.getElementById('file_'+id).value="";
		fld.form.reset();
		fld.focus();
		return false;
	}
	return true;

}

function isNumberKey(evt)
{

 var charCode = (evt.which) ? evt.which : event.keyCode
 if (charCode > 8 && charCode <= 222)
	return false;

 return true;
}


//Ajax code
function sendRequest(varURL,varObj){ //Function for open method
	varObj.open("GET", varURL, true);
	varObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	varObj.send(null);
}

function createObject(varobj){  //Function for creating XMLHttpRequest object
	if(window.XMLHttpRequest)
		varobj = new XMLHttpRequest();
	else if (window.ActiveXObject)
		varobj = new ActiveXObject("Microsoft.XMLHTTP");
	return varobj;
}

function fillCombobox(varObj,varctrlName){  //Function for filling select box
	if(document.frmregister.county.options.length > 0){
		for(j=document.frmregister.county.options.length-1;j>=0;j--)
			document.frmregister.county.options[j]=null;
	}

	var resp = varObj.responseText;
	var arrresp = resp.split("|");

	for(i=0;i<arrresp.length;i++){
		var arropt=	arrresp[i].split(":");
		var opt = new Option(arropt[1], arropt[0]);
		document.frmregister.county[document.frmregister.county.options.length] = opt;
	}
}

function getCountryState(ctrlName, country)  //Function to get country and sending request
{

	document.getElementById(ctrlName).disabled=false;

	var req = null;
	req = createObject(req);

	req.onreadystatechange = function()
	{
		if(req.readyState == 4) {
			if(req.status == 200)
				fillCombobox(req,ctrlName);
			else
				alert("Error: returned status code");
		}
	};


	region_id = document.getElementById(country).value;
	county_val = document.getElementById('county_val').value;

	if(document.getElementById('county_val').value!=0)
	{
		srcdetail_id = document.getElementById('county_val').value;
		url = "select_county.php?region_id="+region_id+"&county_id="+srcdetail_id;
	} else {
		url = "select_region.php?region_id="+region_id;
	}

	sendRequest(url,req);
}
//End Ajax code

function GetRegionByCounty() {
	var county_id = $('#county').val();
	$('#region_msg').hide();
	$.ajax({
		url: 'select_region.php?county_id='+county_id,
		type: 'GET',
		success: function(result) {
			$('#region').val(result);

			var region = '';
			switch (result) {
				case '1': region = 'North West';	break;
				case '2': region = 'North East';	break;
				case '3': region = 'Central West';	break;
				case '4': region = 'Central';		break;
				case '5': region = 'Central East';	break;
				case '6': region = 'South East';	break;
				case '7': region = 'South West';	break;
				default: return;
			}
			$('#region_msg').html('You will be listed in '+region+' Florida');
			$('#region_msg').slideDown();
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			$('#region').val('0');
		}
	});
}

function ToggleOther() {
	if ($('#other_service_chk').is(':checked')) {
		$('#other_service').removeAttr("disabled");
	} else {
		$('#other_service').attr("disabled","disabled");
	}
}