//jquery on main page
$(document).ready(function() {
    productSlideshow();
    setInterval(function() {productSlideshow()}, 4000);
});

function productSlideshow() {
    $('#cycle').cycle({ 
        fx: 'scrollDown'
    });
}

function confirmDelete(anchor)
  {
    if (confirm('Olete kindel?'))
    {
      anchor.href += '&confirm=1';
      return true;
    }
    return false;
  }
  
function MarkeVeel(item,oLabel,maxLength) {
    
    if (maxLength - item.value.length <= 0) {
        oLabel.value = 0;
        item.value = item.value.substring(0,maxLength);
    } else {
        oLabel.value = maxLength - item.value.length;
    }
}

function toggleCheckbox(id1, id2) {
    
    var box1 = $(id1);
    var box2 = $(id2);
    
    if (!box1 || !box2)
        return false;
    
    box2.checked = false;
    
    return true;
}

function toggleSelect(id1, id2) {
    
    var id1 = $(id1);
    var id2 = $(id2);
    
    if (!id1 || !id2)
        return false;
    
    if (id1.checked == false)
        id2.disabled = 'disabled';
    else
        id2.disabled = '';
    
    return true;
}

function toggleLevelDuration() {
    
    duration = $('featured-level-duration');
    level = $('featured-level');
    
    if (!duration || !level)
        return false;
    
    e1 = $('featured-cat');
    e2 = $('featured-cat-all');
    e3 = $('featured-cat-2nd');
    e4 = $('featured-cat-2nd-all');
    
    if ((e1 == null || (e1 && e1.checked == false)) && (e2 == null || (e2 && e2.checked == false)) && (e3 == null || (e3 && e3.checked == false)) && (e4 == null || (e4 && e4.checked == false))) {
        duration.disabled = 'disabled';
        level.disabled = 'disabled';
    } else {
        duration.disabled = '';
        level.disabled = '';
    }
    
    return true;
}

function toggleHomepageDuration() {
    
    duration = $('featured-homepage-duration');
    
    if (!duration)
        return false;
        
    e = $('featured-homepage');
    
    if (e == null || (e && e.checked == false))
        duration.disabled = 'disabled';
    else
        duration.disabled = '';
    
    return true;
}

function updateFeaturedDuration(duration) {
    
    duration_l = $('featured-level-duration');
    duration_h = $('featured-homepage-duration');
    
    sel_l = duration_l.value;
    sel_h = duration_h.value;
    
    if (duration_l) {
    
        duration_l.length = 0;
        for (i = 1; i <= duration; i++) {
            duration_l.options[i-1] = new Option(i, i, (i==sel_l), (i==sel_l));
        }
    }
    
    if (duration_h) {
    
        duration_h.length = 0;
        for (i = 1; i <= duration; i++) {
            duration_h.options[i-1] = new Option(i, i, (i==sel_h), (i==sel_h));
        }
    }
    
    return true;
}

function FormatDateTime(dt) {
    return FormatDate(dt)+' '+FormatTime(dt);
}

function FormatDate(dt) {
    var y = dt.getFullYear();
    var m = dt.getMonth()+1;
    var d = dt.getDate();
    if (m < 10) m = '0'+m;
    if (d < 10) d = '0'+d;
    return d+'.'+m+'.'+y;
}

function FormatTime(dt) {
    var h = dt.getHours();
    var n = dt.getMinutes();
    if (h < 10) h = '0'+h;
    if (n < 10) n = '0'+n;
    return h+':'+n;
}

function SetupDatePreview() {
    var oForm = document.getElementById('sell-form');
    if (oForm.duration)
        oForm.duration.onchange = updateDatePreview;
    oForm.start_date.onchange = updateDatePreview;
    oForm.start_time.onchange = updateDatePreview;
    updateDatePreview();
}

function PictureView(url) {

	OpenWin(url, 640, 460, 'pictureView', 'top=0,left=0,location=no,menubar=no,scrollbars=yes,status=yes,resizable=yes', true);

	return false;
}

function CharsLeft(item,oLabel,maxLength) {
    
    if (maxLength - item.value.length <= 0) {
        oLabel.value = 0;
        item.value = item.value.substring(0,maxLength);
    } else {
        oLabel.value = maxLength - item.value.length;
    }
}

function ExpandTextarea(src, target, rows) {
    target.rows = rows;
    src.style.display = 'none';
}
function checkLen(str, limit, err1) {
    if (str.length > limit) {
        alert(err1);
        return false;
    }
    return true;
}

function switchShipping(state) {
    
    if ($('ship-cost-buyer'))
        $('ship-cost-buyer').disabled = state;
    if ($('ship-cost-seller'))
        $('ship-cost-seller').disabled = state;
    
    return true;
}
function updateShipping() {
    
    elem = $('ship-local');
    
    if (elem && elem.checked)
        switchShipping(true);
    else
        switchShipping(false);
    
    elem2 = $('ship-country');
    if (elem2 && !elem2.checked) {
        
        if (hrx = $('ship-courier-hrx')) {
            $('ship-courier-hrx').checked = '';
            updateHRX();
        }
    }
    
    return true;
}
function updateShipbyOtherChk(oTxt) {
    document.getElementById('shipby-other').checked = (oTxt.value.length != 0);
    return true;
}
function updateShipbyOtherTxt(oChk) {
    if (!oChk.checked)
        oChk.form.shipby_other.value = '';
    return true;
}
function updateHRX() {
    
    elem = $('ship-courier-hrx');
    
    if (elem && elem.checked)
        switchHRX(true);
    else
        switchHRX(false);
    
    return true;
}
function switchHRX(state) {
    
    if ($('hrx_details')) {
        $('hrx_details').style.display = state ? '' : 'none';
    }
    
    if (state == true && $('ship-country'))
        $('ship-country').checked = 'checked';
    
    return true;
}

function updateHRXCities(elem, county_id) {
    
    var city = $(elem);
    
    city.options.length = 1;
    city.options[0] = new Option('---', '0');
    if (county_id > 0 && cities[county_id]) {
    
        for (i = 1; i <= cities[county_id].length; i++) {
            city.options[i] = new Option(cities[county_id][i-1][0], cities[county_id][i-1][1]);
        }
    }
}

function selectHRXCity(elem, val) {
    
    var city = $(elem);
    
    if (val > 0) {
        for (i = 1; i <= city.options.length; i++) {
            if (city.options[i].value == val) {
                city.options[i].selected = 'selected';
                break;
            }
        }
    }
}

