function registerOver() {
var overLinks = $$('a.over');
overLinks.each(function(link) {
link.onmouseover = highlightOver;
link.onmouseout = highlightOut;
});
var inputOverLinks = $$('input.over');
inputOverLinks.each(function(link) {
link.onmouseover = highlightOver;
link.onmouseout = highlightOut;
});

var overLinks = $$('a[class="overc"]');
overLinks.each(function(link) {
link.onmouseover = highlightOver;
link.onmouseout = highlightOut;
link.onmousedown = pressOver;
link.onmouseup = highlightOver;
});
var inputOverLinks = $$('input[class="overc"]');
inputOverLinks.each(function(link) {
link.onmouseover = highlightOver;
link.onmouseout = highlightOut;
link.onmousedown = pressOver;
link.onmouseup = highlightOver;
});
}


function highlightOver(){
var image;
if (this.nodeName == 'A') {
image = $(this).firstChild;
} else {
image = $(this);
}
if (image.src.indexOf('_h.gif') <0 && image.src.indexOf('_h.jpg') <0)  {
if (image.src.indexOf('_p.gif') >=0 || image.src.indexOf('_p.jpg') >=0) {
image.src = image.src.replace('_p.gif','_h.gif');
image.src = image.src.replace('_p.jpg','_h.jpg');
} else {
image.src = image.src.replace('.gif','_h.gif');
image.src = image.src.replace('.jpg','_h.jpg');
}
}
}
function highlightOut(){
var image;
if (this.nodeName == 'A') {
image = $(this).firstChild;
} else {
image = $(this);
}
if (image.src.indexOf('_p.gif') >=0 || image.src.indexOf('_p.jpg') >=0) {
image.src = image.src.replace('_p.gif','.gif');
image.src = image.src.replace('_p.jpg','.jpg');
} else {
image.src = image.src.replace('_h.gif','.gif');
image.src = image.src.replace('_h.jpg','.jpg');
}
}

function pressOver(){
var image;
if (this.nodeName == 'A') {
image = $(this).firstChild;
} else {
image = $(this);
}
if (image.src.indexOf('_h.gif') <0 || image.src.indexOf('_h.jpg') <0)  {
if (image.src.indexOf('_h.gif') >=0 || image.src.indexOf('_h.jpg') >=0) {
image.src = image.src.replace('_h.gif','_p.gif');
image.src = image.src.replace('_h.jpg','_p.jpg');
} else {
image.src = image.src.replace('.gif','_p.gif');
image.src = image.src.replace('.jpg','_p.jpg');
}
}
}

function scrollPhotos () {
        $('.imagescroll img').each(function (photo) {
                photo.title = photo.alt;
                photo.onmouseover = highlightOver;
                photo.onmouseout = highlightOut;
                photo.observe('click', respondToClick);
        });

}

function selectPhoto(url,title) {
try {
var photo = $('photo');
photo.src = url.replace('_icon_h','_photo');
photo.src = photo.src.replace('_icon','_photo');
var imageTitle = photo.parentNode.firstChild;
if (title!= null) {
imageTitle.innerHTML = title;
} else {
imageTitle.innerHTML = "";
}
} catch(e){}
return false;
}

function changePic(picture) {
var backpic = document.getElementById('backpic');
backpic.src=picture;
}

function displayMenu(menuid) {
try {
var menu = document.getElementById(menuid);
if (menu.style.display == 'none') {
Effect.SlideDown(menu, {duration:0.5,afterFinish:function() {
Event.observe(document,'click',function (e)  {
menu.style.display = 'none';
try{
Event.stopObserving(document, 'click');
}catch(e) {}
}); 
}
});
} else {
menu.style.display = 'none';
}
} catch (e) {
alert (e);
}
return false;
}
/*
function toggleBasket(obj) {
var dropDown = document.getElementById('basketdrop');
if (dropDown.style.display == 'none'){
basketOn();
}else {
basketOff();
}
}

function basketOff() {
new Effect.SlideUp('basketdrop', {duration:0.2});
document.onclick=null;
}

function basketOn() {
animateBasket();
RemoteName.getBasket( { callback:function(result) {
var dropDown = $('basketdrop');
var fullbag = dropDown.select("[class='fullbag']").first();
if (result == null || result.items.length ==0) {
fullbag.innerHTML = "";
dropDown.select("[class='emptybag']").first().style.display = 'inline';
dropDown.down();

//dropDown.down().innerHTML = "Your bag is empty";
} else {
var content = document.createElement("ul");
dropDown.select("[class='emptybag']").first().style.display = 'none';
fullbag.innerHTML = "";
var basketlist = dropDown.select("[class='basketlist']").first();
for (i=0;i< result.items.length; i++){
if (result.items[i] != null) {
var line = basketlist.select("[class='productline']").first().cloneNode(true);
line.select("[class='item']").first().innerHTML =  result.items[i].quantity + ' x ' + result.items[i].productDesc;
line.select("[class='price']").first().innerHTML = result.items[i].subTotal;
line.addClassName("line");
content.appendChild(line);

//content += '<li class="line"><span class="item">' + result.items[i].quantity + ' x ' + result.items[i].productDesc + '</span><span class="price">' + result.items[i].subTotal + '</span></li>';
} 
}
if (result.discountTitle != null && result.discountTitle != "") {
var line = basketlist.select("[class='discountline']").first().cloneNode(true);
line.select("[class='item']").first().innerHTML =  result.discountTitle;
line.select("[class='price']").first().innerHTML = '-' + result.discountValue ;
line.addClassName("line");
content.appendChild(line);
//content += '<li class="line"><span class="item">' + result.discountTitle + '</span><span class="price">-' + result.discountValue + '</span></li>';
}

if (result.deliveryDesc != null && result.deliveryDesc != "") {
var line = basketlist.select("[class='deliveryline']").first().cloneNode(true);
line.select("[class='item']").first().innerHTML = line.select("[class='item']").first().innerHTML.replace(":",":" +result.deliveryDesc);
line.select("[class='price']").first().innerHTML = result.deliveryPrice;
line.addClassName("line");
content.appendChild(line);
//content += '<li class="line"><span class="item">Delivery:' + result.deliveryDesc + '</span><span class="price">' + result.deliveryPrice + '</span></li>';
}
var totalLine = basketlist.select("[class='totalline']").first().cloneNode(true);
totalLine.select("[class='total']").first().innerHTML = "Total";
totalLine.select("[class='tprice']").first().innerHTML = result.total;
totalLine.addClassName("line");
content.appendChild(totalLine);
dropDown.appendChild(content);
dropDown.down();
//content += '<li class="line"><span class="total">Total</span><span class="tprice">' + result.total + '</span></li></ul>';
//dropDown.down().innerHTML = content;
} 
Effect.SlideDown('basketdrop', {duration:0.5,
afterFinish:closeBasket}
);
stopAnimateBasket();
}, errorHandler:function(message) {
        alert(message);
stopAnimateBasket();
} 
});

}

function closeBasket() {
document.onclick=basketOff;
}
*/
function addOne(productId) {
//animateBasket();
RemoteName.addProd(productId,1,{ callback:function(result) { refreshSummary(); setErrorLog(result);stopAnimateBasket();}   ,
errorHandler:function(message) { refreshSummary();setErrorLog(message);stopAnimateBasket();} 
}
);
return false;
}

function removeOne(productId) {
//animateBasket();
RemoteName.removeProd(productId,1,{ callback:function(result) { refreshSummary();setErrorLog(result);stopAnimateBasket();}   ,
errorHandler:function(message) { refreshSummary();setErrorLog(message);stopAnimateBasket();} 
}
);
return false;
}

function refreshSummary() {

new Ajax.Request('checkout_summary.htm?r='+ (new Date().getTime()), {
method: 'get',
onSuccess: function(transport) {

var orderSumTable = $('ordersum');

orderSumTable.update(transport.responseText);
},
onFailure:function (transport) {
alert("error");
}
});
}

function setErrorLog(message) {
var errorLog = $('errorLog');
errorLog.removeClassName('error');
errorLog.removeClassName('log');
if (message != null && message != "") {
if (message.type == null) {
errorLog.addClassName('error');
} else {
errorLog.addClassName(message.type.toLowerCase());
}
errorLog.innerHTML = message.text;
} else {
errorLog.innerHTML = '';
}
var itemNumber = $('itemnumber');
if (itemNumber != null) {
if (message.totalItems != null) {
        
itemNumber.innerHTML = message.totalItems + " " + (itemNumber.innerHTML.substring(itemNumber.innerHTML.indexOf(" "),itemNumber.innerHTML.length));

if (message.totalItems <2) {
if (itemNumber.innerHTML.endsWith("s")) {
        itemNumber.innerHTML = itemNumber.innerHTML.substring(0,itemNumber.innerHTML.length - 1);
}
} else {
if (!itemNumber.innerHTML.endsWith("s")) {
        itemNumber.innerHTML = itemNumber.innerHTML+"s";
}
}
}
}
}

function setErrorMsg(messageTxt) {
var errorLog = $('errorLog');
//alert(errorLog+"/"+messageTxt);
errorLog.removeClassName('log');
errorLog.addClassName('error');
errorLog.innerHTML = messageTxt;
}

function addToBasket(form) {
try {

/*animateBasket();
*/
var eForm = $(form);
var attributes = eForm.getInputs('radio');
var sAtt = Form.serializeElements(attributes,true);
var modelId = eForm.modelId.value;
RemoteName.add(modelId,sAtt,1,{ 
    callback:function(message) {
        setErrorLog(message);
        stopAnimateBasket();
        if ('LOG' == message.type) {
            var checkoutLink = $('checkoutLink');
            if (checkoutLink) {
                window.location = checkoutLink.getAttribute("value");
            }
          }
    },
errorHandler:function(message) {  setErrorLog(message);stopAnimateBasket();}
}
);
} catch (e) {
alert(e);
setErrorMsg("An error occured");
stopAnimateBasket();
}
return false;
}

function doNothing() {
return false;
}

function updateCountry() {
var countryBox = $('address.country');
var opt = countryBox.options[countryBox.selectedIndex];
var zonesTxt = eval(opt.onclick).call();
var zones = $A(zonesTxt);
var deliveryOptions = ($('checkoutform')).getInputs('radio', 'deliveryOption');
deliveryOptions.each(function(radioB) {
var supported = false;
zones.each(function(zone) {
if (zone == radioB.value ) {
supported = true;
}
});
if (radioB.checked && !supported) {
radioB.checked = false;
}
radioB.disabled = !supported;
});
var noneSelected = true;
deliveryOptions.each(function(radioB) {
if (radioB.checked) {
noneSelected = false;
}
});
if (noneSelected) {
updateDelivery();
}
}

function setDelivery(deliveryOptionId) {
var deliveryOptions = ($('checkoutform')).getInputs('radio', 'deliveryOption');
deliveryOptions.each(function(radioB) {
if (radioB.value == deliveryOptionId) {
radioB.checked = true;
} else {
radioB.checked = false;
}
});
}

function updateDelivery() {
var countryId = $('address.country').value;
var deliveryOption;
var deliveryOptions = ($('checkoutform')).getInputs('radio', 'deliveryOption');
deliveryOptions.each(function(radioB) {
if (radioB.checked == true) {
deliveryOption = radioB.value;
}
});

RemoteName.updateDelivery(deliveryOption,countryId,{
callback:function(result) {
refreshSummary();

setDelivery(result.deliveryOptionId);
}
});
}

function updatePromotion() {
var promotionCode = $('promotionCode').value;


RemoteName.applyPromotion(promotionCode,{
callback:function(result) {
if (result.text.match('^ok$') ) {
refreshSummary();
setErrorMsg("");
} else {
    refreshSummary();
setErrorMsg(result.text);
}

}
});
}


function animateBasket(){
/*document.body.style.cursor='wait';
$('basket').src = $('basket').src.replace('.gif','_a.gif')*/
}

function stopAnimateBasket(){
/*setTimeout("$('basket').src =  $('basket').src.replace('_a.gif','.gif')",1200);
document.body.style.cursor='auto';*/
}

function prebook(form) {
new Ajax.Request('../notify/book?r='+ (new Date().getTime()), {
method: 'get',
onSuccess: function(transport) {
try {
if (transport.responseText.match('^ok$') ) {
// WARNING: this might loop.
form.submit();
} else {
setErrorMsg(transport.responseText);
}
} catch(e) {
alert(e);}
},
onFailure: function(transport) {
alert("Failure:"+transport.responseText);
setErrorMsg(transport.responseText);
}
});
return false;
}

function createMenuCat(menu) {
if($(menu) == null) { return; }
$(menu).select('.cat_list').each(function(list) {
    list.hide();

});
$(menu).select('.cat_menu').each(function(cat) {
    cat.onclick = function() {
        if(cat.hasClassName('selected')) {
            closeCatMenu(cat);
        } else {
            openCatMenu(cat);

        }
        
      
    }
});

}

function openCatMenu(cat) {
    if(cat == null) {return;}
    cat.addClassName('selected');
    var content  = $(cat.parentNode).select('.cat_list');
    if (content != null && content.first() !=null) {
        Effect.SlideDown(content.first(), { duration: 0.5 });
    }
}
function closeCatMenu(cat) {
    if(cat == null) {return;}
    cat.removeClassName('selected');
    $(cat.parentNode).select('.cat_list').first().hide();
}
