﻿///<reference path="../scripts/jquery-1.4.1-vsdoc.js" />
///<reference path="../scripts/json2.js" />
///<reference path="../scripts/jquery.functions.js" />

function showChDDL(checkBoxClient, ddlClientReplace, ddlClient) {
    $(checkBoxClient).show();
    $(ddlClientReplace).show();
    $(ddlClient).hide();
}

function setSelectedAmount(checkBoxClient, ddlClientReplace, ddlClient) {

    var selectedAmount = 0;

    $(checkBoxClient + " input[type=checkbox]").each(function(i) {
        if (typeof this.checked != "undefined") {
            if (this.checked === true){ selectedAmount++; }
        }
    });

    //clear the dropdown
    $(ddlClient + ">option").remove();
    var s = (window['sel_lit'] || "Selected");
    var s2 = (window['sel2_lit'] || "Select");
    if (selectedAmount === 0) {
        $(ddlClient).append($('<option></option>').val(' ' + s2 + '...').html(' ' + s2 + '...'));
    } else {
        $(ddlClient).append($('<option></option>').val(' ' + selectedAmount + ' ' + s).html(' ' + selectedAmount + ' ' + s));
    }
}
function hideChDDL(checkBoxClient, ddlClientReplace, ddlClient) {
    $(checkBoxClient).hide();
    $(ddlClientReplace).hide();
    $(ddlClient).show();
    setSelectedAmount(checkBoxClient, ddlClientReplace, ddlClient);
}
function ClearAllControls(holder) {
    $(holder || "form").find(":input:not(:button,:submit,:radio)").each(function(i, el) {
        el = $(el);
        if (el.is(":checkbox")) {
            el.attr("checked", "");
        } else {
            el.val("");
        }
    });
}

function AddToBookmark(url, title) {
    if (!title) { title = document.title; }
    if (!url) { url = window.location; }
    if (document.all) {
        window.external.AddFavorite(url, title);
    } else if (window.sidebar) {
        window.sidebar.addPanel(title, url, "");
    } else if (window.sidebar && window.sidebar.addPanel) {
        window.sidebar.addPanel(title, url, "");
    }
}

function doSaveAs() {
    if (document.execCommand) {
        document.execCommand("SaveAs");
    }
    else {
        alert("Save-feature available only in Internet Exlorer 5.x.");
    }
}

function AddToBookmarkSetHome() {
    if (document.all) {
        document.body.style.behavior = 'url(#default#homepage)';
        document.body.setHomePage(window.location);

    }
    else if (window.sidebar) {
        if (window.netscape) {
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch (e) {
                alert('this action was aviod by your browser，if you want to enable，please enter about:config in your address line,and change the value of     signed.applets.codebase_principal_support to true');
            }
        }
        var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
        prefs.setCharPref('browser.startup.homepage', window.location);
    }
}

function toggleRowItem(row) {
    if (row.style.display == 'none') {row.style.display = ''; } else {row.style.display = 'none'; }
}

function getNextSibling(startBrother) {
    endBrother = startBrother.nextSibling;
    while (endBrother.nodeType != 1) {
        endBrother = endBrother.nextSibling;
    }
    return endBrother;
}

function setRange(dropdown, txtFrom, txtTo) {

    var value = dropdown.options[dropdown.selectedIndex].value;
    var items = value.split("&");
    document.getElementById(txtFrom).value = items[0];
    document.getElementById(txtTo).value = items[1];
}

function hasClass(ele, cls) {
    //return ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
    return jQuery(ele).hasClass(cls);
}

function addClass(ele, cls) {
    //if (!this.hasClass(ele, cls)) ele.className += " " + cls;
    jQuery(ele).addClass(cls);
}

function removeClass(ele, cls) {
    jQuery(ele).removeClass(cls);
}

function setItemPrintable(items, isPrintable) {
    if (isPrintable) {
        jQuery(items).addClass("toPrint");
    } else {
        jQuery(items).removeClass("toPrint");
    }
}
function openPopup(page, width, heigth) {
    var w = 800, h = 600;

    try {
        w = screen.width;
        h = screen.height;
    } catch (err) {
        w = 800;
        h = 600;
    }

    var popW = width, popH = heigth;

    var leftPos = (w - popW) / 2, topPos = (h - popH) / 2;
    var url = encodeURI(page);
    var newWindow = window.open(url, '', 'width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos + ', statusbar=0 , scrollbars=1 , menubar=0');

    if (newWindow !== null) { newWindow.focus(); }
}
function openEmail(params) {
    openPopup('../Email/Default.aspx?' + params, 697, 570);
}

function openCert(url) {
    openPopup(url, 1000, 860);
}

function stopBubble(e) {
    if (!e) { return; }
    if (e.stopPropagation) {
        e.stopPropagation();
    }
    e.cancelBubble = true;
}

function togglePrints(isPrintable) {
    setItemPrintable("Image", isPrintable);
    setItemPrintable("Details", isPrintable);

    if (isPrintable) {
        document.getElementById('chInfo').checked = 'checked';
        document.getElementById('chImg').checked = 'checked';
    } else {

        document.getElementById('chInfo').checked = '';
        document.getElementById('chImg').checked = '';

    }
}

function toggleEmail(isPrintable) {

    if (isPrintable) {
        document.getElementById('chImgEmail').checked = 'checked';
        document.getElementById('chInfoEmail').checked = 'checked';
    } else {
        document.getElementById('chImgEmail').checked = '';
        document.getElementById('chInfoEmail').checked = '';
    }
}

function toggleChecks(listTable, b) {
    $("#" + listTable.id + " input[type=checkbox]").each(function(i) {
        if (typeof this.checked != "undefined") {
            if (b === null) {
                this.checked = (!this.checked);
            }
            else {
                this.checked = b;
            }
        }
    });
}

function toggleChecksBoxes(className, b) {
    $("." + className + " input[type=checkbox]").each(function(i) {
        if (typeof this.checked != "undefined") {
            if (b === null) {
                this.checked = (!this.checked);
            }
            else {
                this.checked = b;
            }
        }
    });
}
function toggleChecksBoxesIn(sel, b) {
    var inp = $(sel).find("input[type=checkbox]");
    if (b === undefined) { b = !$(sel).data("tglchk"); }
    inp.each(function(i, el) {
        $(el).attr("checked", b);
    });
    $(sel).data("tglchk", !!b);
}
function selectRow(parcel, state, holder) {
    var hdn = $(holder);
    if (!hdn.length) { return; }
    var val = hdn.val();
    if (parcel) {
        parcel = ',' + parcel + ',';
        val = val.replace(parcel, '');
        if (state) {
            val += parcel;
            hdn.val(val);
        } else {
            hdn.val(val);
        }
    }
    //    var count = 0;
    //    var items = val.split(",,");
    //    if (items && items.length && items[0]) {
    //        count = items.length;
    //    }
    //    $("#selCount").text("Selected items: " + count);
}
function toggleChecksRows(className, b) {
    $("." + className + " input[type=checkbox]").each(function(i, el) {
        if (typeof this.checked != "undefined") {
            var ttl = el.title;
            if (!ttl) ttl = el.parentNode.title;
            if (b === null) {
                this.checked = (!this.checked);
                selectRow(ttl, this.checked, holderId);
            }
            else {
                this.checked = b;
                selectRow(ttl, this.checked, holderId);
            }
        }
    });
}

function toggleVisibility(className) {
    $("." + className + " ").toggle();
}

function scrollToView(id) {
    document.getElementById(id).scrollIntoView();
}

function changeRadioColor(idColor, idFancy, isColor) {
    if (isColor === true) {
        document.getElementById(idColor).checked = true;
        document.getElementById(idFancy).checked = false;
    } else {
        document.getElementById(idColor).checked = false;
        document.getElementById(idFancy).checked = true;
    }
}
function plusMinus(obj) {
    obj = $(obj);
    if (obj.length) {
        var src = obj.attr("src");
        if (src.indexOf("plus") > 0) {
            src = src.replace("plus", "minus");
        } else if (src.indexOf("minus") > 0) {
            src = src.replace("minus", "plus");
        }
        obj.attr("src", src);
    }
}

function AnchorPosition_getPageOffsetLeft(el) {
    var ol = el.offsetLeft;
    while ((el = el.offsetParent) !== null) { ol += el.offsetLeft; }
    return ol;
}
function AnchorPosition_getPageOffsetTop(el) {
    var ot = el.offsetTop;
    while ((el = el.offsetParent) !== null) { ot += el.offsetTop; }
    return ot;
}
function diamond_colors() {
    var o = document.getElementById('sel_colors');
    var top = AnchorPosition_getPageOffsetTop(o);
    var left = AnchorPosition_getPageOffsetLeft(o);

    var divtag = document.getElementById('colors');

    if (divtag.style.display == 'none') {
        divtag.style.display = 'block';
        //alert(left);
        divtag.style.left = left + 'px';
        top = top + 20;
        divtag.style.top = top + 'px';
        divtag.style.position = 'absolute';
    }
    else if (divtag.style.display == 'block') {
        divtag.style.display = 'none';
        return false;
    }

}
/*From Main Filter*/
function searchin_advance() {
    var frm = document.search_diamonds;
    var adv = document.getElementById('advancedsearch');
    if (!adv) { return; }
    if (adv.style.display == 'none') {
        adv.style.display = 'block';
        $("#LinkAdviceSearch").toggle();
        return false;
    } else if (adv.style.display == 'block') {
        adv.style.display = 'none';
        return false;
    }

}
function close_advancedsearch() {
    var adv = document.getElementById('advancedsearch');
    adv.style.display = 'none';
    $("#LinkAdviceSearch").toggle();
    return false;
}

var pScrollAll = 0;
var pScrollStepAll = 50;

function scrolLeftAll(target) {
    var pOffset = target.offset().left;
    if (pScrollAll > 0) {
        pScrollAll = pScrollAll - pScrollStepAll;
    }
    target.animate({ scrollLeft: pScrollAll + 'px' }, 300);
}

function scrolRightAll(target) {
    var pOffset = target.offset().left;
    if (pScrollAll < 500) {
        pScrollAll = pScrollAll + pScrollStepAll;
    }
    target.animate({ scrollLeft: pScrollAll + 'px' }, 300);
}

var inputsEnable = (function($) {
    return function(el, holder) {
        var enbl = function(e) {
            var v = $(el).is(":checked") ? "" : "disabled";
            var els = $(holder).find(":input").attr("disabled", v);
        };
        $(el).click(enbl);
        enbl();
    };
})(jQuery);

function enableInputs(holder, val) {
    var v = val ? "" : "disabled";
    var els = $(holder).find(":input").attr("disabled", v);
}

(function($) {
    $(document).ready(function() {
        var noImage = "../style/images/noimage_tx.gif";
        $("img").error(function() {
            $(this).unbind("error").attr("oldsrc", jQuery(this).attr("src")).attr("src", noImage);
        });

        var slChange = function() {
            var el = $(this);
            if (el.children(":first").is(":selected")) {
                el.addClass("wtrMark");
            } else {
                el.removeClass("wtrMark");
            }
        };
        $("select.wtrMark").each(slChange)
        .mousedown(function() { $(this).removeClass("wtrMark"); })
        .blur(slChange);
    });
})(jQuery);

function fixSize(el, maxW, maxH) {
    el = jQuery(el);
    if (!el.is("img")) { return; }

    var setSize = function(w2, h2) {
        var ndrt = maxW / maxH;
        var ratio = w2 / h2;
        if (ratio > ndrt) {
            if (w2 > maxW) { el.width(maxW); }
        } else {
            if (h2 > maxW) { el.height(maxH); }
        }
    };

    var w = el[0].width;
    var h = el[0].height;
    if (w + h === 0) {
        var img = new Image();
        $(img).load(function() {
            setSize(this.width, this.height);
        });
        img.src = el.attr("src");
        return;
    }
    setSize(w, h);
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
function setCookie(c_name, value, expiredays) {
    var val = c_name + "=" + escape(value);
    if (expiredays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        val += ";expires=" + exdate.toGMTString();
    }
    document.cookie = val;
}
