var _URL_ROOT = getDocRoot();

function sleep(milliseconds) 
{
 var start = new Date().getTime();
 
 for (var i = 0; i < 1e7; i++)
 	if ((new Date().getTime() - start) > milliseconds)
 		break;
}


function revertElementBG(form, refresh)
{
    form.setAttribute('style', 'background-color: inherit;');
    if((typeof(refresh) != "undefined") && (refresh == true)) window.location.href += "";
}

function checkForm(f)
{
    var champs = f.elements;
    var result = true;

    for(var i=0, n=champs.length; i<n; i++)
    {
        if(champs[i].value == '')
        {
            champs[i].setAttribute('style', 'background-color: #ffaaaa; font-weight: bold;');
            alert("Le champs " + champs[i].name + " ne peut être vide.", "Erreur !");
            result = false;
            break;
        }

    }
    return result;
}

function Rowyn_Update_Record(form, disable_check, force_get)
{

    var m = "POST";

    if(isset(force_get) && (force_get == true))
        m = "GET";

    if(((typeof(disable_check) != "undefined") && (disable_check == true))|| checkForm(form))
        ROWYN5_AJAX.Request(_URL_ROOT + 'ajax/updaterecord/',
                                        {
                                            method: m,
                                            Data: ROWYN5_Form.toURL(form),
                                            onSuccess: function(xhr) 
                                                                    {
                                                                        //var bgo = form.style.background;

                                                                        if(xhr.responseText.toString().match( new RegExp(/ER/) ))
                                                                            form.setAttribute('style', 'background-color: red;');
                                                                        else if(xhr.responseText.toString().match( new RegExp(/OK/) ))
                                                                            form.setAttribute('style', 'background-color: green;');
                                                                        else
                                                                            form.setAttribute('style', 'background-color: orange;');
                                                                        //form.style.background = '#009900';

                                                                        window.setTimeout(function(){revertElementBG(form, true)}, 1000);
                                                                    }
                                        }


                       );
}


function Rowyn_Delete_Record(code)
{
    if((typeof(code) != "undefined"))
    {
    	var answer = confirm("Etes-vous certain(e) de vouloir supprimer cet enregistrement ?");
    	
        if(answer)
        	ROWYN5_AJAX.Request( _URL_ROOT + 'ajax/deleterecord/',
                                        {
                                            method: "GET",
                                            Data: "__oodat__=" + code,
                                            onSuccess: function() {window.status = "Suppresion effectue avec succes";window.location.href += "";}
                                        }


                       );
        else
        	alert("Supression annule !");
    }
}

function Rowyn_Create_Record(table)
{
    if(typeof(table) != "undefined") 
    {
        ROWYN5_AJAX.Request( _URL_ROOT + 'ajax/createrecord/',
                                        {
                                            method: "GET",
                                            Data: "__token__=" + table,
                                            onSuccess: function() {window.status = "Creation effectue avec succes";window.location.href += "";}
                                        }


                       );
    }
}

function Rowyn_Update_SessionLang(lng)
{
    if(typeof(lng) != "undefined")
    {
        ROWYN5_AJAX.Request( _URL_ROOT + 'index/ajxsetsesslang/',
                                        {
                                            method: "GET",
                                            Data: "l=" + lng,
                                            onSuccess: function() {window.location.href = _URL_ROOT + 'index/index';}
                                        }


                       );
    }
}
