// forces IE to cache background images, need this for background image swap (even for sprited images)
try {
    document.execCommand('BackgroundImageCache', false, true);
} catch(err) { }


// trim strings
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}



// input box, removes default example phrase
function checkInput(input,checkFor,replaceWith) {
    if (input.value == checkFor) {
        input.value = replaceWith;
    }
}


// ajax
var Ajax = new Object();
Ajax.isUpdating = true;
Ajax.Request = function(method, url, callback) {
    this.isUpdating = true;
    this.callbackMethod = callback;
    this.request = (window.XMLHttpRequest)? new XMLHttpRequest(): new ActiveXObject("MSXML2.XMLHTTP"); 
    this.request.onreadystatechange = function() { Ajax.checkReadyState(); };
    this.request.open(method, url, true);
    this.request.send(url);
}
Ajax.checkReadyState = function(_id) {
    switch(this.request.readyState) {
        case 1: break;
        case 2: break;
        case 3: break;
        case 4:
            this.isUpdating = false;
            this.callbackMethod(this.request);
    }
}



var submittingQuestion = false;
function submitMyQuestion() {

    if(!submittingQuestion) {
        
        var passed = true;
        
        var myQuestion = document.getElementById('myQuestion');
        var myEmail = document.getElementById('myEmail');
        
        if (myQuestion.value.trim() == '' || myQuestion.value == 'Ask question here ...') {
            myQuestion.style.color = 'red';
            passed = false;
        }
        if (myEmail.value.trim() == '' || myEmail.value == 'Insert email here') {
            myEmail.style.color = 'red';
            passed = false;
        }
        
        if (passed) {
        
            submittingQuestion = true;
        
            var submitButton = document.getElementById('mySubmit');
            submitButton.style.background = 'white url(/img/footer/ajax-loader.gif) no-repeat center center';
            submitButton.style.cursor = 'normal';
            
            myQuestion.disabled = true;
            myEmail.disabled = true;
            
            Ajax.Request('GET','/rpc.php?method=emailQuestion&url='+encodeURIComponent(location.href)+'&email='+encodeURIComponent(myEmail.value)+'&question='+encodeURIComponent(myQuestion.value),questionResponse);
            
                        
        }
        
    }
    
}


function questionResponse(o) {
    if(o.readyState == 4) {
        var response = eval('('+o.responseText+')');
        
        if(response.status == 'pass') {
                submittingQuestion = false;
                
                var submitButton = document.getElementById('mySubmit');
                var myQuestion = document.getElementById('myQuestion');
                var myEmail = document.getElementById('myEmail');
                var myThankYou = document.getElementById('myThankYou');
                
                myThankYou.style.display = "block";
                
                submitButton.style.background = 'white url(/img/footer/submit.gif) no-repeat center center';
                submitButton.style.cursor = 'pointer';
                
                myQuestion.disabled = false;
                myEmail.disabled = false;
                
                myQuestion.value = 'Ask question here ...';
                myEmail.value = 'Insert email here';
        }
        else {
            // what to do if it fails?
            alert("There was a problem submitting your question, please refresh your browser and try again.");
        }
        
    }
    else {
        
            alert("There was a problem submitting your question, please refresh your browser and try again.");
    }
}
