/*
 * SimpleModal Basic Modal Dialog
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2009 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: basic.js 212 2009-09-03 05:33:44Z emartin24 $
 *
 */

$(document).ready(function () {
	$('#basic-modal input.basic, #basic-modal a.basic').click(function (e) {
		e.preventDefault();
		$('#basic-modal-content').modal();
	});
});

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

function validate_application(){

	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 = '<h4 style="color:yellow">Application was successfully sent to employer. Please wait for page to reload.</h4>';
                document.application_form.encoding = 'multipart/form-data';
                setTimeout(document.application_form.submit(),1000);
            }else{
                reloadCaptcha();
                posterror.innerHTML = ajaxRequest.responseText;
                //alert(ajaxRequest.responseText);
            }
        }
	}

    var resume      = document.application_form.applicant_resume.value;
    var captcha     = document.application_form.verification_code.value;

    var query   = '?resume='+resume;
        query  += '&captcha='+captcha;

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

    return false;
}
