function clearStatusOnOrderSearchPage(self,name1,name2,name3,name4,name5,name6){
    var mySelf = $(self);
    var tab = document.getElementsByName(name1);
    var obj1 = $(name2);
    var obj2 = $(name3);
    var obj3 = $(name4);
    var obj4 = $(name5);
    var obj5 = $(name6);
    
    if(mySelf.id == 'status_date_id'){
	if(mySelf.selectedIndex != 0){
	    for (var index = 0; index < tab.length; ++index) {
		tab[index].checked=false;
	    }
	    obj5.checked=false;
	    obj1.disabled=true;
	    obj2.disabled=true;
	    obj3.disabled=false;
	    obj4.disabled=false;
	}else{
	    obj5.checked=true;
	    obj1.disabled=false;
	    obj2.disabled=false;
	    obj3.disabled=true;
            obj4.disabled=true;
	}
    }else if(mySelf.id == "activate_date"){
	$(name1).selectedIndex = 0;
	obj1.disabled=false;
        obj2.disabled=false;
        obj3.disabled=true;
        obj4.disabled=true;
    }else{
	$(name1).value = "";
	obj5.checked=true;
	obj1.disabled=false;
	obj2.disabled=false;
	obj3.disabled=true;
        obj4.disabled=true;
    }
    return false;
}



function getValueFromCookie(cookie_name) {
    var value = Cookie.getCookie(cookie_name + '2');
    if (value !== null) {
        return value;
    }
    return Cookie.getCookie(cookie_name + '1');
}

function printShoppingBagCount() {
    var numItems = getValueFromCookie('num_items_in_cart');
    if (numItems == null) {
        numItems = 0;
    }
    if (numItems == 0 || location.href.indexOf('checkout_receipt.php') > 0) {
        style = "none";
        items = '0 items';
    } else {
        style = "block";
        items = numItems + ' item' + (numItems != 1 ? 's': '');
    }
    var checkout = document.getElementById('hnCheckout');
    checkout.style.display = style;

    document.getElementById('shoppingBagCount').innerHTML = items;
}

function getCheckoutUrl(ssl_url,isChangeMethod,isCreditCard) {
    var currentPage = location.href;
   if(isChangeMethod == 1){
      isCreditCard = 1;
    }

    if (currentPage.indexOf('cart.php') > 0 && isCreditCard == 1) {
        return 'href="' + ssl_url + 'checkout_billing.php" onclick="Omniture.abFix();"';
    }
    if (currentPage.indexOf('checkout_receipt.php') > 0) {
        return 'href="#"';
    }
    if (currentPage.indexOf('checkout_') > 0) {
        return 'href="#" onclick="Omniture.abFix();document.registerfrm.submit();"';
    }
    return 'href="/cart.php" onclick="Omniture.abFix();"';
}

function printCheckoutLink(ssl_url,isChangeMethod,isCreditCard) {
    document.write('<a title="Checkout" ' + getCheckoutUrl(ssl_url,isChangeMethod,isCreditCard) + ' name="&lid=CheckoutTopBar">Checkout</a>');
}

var updateCookiesCallbacks = [];

function addUpdateCoockiesCallback(callback) {
    updateCookiesCallbacks.push(callback);
}

function addReloadCountryPaymentMethod() {
    reloadCountryPaymentMethod("N");
}

function reloadCountryPaymentMethod(change){
    
    var currentPage = location.href;
    var page = "";
    var changeMethod = change;
    if(currentPage.indexOf('cart.php') > 0 || currentPage.indexOf('cart.php?e=t') > 0){
	page = "cart.php";
	target = 'shopping_cart_table_wrapper';
    }else if(currentPage.indexOf('checkout_billing.php') > 0){
	page = "checkout_billing.php";
	target = "main-nobg";
    }else if(currentPage.indexOf('checkout_receipt.php')){
	page = "checkout_receipt.php";
    }
    var url = 'ajax_requests/update_payment_method.php?page='+page+'&change_method='+changeMethod;
    new Ajax.Updater(target, url, {
	asynchronous: true,
	evalScripts: true
    });
}

//footer newsletter signup 
function clearSignup(t) {
    clearFormField(t.form.email,"Enter E-mail Address");
}

function clearSearchBox(t){
    clearFormField(t.form.query,"Search");
}

function clearFormField(input,value){
    if(input.value == value){
	input.value = "";
    }
}

//legacy methods
function popUp(url) {
    sealWin = window.open(url, "win", 'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=450');
    self.name = "mainWin";
}

function newWd(page, width, height, popup_name) {
    popup_name = typeof(popup_name) == 'undefined' ? 'BigPic' : popup_name;
    newwin = window.open(page, popup_name, 'left=20,top=20,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=' + width + ',height=' + height);
    newwin.focus();
}

function newWdR(page, width, height) {
    newwin = window.open(page, 'BigPic', 'left=20,top=20,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=' + width + ',height=' + height);
    newwin.focus();
}

function newWdS(page, width, height) {
    newwin = window.open(page, 'Freshpair', 'left=20,top=20,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=' + width + ',height=' + height);
    newwin.focus();
}

function newWdF(page, width, height) {
    newwin = window.open(page, 'Freshpair', 'left=20,top=20,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=' + width + ',height=' + height);
    newwin.focus();
}

function newWdE(page, width, height) {
    var putItThere = null;
    var chasm = screen.availWidth;
    var mount = screen.availHeight;
    var w = 0;
    var h = 0;

    newwin = window.open(page, 'BigPic', 'screenX=20,screenY=20,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=' + width + ',height=' + height + ',left=' + ((chasm - w - 10) * .5) + ',top=' + ((mount - h - 30) * .5));
    newwin.focus();
}

function resizeWindowByElementId(elementId) {
    var element = document.getElementById(elementId);
    if(element){
	iWidth = element.offsetWidth - getWindowWidth();
	iHeight = element.offsetHeight - getWindowHeight();
	window.resizeBy(iWidth, iHeight); 
    }
}

function getWindowWidth(){
    if(typeof(window.innerWidth) == 'number' ) { //Non-IE
	return window.innerWidth;
    } else if(document.documentElement && document.documentElement.clientWidth){ //IE 6+ in 'standards compliant mode'
	return document.documentElement.clientWidth;
    } else if(document.body && document.body.clientWidth){ //IE 4 compatible
	return document.body.clientWidth;
    }
    return 0;
}

function getWindowHeight(){
    if(typeof(window.innerHeight) == 'number' ) { //Non-IE
	return window.innerHeight;
    } else if(document.documentElement && document.documentElement.clientHeight){ //IE 6+ in 'standards compliant mode'
	return document.documentElement.clientHeight;
    } else if(document.body && document.body.clientHeight){ //IE 4 compatible
	return document.body.clientHeight;
    }
    return 0;
}

function getURLParam(strParamName) {
    var strReturn = "";
    var strHref = window.location.href;
    if (strHref.indexOf("?") > -1) {
        var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
        var aQueryString = strQueryString.split("&");
        for (var iParam = 0; iParam < aQueryString.length; iParam++) {
            if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1) {
                var aParam = aQueryString[iParam].split("=");
                strReturn = aParam[1];
                break;
            }
        }
    }
    return unescape(strReturn);
}

function setMasterSizeCookie(master_size_id,category_id){
    var value = master_size_id + '|0|' + category_id;
    setPreferencesCoookie(value);
}

function setAliasCookie(alias_id,category_id){
    var value = '0|' + alias_id + "|" + category_id;
    setPreferencesCoookie(value);
}

function setPreferencesCoookie(value) {
    var aliasCookie = Cookie.getCookie('preferences');
    var master_size_id = value.split("|")[0];
    var alias_id = value.split("|")[1];
    var category_id = value.split("|")[2];
    if(aliasCookie) {
	var chunks = aliasCookie.split(";");

	var master_size_id_from_cookie;
	var alias_id_from_cookie;
	var category_id_from_cookie;
	
	var newCookie = new Array();
	for(var i = 0; i < chunks.length; i++){

	    master_size_id_from_cookie = chunks[i].split("|")[0];
	    alias_id_from_cookie = chunks[i].split("|")[1];
	    category_id_from_cookie = chunks[i].split("|")[2];

	    if(category_id_from_cookie != category_id && (master_size_id_from_cookie != master_size_id || alias_id_from_cookie != alias_id)) {
		newCookie.push(chunks[i]);
	    }
	}
	newCookie.push(value);
	newCookieValue = newCookie.join(';');
    } else {
	newCookieValue = value;
    }
    Cookie.setCookie('preferences',newCookieValue,0,'/');
}

function submitDeptMenu(t){
    var size_dropdown = t.form.alias_id;
    if(size_dropdown.type == 'select-one') {
	alias_id = size_dropdown.options[t.form.alias_id.selectedIndex].value;
	var category_id = t.form.id.value;
	setAliasCookie(alias_id,category_id);
    }
    
    if (typeof(t.form["occasion"])!="undefined"){
        if (t.form["id"].value=="" && t.form["occasion"].value!=""){
            t.form["occasion"].value="";
        }
    }
    t.form.submit();
}

function goToBackLink(){
    if (window.location.pathname.split('/').length == 2) { //is item page
	var cookie = Cookie.getCookie('referring_page');
	if(cookie){
	    var data = cookie.split('/'); 
	    window.location = '/catalog.php?id=' + data[0]+ '&brand=' + data[1]+ '&sectionid=' +  data[2];
	    return false;
	}
	window.location = '/';
    }
    return false;
}

function openFreeShippingPopup(){
    var cookie = Cookie.getCookie('fs_popup');
    var query_string = '';
    if(cookie){
	var data = cookie.split('|');
	query_string = '?id='+data[0];
    }
    newWd('/popup_free_shipping.php'+query_string,200,200);
}

function checkCountryMethodPayment(countryLive,methodPaymentCreditCard,methodPaymentPaypal,self){

    var isCountryLive = $(countryLive);
    var methodPaymentCreditCard = $(methodPaymentCreditCard);
    var methodPaymentPaypal = $(methodPaymentPaypal);
    if(isCountryLive.checked && !methodPaymentCreditCard.checked && !methodPaymentPaypal.checked){
	alert("You can't switch off this method becouse Country will be without any payment method");
    }
}

var updateCookiesCallbacks = [];

function addUpdateCoockiesCallback(callback) {
   updateCookiesCallbacks.push(callback);
}

function clearUpdateCoockiesCallbacks() {
   updateCookiesCallbacks.clear();
}
