
$('form#registration_form').submit(function() {

    var name = document.registration_form.user_group_id.value;
    var ajaxRequest;  // The variable that makes Ajax possible!

	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			//alert(ajaxRequest.responseText);
            var posterror = document.getElementById('posterror');
            if(ajaxRequest.responseText == ''){
                posterror.innerHTML = '';
                document.registration_form.submit();
            }else{
                reloadCaptcha();
                posterror.innerHTML = ajaxRequest.responseText;
                //alert(ajaxRequest.responseText);
            }
        }
	}

    var username    = document.registration_form.username.value;
    var password1   = document.registration_form.elements['password[original]'].value;
    var password2   = document.registration_form.elements['password[confirmed]'].value;
    var email       = document.registration_form.email.value;
    var captcha     = document.registration_form.captcha.value;
    var agree       = document.registration_form.AgreeTerms;
    var agreeterm;

    if(agree.checked) agreeterm = 1;
    else agreeterm = 0;

    var query   = '?name='+name;
        query  += '&username='+username;
        query  += '&password1='+password1;
        query  += '&password2='+password2;
        query  += '&email='+email;

    if(name == 'JobSeeker') {
        query += '&namefield='+document.registration_form.FirstName.value;
        query += '&lastname='+document.registration_form.LastName.value;
    }
    else if(name == 'Employer') {
        query += '&namefield='+document.registration_form.CompanyName.value;
        query += '&contactname='+document.registration_form.ContactName.value;
		//query  += '&state='+document.registration_form.Province.value;
        query += '&companydesc='+FCKeditorAPI.GetInstance('CompanyDescription').GetXHTML(true);
    }

		query  += '&country='+document.registration_form.Country.value;
		query  += '&state='+document.registration_form.State.value;
        query  += '&city='+document.registration_form.City.value;
		query  += '&zipcode='+document.registration_form.ZipCode.value;
		query  += '&address='+document.registration_form.Address.value;	
        query  += '&phonenumber='+document.registration_form.PhoneNumber.value;
        query  += '&captcha='+captcha;
        query  += '&agree='+agreeterm;

	ajaxRequest.open("GET", "/custom/registration/validation.php"+query, true);
	ajaxRequest.send(null);

    return false;
    
});

$('#reload_captcha').click(function() {
	reloadCaptcha();
});

function reloadCaptcha(){
    document.getElementById("captchaimg").src = '/custom/kcaptcha/index.php?'+Math.random();
}
