function getXMLHTTPRequest()
{
    var request = false;
    try
    {
        request = new XMLHttpRequest(); /* e.g. Firefox */
    }
    catch(err1)
    {
        try
        {
            vrequest = new ActiveXObject("Msxml2.XMLHTTP");
        /* some versions IE */
        }
        catch(err2)
        {
            try
            {
                request = new ActiveXObject("Microsoft.XMLHTTP");
            /* some versions IE */
            }
            catch(err3)
            {
                request = false;
            }
        }
    }
    return request;
}

var myRequest = getXMLHTTPRequest();

function checkUsername(username) {
    // build the URL of the server script we wish to call
    var url = "available.php?username=" + username;
    // ask our XMLHTTPRequest object to open a server connection
    myRequest.open("GET", url, true);
    // prepare a function responseAjax() to run when the response has arrived
    myRequest.onreadystatechange = responseCheckUsername;
    // and finally send the request
    myRequest.send(null);
    document.getElementById("checkicon").setAttribute("class", "checking");
    document.getElementById("checkicon").setAttribute("src", "images/refreshicon.gif");
}

function responseCheckUsername() {
    if(myRequest.readyState == 4) {
        // if server HTTP response is "OK"
        if(myRequest.status == 200) {
            if(myRequest.responseText == "yes"){
                document.getElementById("checkicon").setAttribute("class", "suc");
                document.getElementById("checkicon").setAttribute("src", "images/okicon.gif");
            }
            else{
                document.getElementById("checkicon").setAttribute("class", "fail");
                document.getElementById("checkicon").setAttribute("src", "images/noicon.gif");
            }
        }
        else {
    // issue an error message for any
    // other HTTP response
    }
    }

}

function resetCheckUsernamePicture(){
    document.getElementById("checkicon").setAttribute("class", "avail");
    document.getElementById("checkicon").setAttribute("src", "images/checkb.gif");
}

function select_text()
{
    var content=eval("document.quickPlayForm.username");
    content.focus();
    content.select();
}

function clickclear(thisfield, defaulttext) {
    if (thisfield.value == defaulttext) {
        thisfield.value = "";
    }
}
function clickrecall(thisfield, defaulttext) {
    if (thisfield.value == "") {
        thisfield.value = defaulttext;
    }
}

//checks if date is OK
function checkDate(day, month, year){
    // This instruction will create a date object
    switch(month){
        case '1':case '3':case '5':case '7':case '8':case '10':case '12':
            if(day<=31 && day>=1) return true;
            else{
                alert("Wrong date!");
                return false;
            }
            break;
        case '4':case '6':case '9':case '11':
            if(day<=30 && day>=1) return true;
            else{
                alert("Wrong date!");
                return false;
            }
            break;
        case '2':
            if(day<=29 && day>=1 && year%4==0 && (year%400==0 || year%100!=0)) return true;
            else if(day<=28 && day>=1) return true;
            else{
                alert("Wrong date!");
                return false;
            }
            break;
        default:
            return true;
            break;
    }
    return true;
}

function goodbye(e) {
    if(!e) e = window.event;
    //e.cancelBubble is supported by IE - this will kill the bubbling process.
    e.cancelBubble = true;
    e.returnValue = 'Are you sure you want to leave?\nYour will not be able to finish the game!'; //This is displayed on the dialog

    //e.stopPropagation works in Firefox.
    if (e.stopPropagation) {
        e.stopPropagation();
        e.preventDefault();
    }
}

var RecaptchaOptions = {
    theme : 'custom'
};

function callFeedback(){
    $.blockUI({
        message: $('#feedback_form'),
        css: {
            top: '10%',
            backgroundColor: '#1f1f1f',
            color: '#D7AC00',
            border:'3px solid #D7AC00'
        }
    });
    //$('#close_feedback').attr('title','Click to unblock').click($.unblockUI);
    $('#close_feedback').attr('title','Click to close').click(closeFeedback);
    $('#aplet').css('visibility', 'hidden');
}


function submitContactForm(){
    //Get the data from all the fields
    var name = $('input[name=name]');
    var email = $('input[name=email]');
    var subject = $('input[name=subject]');
    var text = $('textarea[name=text]');
    var recaptcha_response_field = $('input[name=recaptcha_response_field]');

    var recaptcha_challenge_field = $('#recaptcha_challenge_field').val()
    var data;
    //organize the data properly
    data = 'name=' + name.val() + '&email=' + email.val() + '&subject='
    + subject.val() + '&text='  + encodeURIComponent(text.val()) + '&recaptcha_response_field=' + recaptcha_response_field.val()+'&ajax=1&submit=1&recaptcha_challenge_field='+recaptcha_challenge_field;
    //start the ajax
    $.ajax({
        //this is the php file that processes the data and send mail
        url: "contact.php",

        //GET method is used
        type: "POST",

        //pass the data
        data: data,

        //Do not cache the page
        cache: false,

        //success
        success: formSuccided
    });
    
    
    $.blockUI();
    // !!! Important !!!
    // always return false to prevent standard browser submit and page navigation
    return false;
    
}

function submitContactFormReg(){
    //Get the data from all the fields
    var subject = $('input[name=subject]');
    var text = $('textarea[name=text]');
    var recaptcha_response_field = $('input[name=recaptcha_response_field]');

    var recaptcha_challenge_field = $('#recaptcha_challenge_field').val()
    var data;
    //organize the data properly
    data = 'subject='+ subject.val() + '&text='  + encodeURIComponent(text.val()) + '&recaptcha_response_field=' + recaptcha_response_field.val()+'&ajax=1&submit=1&recaptcha_challenge_field='+recaptcha_challenge_field;
   
    //start the ajax
    $.ajax({
        //this is the php file that processes the data and send mail
        url: "contact.php",

        //GET method is used
        type: "POST",

        //pass the data
        data: data,

        //Do not cache the page
        cache: false,

        //success
        success: formSuccided
    });


    $.blockUI();
    // !!! Important !!!
    // always return false to prevent standard browser submit and page navigation
    return false;

}

function closeFeedback(){
    $.unblockUI();
    $('#aplet').css('visibility', 'visible');
}


function formSuccided(responseText){
    if(responseText=='1')
        thankYouForSumbitting();
    else{
        $('#feedback_error').text(responseText);
        callFeedback();
    }
    Recaptcha.reload();
}

function thankYouForSumbitting(){
    $.blockUI({
        message: $('#feedback_thankyou'),
        css: {
            backgroundColor: '#1f1f1f',
            color: '#D7AC00',
            border:'3px solid #D7AC00'
        }
    });
    //$('#close_feedback').attr('title','Click to unblock').click($.unblockUI);
    $('#close_feedback2').attr('title','Click to close').click(closeFeedback);
    $('#aplet').css('visibility', 'hidden');
}