var request = { getParameter: _getParameter };

function _getParameter(querystring) {
    var querystr = new Array();
    loc = window.location.search.substr(1).split('&');
    if ((loc != '') && (loc != null)) {
        for (var icnt = 0; icnt < loc.length; icnt++) {
            var q = loc[icnt].split('=');
            querystr[q[0].toLowerCase()] = q[1];
        }
        return querystr[querystring.toLowerCase()];
    } else {
        return (null);
    }
}


function ValidaEmail(FormField) {
    var x = FormField.value;
    var filter = /^[a-z0-9][\w\.-]*[a-z0-9]@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z0-9][a-z\.]*[a-z]$/;

    if (filter.test(x) == false) {
        alert('O e-mail digitado não é válido.');
        FormField.focus()
        return false;
    }
    return true;
}


function AviseMe() {
    if (document.frmaviseme.nome.value == '') {
        alert('O seu nome não foi preenchido.');
        frmaviseme.nome.focus();
    }
    else {
        if (document.frmaviseme.email.value == '') {
            alert('O seu e-mail não foi preenchido.');
            document.frmaviseme.email.focus();
        }
        else {
            document.frmaviseme.submit();
        }
    }
}


function Trim(Texto) {
    var iPosIni, iPosFim;
    iPosIni = 0;

    while ((Texto.substr(iPosIni, 1) == ' ')) {
        iPosIni++;
    }

    Texto = Texto.substr(iPosIni, Texto.length);
    iPosFim = Texto.length - 1;

    while ((Texto.substr(iPosFim, 1) == ' ')) {
        iPosFim--;
    }

    iPosFim++;
    stam = Texto.length - iPosFim;
    ntam = Texto.length - Texto.substr(iPosFim, stam).length;

    return Texto.substr(0, ntam);
}


function BrandRedirect(BrandId) {
    window.location = "http://www.sacks.com.br/site/marca.asp?id=" + BrandId;
}


function Ajax(url, method, returntag, parameters) {

    try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) {
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (ex) {
            try {
                xmlhttp = new XMLHttpRequest();
            }
            catch (exc) {
                //alert("Esse browser não tem recursos para uso do Ajax");
                xmlhttp = null;
            }
        }
    }

    url = url + '?' + Math.random();

    if (method == "GET") {
        xmlhttp.open("GET", url, true);
    } else {
        xmlhttp.open("POST", url, true);
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
        xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
        xmlhttp.setRequestHeader("Pragma", "no-cache");
    }

    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 1) {
            //document.getElementById(returntag).innerHTML='<img src="load.gif">'
        }
        if (xmlhttp.readyState == 4 && returntag != '') {
            //alert(xmlhttp.responseText);
            document.getElementById(returntag).innerHTML = xmlhttp.responseText;
        }
    }

    if (method == "GET") {
        xmlhttp.send(null);
    } else {
        xmlhttp.send(parameters);
    }

}


function Clear(ObjectId) {
    document.getElementById(ObjectId.id).value = "";
}


function PopUp(href, width, height, name, scroll) {
    var top = (screen.height - height) / 2;
    var left = (screen.width - width) / 2;
    window.open(href, name, 'scrollbars=' + scroll + ',status=0,menubar=0,resize=0,width=' + width + ',height=' + height + ',top=' + top + ',left=' + left)
}


function loadStars() {
    star1 = new Image();
    star1.src = "image/star1.gif";
    star2 = new Image();
    star2.src = "image/star2.gif";
}


function highlight(imgObj) {
    x = imgObj.id;
    y = x * 1 + 1;
    for (i = 1; i < 6; i++) {
        document.getElementById(i).src = star1.src;
    }
    for (i = 1; i < y; i++) {
        document.getElementById(i).src = star2.src;
    }
}

function losehighlight(imgObj) {
    x = imgObj.id;
    y = x * 1 + 1;
    for (i = 1; i < y; i++) {
        document.getElementById(i).src = star1.src;
    }
    x = document.getElementById('avaliacao').value;
    y = x * 1 + 1;
    for (i = 1; i < y; i++) {
        document.getElementById(i).src = star2.src;
    }
}


function setStar(imgObj, objectId) {
    if (document.getElementById(objectId).value == imgObj.id) {
        document.getElementById(objectId).value = 0
    }
    else {
        document.getElementById(objectId).value = imgObj.id;
    }
}


function SubmeteProdutoAvaliacao() {

    var idproduto = document.getElementById('idproduto').value;
    var nome = document.getElementById('nome').value;
    var email = document.getElementById('email').value;
    var avaliacao = document.getElementById('avaliacao').value;
    var opiniao = document.getElementById('opiniao').value;

    nome = escape(nome);
    opiniao = escape(opiniao);

    if (avaliacao == 0) {
        alert('Selecione a quantidade de estrelas correspondente a sua avaliação.');
        return false;
    }

    if (nome == "") {
        alert('Nome deve ser preenchido!');
        document.getElementById('nome').focus();
        return false;
    }

    if (email == "") {
        alert('E-mail deve ser preenchido!');
        document.getElementById('email').focus();
        return false;
    }

    if (!isEmail(email)) {
        alert('O e-mail não é válido!');
        document.getElementById('email').focus();
        return false;
    }

    if (opiniao == "") {
        alert('Opinião deve ser preenchida!');
        document.getElementById('opiniao').focus();
        return false;
    }


   Ajax('nocache/produto_avaliacao.asp', 'POST', 'mydiv', 'id=' + idproduto + '&nome=' + nome + '&email=' + email + '&classificacao=' + avaliacao + '&comentario=' + opiniao);

    

    return false;
}


function isEmail(email) {
    
    var filter = /^[a-z0-9][\w\.-]*[a-z0-9]@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z0-9][a-z\.]*[a-z]$/;

    if (filter.test(email) == false) {
        return false;
    }

    return true;
}

