/**
 * AJAX Send form
 */

/**
 * checkloggedin
 * check whether the user is logged in or not
 *
 * @param anonymous
 */
function checkloggedin(anonymous){
    if(anonymous == 1){
        //$('msg').style.left = '110px';
        //$('msg').style.top = '120px';
        $('msg').style.display = 'block';
    } else {
        $('comp_display').style.display = 'block';
    }
}


/**
 * sendForm
 * submit the form via AJAX 
 *
 * @param form
 * @param url
 * @return false - not submit the form directly
 */
function sendForm(form, url, updateDiv){
    //startLoading(updateDiv);

    var params = [];
 
    var elem = Form.getElements(form.id);
    
    // get form elements
    for(var i = 0; i < elem.length; i++){
        //only include CHECKED checkboxes
        if((elem[i].type == 'checkbox' || elem[i].type == 'radio') && !elem[i].checked){
            continue;
        }
        if(elem[i].name != ''){
            switch(elem[i].type){
                case 'select-one': //it fixes a problem in IE
                    params.push(elem[i].name + '=' + encodeURIComponent(elem[i].options[elem[i].selectedIndex].value));
                    break;
                case 'textarea':
                    if(typeof tinyMCE != 'undefined') // is there tinyMCE ?
                        if(elem[i].className == 'mceEditor') // is this textarea a mceEditor ?
                             tinyMCE.execCommand("mceRemoveEditor", false, elem[i].id);
                default:
                    params.push(elem[i].name + '=' + encodeURIComponent(elem[i].value));
            }
        }
    }

   $(updateDiv).innerHTML = 'loading...';

   new Ajax.Updater( updateDiv, url, {
        evalScripts: true,
        method: 'post',
        parameters: params.join('&'),
        onComplete: function(req){
        if(req.getHeader('X-Location')) {
            document.location = req.getHeader('X-Location');
        }
        //stopLoading(updateDiv);
        },
        onSuccess: function(req) {
             $(updateDiv).innerHTML = '';
        }
    }); 

   return false;

}

/**
 * startLoading
 * Show loading text and image
 */
function startLoading(updateDiv){
    if(!$(updateDiv)){
        var loading = document.createElement('span');
    
        with(loading){
            id = 'loadingComp';
            style.textAlign = 'center';
            style.fontSize = '14px';
            style.position = 'absolute';
            style.left = '140px';
            style.top = '180px';
            innerHTML = '<strong>Sending ...</strong><br /><br /><img src="themes/yyw/images/ajax-loader.gif" />';
        }
        $(updateDiv).style.opacity = '.2';
        $(updateDiv).style.filter = 'alpha(opacity=20)';
        $(updateDiv).innerHTML = loading;
    }
}

/**
 * stopLoading
 * Destroy loading
 */
function stopLoading(updateDiv){
    //$(updateDiv).removeChild($('loadingComp'));
    //$(updateDiv).style.opacity = '1';
    //$(updateDiv).style.filter = 'alpha(opacity=100)';
    $(updateDiv).innerHTML = '';
}

function getXmlNodeValue(xmlNode){
    return Try.these(
            function() {return xmlNode.text;},
            function() {return xmlNode.textContent;}
            );
}



function ajaxValidate(name, value, updateDiv, url) {
    
    $(updateDiv).innerHTML = 'loading...';
    new Ajax.Updater( updateDiv, url, {
        evalScripts: true,
        method: 'post',
        parameters: 'name=' + encodeURIComponent(name) + '&value=' + encodeURIComponent(value),
        onComplete: function(req){

        //stopLoading(updateDiv);
        },
        onSuccess: function(req) {
             $(updateDiv).innerHTML = '';
        }
    });

}
