﻿//<![CDATA[
var ecwid = {};

ecwidMessages = {
"Minicart.out_of_stock" : "The product is not available to buy online. <br/>Please contact us for more information.",
"DnDLabel.drag_me" : "Drag & Drop Me to the Cart",
"Minicart.drag_drop" : "<span class='ecwid-minicart-label-text'><i>To add, drag &amp; drop product to cart</i></span>",
"Minicart.shopping_bag" : "",
"Minicart.open_bag" : "",
"ShoppingCartView.shopping_bag" : "Your Shopping Cart",
"Pager.next" : "<span></span> \u00BB",
"Pager.page" : "Page:",
"Pager.previous" : "\u00AB <span></span>",
"ShoppingCartView.estimated_tax" : "GST 7%",
"ShoppingCartView.tax" : "GST 7%",
"OrderComponent.tax" : "GST 7%",
"OrderBox.tax" : "GST 7%",
"ProductDetails.in_bag" : "\ in the Cart",
"ProductDetails.In_bag" : "In the Cart",
"ProductDetails.qty" : "Quantity",
"ProductDetails.open_bag" : "View Cart",
"EmptyShoppingCartPanel.empty" : "Your Shopping Cart is empty",
"OrderComponent.tax" : "GST 7%",
"OrderBox.tax" : "GST 7%"
};

//~ECWID INTEGRATION FUNCTION
function checkEcwidLinks() {
    $j(function () {
        $debug("checkEcwidLinks()");
       
    var recheck = true;
    $j("div.ecwid-productBrowser-auth a.gwt-Anchor").each(function () {
        if ($j(this).text() == "Sign Out") { /*$j("#th_signout a").html($j.cookie("login-email") + " - Sign Out");*/ $j("#top_header").attr("statustype", "logout"); $j("#th_account > a").attr("href", "c13_account.html#ecwid:mode=accountSettings"); $j("#th_account").css("display", "list-item"); }
        else if ($j(this).text() == "Sign In") { $j("#top_header").attr("statustype", "login"); $j("#th_account").css("display", "none"); }
        //else if ($j(this).text() == "Sign In") { $j("#top_header").attr("statustype", "login"); $j("#th_account > a").attr("href", ""); }
        //$debug($j(this).text());
        recheck = false;
    });
});}


ecwid.getset = function () {
    function getRegLabelFor(text) {
        $debug("getRegLabelFor(" + text + ") : Searching : ");
        var tmpVar = "";
        $j(".ecwid-register-popup label.ecwid-fieldLabel").each(function () {
            if ($j(this).text() == text) {
                $debug("getRegLabelFor(" + text + ") : Found : " + $j(this).attr("for"));
                tmpVar = $j(this).attr("for");
                return;
            }
        });
        $j(".ecwid-login-popup label.ecwid-fieldLabel").each(function () {
            if ($j(this).text() == text) {
                $debug("getRegLabelFor(" + text + ") : Found : " + $j(this).attr("for"));
                tmpVar = $j(this).attr("for");
                return;
            }
        });
        
        return tmpVar;
    }

    var dynamicId = 0;
    function getButtonId(area, text) {
        var tmpVar = "";
        $j(area + " .gwt-Button").each(function () {
            if ($j(this).text() == text) {
                $debug("getButtonId(" + area + "," + text + ") : Found");
                tmpVar = "gh-ecwid-" + dynamicId;
                $j(this).attr("id", "gh-ecwid-" + dynamicId);
                dynamicId += 1;
                return;
            }
        });
        return tmpVar;
    }
    return { getRegLabelFor: getRegLabelFor, getButtonId: getButtonId }
} (ecwid);


ecwid.topnavigation = function () {

    var timerLoginPopup;

    //[Sign in]
    function signin(remote) {
        $debugtimer(true, "ecwid.topnavigation.signin(" + remote + ")");

        //[To check top navigation click or internal click]
        if (remote) {
            $j("div.ecwid-productBrowser-auth a.gwt-Anchor").each(function () {
                if ($j(this).text() == "Sign In") { $j(this).trigger('click'); timerLoginPopup = setInterval("ecwid.topnavigation.checkLoginPopup()", 300); return false; }
                $debug("ecwid.topnavigation.signin() : " + $j(this).text());
            });
        }

        /*[Redesign the layout and add content]*/
        $j("div.ecwid-popup").corner("round 4px");
        $j("div.ecwid-popup .ecwid-popup-headLabel").html("CRS Communcation <span>Customer Login</span><br/><span class=\"desc\">Login using your registered email and password</span>");
        $j("div.ecwid-popup .ecwid-popup-content").append("<div class=\"popup-note\"><strong>Note:&nbsp;</strong> Your privacy is important to us. The personal information entered on this page will not be used for marketing purposes or be sold to others without your consent.</div>")
        $j("div.ecwid-popup").attr("onclick", "$debug('popupclick');");

        //[Manipulate the links/action]
        $j("div.ecwid-popup a.gwt-Anchor").each(function () {
            var $this = $j(this);

            if ($this.text() == "Forgot your password?") { $this.attr("href", "javascript:ecwid.topnavigation.forgetPassword();"); }
            else if ($this.text() == "Create new account") { $this.attr("href", "javascript:ecwid.topnavigation.createAccount();mcRegisterOpen();"); }
            $debug("ecwid.topnavigation.signin : set href " + $this.text());
        });

        $j("#" + ecwid.getset.getButtonId("div.ecwid-popup", "Sign In")).mouseup(function () {
            var $temp = $j("#" + ecwid.getset.getRegLabelFor("Email") + "").val();
            $j.cookie("login-email", $temp); /*location.reload(true);*/
        });

        $j(".button.ecwid-popup-closeButton").mouseup(function () {
            clearInterval(timerLoginPopup);
        });

        $debugtimer(false, "ecwid.topnavigation.signin(" + remote + ")");
    };

    function checkLoginPopup() {
        $debug("checkLoginPopup " + $j(".ecwid-login-popup").length);
        if ($j(".ecwid-login-popup").length <= 0) { clearInterval(timerLoginPopup); /*location.reload(true);*/ }
    }

    //[Sign out]
    function signout() {
        $debugtimer(true, "ecwid.topnavigation.signout()");

        $j("div.ecwid-productBrowser-auth a.gwt-Anchor").each(function () {
            var $this = $j(this);
            if ($this.text() == "Sign Out") {
                $this.trigger('click');
                setTimeout(function () { location.href = "default.aspx"; }, 500);
                $debug("ecwid.topnavigation.signout : trigger " + $this.text());
            }
            checkEcwidLinks();
            return;
        });
        $debugtimer(false, "ecwid.topnavigation.signout()");
    };

    //[Create account]
    function createAccount() {
        clearInterval(timerLoginPopup);
        $j("div.ecwid-popup").corner("round 4px");
        $j("div.ecwid-popup a.gwt-Anchor").each(function () {
            if ($j(this).text() == "I have a password. Sign in.") { $j(this).attr("href", "javascript:signin(false);"); }
            $debug($j(this).text());
        });
    }

    //[Forget password]
    function forgetPassword() {
        clearInterval(timerLoginPopup);
        $j(function () {
            $j("div.ecwid-popup").corner("round 4px");
            $j("div.ecwid-popup .ecwid-popup-headLabel").html("Forgot your password?<br/><span class=\"desc\">Please enter your email address below</span>");
        });
    }

    return { signin: signin, signout: signout, checkLoginPopup: checkLoginPopup, createAccount: createAccount, forgetPassword: forgetPassword }
} (ecwid);


/*~ECWID FORMS INTEGRATION FUNCTION
function signup() {$j(document).ready(function(){
    
    $j("div.ecwid-LoginForm #gwt-uid-1").val( $j("#email").val() );
    $j("div.ecwid-LoginForm #gwt-uid-2").val( $j("#firstname").val() );
    $j("div.ecwid-LoginForm button.gwt-Button").click();
    
});}
*/

function signin2(remote) {$j(document).ready(function(){
    $debugtimer(true, "signin2()");

    $debugtimer(true, "ecwid.topnavigation.signin2(" + remote + ")");

    //[To check top navigation click or internal click]
    if (remote) {
        $j("div.ecwid-productBrowser-auth a.gwt-Anchor").each(function () {
            if ($j(this).text() == "Sign In") { $j(this).trigger('click'); timerLoginPopup = setInterval("ecwid.topnavigation.checkLoginPopup()", 300); return false; }
            $debug("ecwid.topnavigation.signin2() : " + $j(this).text());
        });
    }

    /*[Redesign the layout and add content]*/
    $j("div.ecwid-popup").corner("round 4px");
    $j("div.ecwid-popup .ecwid-popup-headLabel").html("CRS Communcation <span>Customer Login</span><br/><span class=\"desc\">Login using your registered email and password</span>");
    $j("div.ecwid-popup .ecwid-popup-content").append("<div class=\"popup-note\"><strong>Note:&nbsp;</strong> Your privacy is important to us. The personal information entered on this page will not be used for marketing purposes or be sold to others without your consent.</div>")
    $j("div.ecwid-popup").attr("onclick", "$debug('popupclick');");

    //[Manipulate the links/action]
    $j("div.ecwid-popup a.gwt-Anchor").each(function () {
        var $this = $j(this);

        if ($this.text() == "Forgot your password?") { $this.attr("href", "javascript:ecwid.topnavigation.forgetPassword();"); }
        else if ($this.text() == "Create new account") { $this.attr("href", "javascript:ecwid.topnavigation.createAccount();mcRegisterOpen();"); }
        $debug("ecwid.topnavigation.signin2 : set href " + $this.text());
    });

    $j("#" + ecwid.getset.getButtonId("div.ecwid-popup", "Sign In")).mouseup(function () {
        var $temp = $j("#" + ecwid.getset.getRegLabelFor("Email") + "").val();
        $j.cookie("login-email", $temp); /*location.reload(true);*/
    });

    $j(".button.ecwid-popup-closeButton").mouseup(function () {
        clearInterval(timerLoginPopup);
    });

    $debugtimer(false, "signin2()");
});}

function forgetPassword() {$j(document).ready(function(){
    $j("div.ecwid-popup").corner("round 4px");
    $j("div.ecwid-popup .ecwid-popup-headLabel").html("Forgot your password?<br/><span class=\"desc\">Please enter your email address below</span>");
});}

function createAccount() {$j(document).ready(function(){

    clearInterval(timerLoginPopup);
    $j("div.ecwid-popup").corner("round 4px");
    $j("div.ecwid-popup a.gwt-Anchor").each(function(){
        if($j(this).text()=="I have a password. Sign in.") { $j(this).attr("href","javascript:signIn(false);"); }
        $debug($j(this).text());
    });
    
});}

//~JS DirectCall
function ecwidCreateAccount() {
    $j("div.ecwid-popup button.gwt-Button").each(function(){
        if($j(this).text()=="Register") { $j(this).click(); }
        $debug("Ecwid click: "+$j(this).text());
    });
    setTimeout("check_busy()", 500);
}
function check_busy() {
    if($j("body").hasClass("busy")==false) {

        if ($j("#" + ecwid.getset.getRegLabelFor("Email") + " div.ecwid-fieldEnvelope-label").text() == "Email already registered") {
        	$debug("#dialog-email-registered");
        	alert("Email already registered, please choose another email.");
        	mcBackStep2();
        }
        else { mcRegister(); checkEcwidLinks();  }
    }
    else { setTimeout("check_busy()", 500); }
    
    $debug("check_busy");
}

function signOut() {$j(document).ready(function(){
    
    $j("div.ecwid-productBrowser-auth a.gwt-Anchor").each(function(){
        if($j(this).text()=="Sign Out") { $j(this).trigger('click'); }
        $debug($j(this).text());
        checkEcwidLinks();
        return;
    });
});}





//]]>
