function initSearchKeywords(){
    var kw = document.getElementById("searchKeywords");
    addAnEvent(kw,'focus',clearTextbox);
    addAnEvent(kw,'blur',checkTextbox);
    //kw.value = "eg: Men Titanium";
    //kw.style.color="#8D8C83"
    //document.getElementById("searchSubmit").disabled = true;
}

function initCopyAddress(){
    var field_names = new Array('first_name','last_name','company','address1','address2','city','state','country','zip','phone');

    for(i = 0; i < field_names.length; i++){
        var bill_ref = document.getElementById(field_names[i]);
        addAnEvent(bill_ref,'change',copyField);      
    }


}

function copyField(){
    var do_copy = document.getElementById("ship_same_as_bill");    

    if(do_copy.checked){
        ship_ref = document.getElementById('ship_' + this.name);
        ship_ref.value = this.value;
               
    }    
}

function clearTextbox(){
    if (this.value == "eg: Men Titanium"){
        this.value = "";
        this.style.color="#000"
        //document.getElementById("searchSubmit").disabled = false;
    }
}

function checkTextbox(){
    if (this.value == "") {
        this.value = "eg: Men Titanium";
        this.style.color="#8D8C83"
        //document.getElementById("searchSubmit").disabled = true;
    }
}

function initColorMenu(){
    var c = document.getElementById("color");
    addAnEvent(c,'change',switchColor); 
}
function switchColor(){
    var opt = document.getElementById("optionsBox");
    opt.action = "";
    opt.submit();
}

//tracking and receipt pop ups
function initLinks(){
    var c = document.getElementById('content');
    var as = c.getElementsByTagName('a');
    for (var i=0; i<as.length; i++){
        if (as[i].className.indexOf("tracking") != -1){
            addAnEvent(as.item(i),'click',trackWin);
        }
        if (as[i].className.indexOf("receipt") != -1){
            addAnEvent(as.item(i),'click',receiptWin);
        }
        if (as[i].className.indexOf("popup") != -1){
            addAnEvent(as.item(i),'click',popupWin);
        }        
    }
}

function trackWin(){
    appWindow = window.open (this.href,"appWindow","width=600,height=400,toolbar=0,resizable=1,scrollbars=1");
    appWindow.focus ();
    return false;
}

function receiptWin(){
    appWindow = window.open (this.href,"appWindow","width=600,height=400,toolbar=0,resizable=1,scrollbars=1");
    appWindow.focus ();
    return false;
}

function popupWin(){
    appWindow = window.open (this.href,"appWindow","width=400,height=260,toolbar=0,resizable=1,scrollbars=1");
    appWindow.focus ();
    return false;
}

function initSort(){
    var sort = document.getElementById('sort');
    addAnEvent(sort,'mouseover',showSort);
    addAnEvent(sort,'mouseout',hideSort);
}
function showSort(){
    this.getElementsByTagName('ul')[0].className = "hover";
}
function hideSort(){
    this.getElementsByTagName('ul')[0].className = "";
}

function sfTarget () {
    var sfEls=document.getElementsByTagName("div");
    var aEls = document.getElementsByTagName("a");
    document.lastTarget = null;
    for (var i=0; i<sfEls.length; i++) {
        if (sfEls[i].id) {
            if (location.hash==("#" + sfEls[i].id)) {
                sfEls[i].className+=" sftarget";
                document.lastTarget=sfEls[i];
            }
            for (var j=0; j<aEls.length; j++) {
                if (aEls[j].hash==("#" + sfEls[i].id)) aEls[j].targetEl = sfEls[i]; aEls[j].onclick = function() {
                    if (document.lastTarget) document.lastTarget.className = document.lastTarget.className.replace(new RegExp(" sftarget\\b"), "");
                    if (this.targetEl) this.targetEl.className+=" sftarget"; document.lastTarget=this.targetEl;
                    return true;
                }
            }
        }
    }
}


function form_lock(button_ref, form_id){

    button_ref.src = '/images/btns/processingOrderBtn.gif';
    $(form_id).submit();
    $(form_id).disable();
    
    return false;

}


function initialize(){
    if ( document.getElementById ){
        if (document.all && !document.getElementById("chromeless")) sfTarget();
        if (document.getElementById("searchKeywords")) initSearchKeywords();
        if (document.getElementById("optionsBox") && document.getElementById("color")) initColorMenu();
        initLinks();
        if (document.getElementById("sort")) initSort();
        if (document.getElementById("ship_same_as_bill")) initCopyAddress();
    }
}

function addAnEvent( target, eventName, functionName){
   eval('target.on'+eventName+'=functionName');
}

window.onload=initialize;