var current_id = "link_thumb_0";

function get(id){
    return document.getElementById(id);
}

function selectZoom(src,thumb){
    current_id = thumb.id;
    get('zoom').src = src;
    for(i = 0 ; get('link_thumb_'+i) != null ; i++){
        get('link_thumb_'+i).className = "thumb";
    }
    thumb.className = "thumb select";
}

function simulateClick(id) {
    var cb = get(id);
    if (cb.dispatchEvent) {
        var evt = document.createEvent("MouseEvents");
        evt.initEvent("click", true, true);
        cb.dispatchEvent(evt);
    } else {
        cb.click();
    }
}

function simulateClickMultibox () {

}

function confirmDeleteAddress(id){
    if(confirm(confirmDeleteAddressText)){
        window.location="address_book_process.php?action=deleteconfirm&delete="+id;
    }
}

function checkSearch(){
    var err = "";

    if(!get("keywords").value && !get("pfrom").value && !get("pto").value){
        err+="Veullez remplir au moins un des critères de recherche."+"\n";
    }else{
        if(get("pfrom").value && (!isNumeric(get("pfrom").value ) || get("pfrom").value <= 0)){
            err+="Le prix minimum doit être un nombre positif."+"\n";
        }
        if(get("pto").value && (!isNumeric(get("pto").value ) || get("pto").value <= 0)){
            err+="Le prix maximum doit être un nombre positif."+"\n";
        }
    }
    if(err){
        alert(err);
        return false;
    }else{
        return true;
    }
}

function checkConcours(){
    var err="";

    if (!get("name").value) err+="Veuillez préciser votre nom.\n";
    if (!get("prenom").value) err+="Veuillez préciser votre prénom.\n";
    if (!get("adresse").value) err+="Veuillez préciser votre adresse.\n";
    if (!get("code_postal").value) err+="Veuillez préciser votre code postal.\n";
    if (!get("ville").value) err+="Veuillez préciser votre ville.\n";
    if (!get("emailJeux").value){
        err+="Veuillez préciser votre e-mail.\n";
    }else if (!isEmail(get("emailJeux").value)) {
        err+="L'email n'est pas valide.\n";
    }

    if (get('sondage').value==0) err+="Merci de préciser comment vous avez connu ce site.\n";

    if(err){
        alert(err);
        return false;
    }else{
        get("formJeuConcours").value='formJeuConcours';
        get("formJeux").submit();
        return true;
    }

}

function deleteProduct(id){
    get('cart_delete').value = id;
    get('cart_quantity').submit();
}

function isNumeric(str){
    var re = /[\D]/g
    if (re.test(str)) return false;
    return true;
}

// returns true if the string is a valid email
function isEmail(str){
    var re = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/
    return re.test(str);
}


function getDim(){

    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;

    if (self.innerHeight) {	// all except Explorer
        if(document.documentElement.clientWidth){
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }

    return [pageWidth,pageHeight];
}



function CountBack(limit) {
    for (i=0;i<=limit;i++) {
        if(!document.getElementById("hdncntdwn"+i)) continue;
        secs = parseInt(document.getElementById("hdncntdwn"+i).value);

        
        DisplayStr = '';
        if (secs>86400) DisplayStr = Math.floor(secs/86400).toString() + 'j ';
        DisplayStr = DisplayStr + calcage(secs+xdif,3600,24) + "h " + calcage(secs+xdif,60,60) + "m " + calcage(secs+xdif,1,60) + "s";
        document.getElementById("cntdwn"+i).innerHTML = DisplayStr;
        
    }
    xdif-=1;
    setTimeout("CountBack("+limit+")", 1000);
}

function calcage(secs, num1, num2) {
    strCalcage = ((Math.floor(secs/num1))%num2).toString();
    if (strCalcage.length < 2) strCalcage = "0" + strCalcage;
    return strCalcage;
}



$(function(){
    $('#keywords').autocomplete({
        source: "ajax_search.php",
        minLength: 4,
        select: function( event, ui ) {
        window.location = ui.item.url;
        return false;
        }
        }).data("autocomplete")._renderItem = function( ul, item ) {
        return $( "<li></li>" )
        .data( "item.autocomplete", item )
        .append( "<a>"+ item.label + "</a>" )
        .appendTo( ul );
    };

});
