function strstr (haystack, needle, bool) {
    // Finds first occurrence of a string within another
    //
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/strstr    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: strstr('Kevin van Zonneveld', 'van');
    // *     returns 1: 'van Zonneveld'    // *     example 2: strstr('Kevin van Zonneveld', 'van', true);
    // *     returns 2: 'Kevin '
    // *     example 3: strstr('name@example.com', '@');
    // *     returns 3: '@example.com'
    // *     example 4: strstr('name@example.com', '@', true);    // *     returns 4: 'name'
    var pos = 0;

    haystack += '';
    pos = haystack.indexOf(needle);if (pos == -1) {
        return false;
    } else {
        if (bool) {
            return haystack.substr(0, pos);} else {
            return haystack.slice(pos);
        }
    }
}

function str_replace (search, replace, subject, count) {
    // Replaces all occurrences of search in haystack with replace
    //
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/str_replace    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    // %          note 1: The count parameter must be passed as a string in order    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'    var i = 0,
        j = 0,
        temp = '',
        repl = '',
        sl = 0,        fl = 0,
        f = [].concat(search),
        r = [].concat(replace),
        s = subject,
        ra = r instanceof Array,        sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
    for (i = 0, sl = s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }for (j = 0, fl = f.length; j < fl; j++) {
            temp = s[i] + '';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {this.window[count] += (temp.length - s[i].length) / f[j].length;
            }
        }
    }
    return sa ? s : s[0];}

function box( elemID )
{
    var elem = document.getElementById( elemID );
    if( elem.style.visibility != 'visible' )
    {
        elem.style.visibility = 'visible';
    }
    else
    {
        elem.style.visibility = 'hidden';
    }
}

function product( elemID )
{
    var elem = document.getElementById( elemID );
    if( elem.style.visibility != 'visible' )
    {
        elem.style.visibility = 'visible';
        var product = document.getElementById("productrefa");
		product.id = "productrefaopen";
        
    }
    else
    {
        elem.style.visibility = 'hidden';
        var product = document.getElementById("productrefaopen");
		product.id = "productrefa";
    }
}

function areapessoalEditProduct( elemID, prodID )
{
    if( document.getElementById('product_'+elemID+'_editar_tr').className == 'carrinhoedit' )
    {
        /** Aparece o Editar **/

        //Agarra no valor que lá estava
        var valor = document.getElementById('product_'+elemID+'_quantidade').innerHTML;
        //Altera o content para uma input type text
        document.getElementById('product_'+elemID+'_quantidade').innerHTML = '<input type="text" name="edit_product_'+elemID+'_quantidade" id="quantityfield" class="quantityinput roundedinput" value="'+valor+'" />';
        //Altera o lapis para um visto
        document.getElementById('product_'+elemID+'_editar_tr').className = 'carrinhoaccept';
        //Altera de "Editar" para "Aceitar"
        document.getElementById('product_'+elemID+'_editar_text').innerHTML = 'Aceitar';
    }
    else
    {
        /** Remove o Editar **/

        //Agarra no valor que lá estava
        var valor = document.getElementsByName('edit_product_'+elemID+'_quantidade')[0].value;
        //Altera o content para uma string
        document.getElementById('product_'+elemID+'_quantidade').innerHTML = valor;
        //Altera o visto para um lapis
        document.getElementById('product_'+elemID+'_editar_tr').className = 'carrinhoedit';
        //Altera de "Aceitar" para "Editar"
        document.getElementById('product_'+elemID+'_editar_text').innerHTML = 'Editar';
        //Salvar a edição do produto na base de dados...
        jQuery.ajax(
        {
            url: '/lib/ajax_requests.php?status=editQtd&item='+prodID+'&val='+valor,
            data: prodID,
            type: 'POST',
            dataType: 'html',

            success: function(msg)
            {
                window.location.href = location;
            }
        });
    }
}

function deleteFromCart( elemID, prodID, norefresh )
{
    //Get the params & transform them
    var number = elemID;
    var parentToHide = 'product_'+number+'_main_container';
    if( document.getElementById('product_'+elemID+'_remove_tr').className == "carrinhoaccept" )
    {
        /** Enables **/

        //Apply <tr> a style.
        document.getElementById(parentToHide).style.color = "";
        //Change the DELETE button image.
        document.getElementById('product_'+elemID+'_remove_tr').className = "carrinhoremo";
        //Show the Edit-Image
        document.getElementById('product_'+elemID+'_editar_tr').style.visibility = "visible";
        jQuery.ajax(
        {
            url: '/lib/ajax_requests.php?status=enable&item='+prodID,
            data: prodID,
            type: 'POST',
            dataType: 'html',

            success: function(msg)
            {
                window.location.href = location;
            }
        });
    }
    else
    {
        /** Disables **/

        if( document.getElementById('product_'+elemID+'_editar_tr').className != 'carrinhoedit' )
        {
            /** Remove o Editar **/

            //Agarra no valor que lá estava
            var valor = document.getElementsByName('edit_product_'+elemID+'_quantidade')[0].value;
            //Altera o content para uma string
            document.getElementById('product_'+elemID+'_quantidade').innerHTML = valor;
            //Altera o visto para um lapis
            document.getElementById('product_'+elemID+'_editar_tr').className = 'carrinhoedit';
            //Altera de "Aceitar" para "Editar"
            document.getElementById('product_'+elemID+'_editar_text').innerHTML = 'Editar';
            //Salvar a edição do produto na base de dados...
        }
        
        //Apply <tr> a style.
        document.getElementById(parentToHide).style.color = "#CcCcCc";
        //Change the DELETE button image.
        document.getElementById('product_'+elemID+'_remove_tr').className = "carrinhoaccept";
        //Hide the Edit-Image
        document.getElementById('product_'+elemID+'_editar_tr').style.visibility = "hidden";
        jQuery.ajax(
        {
            url: '/lib/ajax_requests.php?status=disable&item='+prodID,
            data: prodID,
            type: 'POST',
            dataType: 'html',

            success: function(msg)
            {
                if( norefresh == true )
                    {
                        window.location.href = location;
                    }
            }
        });
    }
}

function disableNewUserMoradaDeEntrega()
{
    //$("#morada_facturacao-table").toggle( 100 );
    if( document.getElementById('deactivateMoradaDeEntrega').value == "sim" )
    {
        document.getElementById('moddadosnome').value = document.getElementById('moddadosnome2').value;
        document.getElementById('moddadosnif').value = document.getElementById('moddadosnif2').value;
        document.getElementById('moddadosmorada').value = document.getElementById('moddadosmorada2').value;
        document.getElementById('moddadosporta').value = document.getElementById('moddadosporta2').value;
        document.getElementById('moddadosandar').value = document.getElementById('moddadosandar2').value;
        document.getElementById('moddadoscp').value = document.getElementById('moddadoscp2').value;
        document.getElementById('moddadoslocalidade').value = document.getElementById('moddadoslocalidade2').value;
    }
}

function enableNewUserMoradaDeEntrega()
{
    //$("#morada_facturacao-table").toggle( 100 );
    
}

function copyDataToMorada( fieldName )
{
    var prefix = "2";
    if( document.getElementById('deactivateMoradaDeEntrega').value == "sim" )
    {
        document.getElementById(fieldName).value = document.getElementById(fieldName+prefix).value;
    }
}

//Menu dos produtos
function OpenFamily( key, inc )
{
    var text = "child_"+key+"_";
    if( inc == undefined || inc == null )
    {
        var inc = 0;
    }
    
    if( document.getElementById( text + inc ) != null || document.getElementById( text + inc ) != undefined )
    {
        $("#"+text+inc).toggle( 200 );
        inc++;
        OpenFamily( key, inc );
    }
}

function HighLightSubFamily( string, parent )
{
    if( string == null || string == undefined )
    {
        return false;
    }
    else
    {
        parent = 'parent_'+parent;
        if( string != null && string != undefined )
        {
            //parent = 'parent_'+parent;
            var familiaID = document.getElementsByName(parent)[0].parentNode.getAttribute('id');
            OpenFamily(familiaID,0);
        }
        document.getElementById( familiaID ).className = "selected rounded5";
        if( string !== "list" )
        {
            string = 'subChild_'+string;
            var subfamiliaID = document.getElementsByName(string)[0].parentNode.getAttribute('id');
            document.getElementById( subfamiliaID ).className = "selectedchild rounded5";
        }
        return true;
    }
}

function alterarDadosPessoais()
{
    $("#rightcontentwrapper").toggle( 200 );
    $("#rightcontentwrapper2").toggle( 200 );
}
