var zoom;



var mImgList = new Array("home","cart","about","contact","careers","account");
var mOutImg = new Array();
var mOverImg = new Array();
var i,j;

//load images

if(document.images) {
	for(i=0;i<mImgList.length;i++) {
		mOutImg[mImgList[i]] = new Image();
		mOutImg[mImgList[i]].src = "/_resources/americansurplus.com/images/menu/m_"+mImgList[i]+".png";
		mOverImg[mImgList[i]] = new Image();
		mOverImg[mImgList[i]].src = "/_resources/americansurplus.com/images/menu/m_"+mImgList[i]+"_on.png";
	}
}



function mOvr(img) {
	if(document.images && document.images[img]) {
		if(document.images[img].filters) { document.images[img].filters.blendTrans.apply(); }
		document.images[img].src = mOverImg[img].src;
		if(document.images[img].filters) { document.images[img].filters.blendTrans.play(); }
	}
}


function mOut(img) {
	if(document.images && document.images[img]) {
		if(document.images[img].filters) { document.images[img].filters.blendTrans.apply(); }
		document.images[img].src = mOutImg[img].src;
		if(document.images[img].filters) { document.images[img].filters.blendTrans.play(); }
	}
}



function LoadProductImage(handle,showloadingimage){
	
	var mainImageID = "myimage";
	var instructionsID = "ProductImageInstructions";
	var loadingImageSrc = "/_resources/americansurplus.com/images/ajax-loader.gif";
	var thumbWidth = 50;
	var thumbHeight = 50;
	var mainImageWidth = 250;
	var mainImageHeight = 250;
	var zoomImageWidth = 400;
	var zoomImageHeight = 400;	
	
	var imgSrc;
	var magnifySrc;	
	imgSrc = handle.src;
	imgSrc = imgSrc.replace("width="+thumbWidth,"width="+mainImageWidth);
	imgSrc = imgSrc.replace("height="+thumbHeight,"height="+mainImageHeight);
	magnifySrc = imgSrc;
	magnifySrc = magnifySrc.replace("width="+mainImageWidth,"width="+zoomImageWidth);
	magnifySrc = magnifySrc.replace("height="+mainImageHeight,"height="+zoomImageHeight);
	
	if(zoom){zoom.parentNode.removeChild(zoom);}
	
	if(showloadingimage){
		// -- if used in IE before main image is done loading, we get the '1 item loading' bug --- //
		var parentHandle = getElement(mainImageID).parentNode;
		parentHandle.removeChild(getElement(mainImageID));
	
		var imageLoading = new Image();
		imageLoading.onload = function() {
			imageLoading.onload = null;
		}
		imageLoading.src = loadingImageSrc;
		imageLoading.id = mainImageID;
	
		parentHandle.appendChild(imageLoading);
		// --- end IE bug --- //
	}
	
	var zoomImage = new Image();
	zoomImage.onload = function() {
		zoomImage.onload = null;
		var image = new Image();
		
		image.onload = function() {
			
			image.onload = null;
			getElement(mainImageID).src = imgSrc;
			zoom = MojoMagnify.makeMagnifiable(getElement(mainImageID),magnifySrc);
			
			// move stuff out of the way...
			var imageWidth;
			var imageHeight;
			var instructionsHeight;
			imageWidth = getElement(mainImageID).offsetWidth;
			imageHeight = getElement(mainImageID).offsetHeight;
			instructionsHeight = getElement(instructionsID).offsetHeight;
			if(imageWidth > 0){				
				getElement("ProductDetailLeft").style.width = imageWidth + "px";
			}	
			if(imageHeight > 0){	
				getElement("ProductMultipleImages").style.top = (imageHeight + instructionsHeight) + "px";
				getElement("ProductMultipleImages").style.display = "block";
			}
			
		};
		image.src = imgSrc;
		
	};
	zoomImage.src = magnifySrc;		
}

function getElement(name) {
	var o1 = null;
	if(document.all) {
		o1 = document.all(name);
	}
	else if(document.getElementById) {
		o1 = document.getElementById(name);
	}
	return o1;
}



/*Look up Account by Email address for quotes */

var formWaitingToBeSubmitted = null;

$(document).ready(function () {
    $('form[name=shipping]').submit(function () {        
        if (ajaxInProcess) {
            formWaitingToBeSubmitted = $(this);
            return false;
        }
    });
});

var inputAccountArr = new Array();

inputAccountArr[0] = "username";
inputAccountArr[1] = "bill_company";
inputAccountArr[2] = "bill_name";
inputAccountArr[3] = "bill_addr1";
inputAccountArr[4] = "bill_addr2";
inputAccountArr[5] = "bill_city";
inputAccountArr[6] = "bill_state";
inputAccountArr[7] = "bill_zip";
inputAccountArr[8] = "bill_phone";
inputAccountArr[9] = "bill_fax";

function checkEmailAddress() {
    var HasEmail1 = false;
    var EmailAddress = '';
    if (getElement("username")) {
        if (getElement("username").style.display != "none") {
            HasEmail1 = true;
            EmailAddress = getElement("username").value;
        }
    }
    if (HasEmail1) {
        ajaxInProcess = true;
        setTimeout('xmlhttpPost("' + EmailAddress + '","account");', 250);
    }
}


function xmlhttpPost(postData, lookupType) {

    startUpdate(postData, lookupType);
    
    var xmlHttpReq;
    
    if (window.XMLHttpRequest) { xmlHttpReq = new XMLHttpRequest(); }
    else if (window.ActiveXObject) { xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); }
    
    if (xmlHttpReq) {
        if (lookupType == "account") {
            xmlHttpReq.open("POST", "/MyAccount/ajax_AccountLookup.asp", true);
        }

        xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlHttpReq.onreadystatechange = function () {            
            if (xmlHttpReq.readyState == 4) {
                // modify this to pause long enough for "updating" message to appear and then hide it once the fields have been filled
                //alert(xmlHttpReq.responseText);
                if (lookupType == "account") {
                    setTimeout("updateInfo('" + escape(xmlHttpReq.responseText) + "','" + lookupType + "')", 1000);
                }
            }
        }

        if (lookupType == "account") {
            ajaxInProcess = true;
            xmlHttpReq.send("username=" + postData);
        }
    }
}

function updateInfo(responseText, lookupType) {

    var inputAccountArr = new Array();

    inputAccountArr[0] = "username";
    inputAccountArr[1] = "bill_company";
    inputAccountArr[2] = "bill_name";
    inputAccountArr[3] = "bill_addr1";
    inputAccountArr[4] = "bill_addr2";
    inputAccountArr[5] = "bill_city";
    inputAccountArr[6] = "bill_state";
    inputAccountArr[7] = "bill_zip";
    inputAccountArr[8] = "bill_phone";
    inputAccountArr[9] = "bill_fax";

    responseText = unescape(responseText);
    
    responseDataArr = null;
    responseDataArr = responseText.split("\t");

    var inputFieldArr = null;

    if (lookupType == "account") {
        inputFieldArr = inputAccountArr;
    }

    var InputName;
    var InputElement;
    var InputValue;
    var InputFound;
    
    if (responseDataArr != null && responseDataArr != '' && inputFieldArr != null) {
        var arLen = inputFieldArr.length;

        for (var i = 0, len = arLen; i < len; ++i) {
            InputName = inputFieldArr[i];
            InputElement = getElement(InputName);
            InputValue = responseDataArr[i];
            InputFound = false;
            if (InputValue != undefined) {//&& InputValue != '') {                
                if (InputElement != null && InputValue != undefined && InputElement.type != undefined) {                    
                    if (InputElement.type == "text") {
                        InputFound = true;
                        InputElement.value = InputValue;
                    }
                    else if (InputElement.type == "select-one") {
                        InputFound = true;
                        InputElement.value = InputValue;
                    }
                    else {
                        alert(InputName+".type = "+InputElement.type);
                    }
                }
                if (!InputFound) {
                    InputElement = getElement(InputName + "1");
                    if (InputElement != null) {
                        if (InputElement.type == "radio") {
                            //alert(InputName+"1.type = radio");
                            for (var j = 1; j < 20; ++j) {
                                InputElement = getElement(InputName + j);
                                if (InputElement != null) {
                                    if (InputElement.value == InputValue) {
                                        //alert(InputName+j+" checked ("+InputElement.value+" = "+InputValue+")");
                                        InputFound = true;
                                        InputElement.checked = true;
                                    }
                                    else {
                                        //alert(InputName+j+" unchecked");
                                        InputFound = true;
                                        InputElement.checked = false;
                                    }
                                }
                                else {
                                    //alert(InputName+j+" is null");
                                    break;
                                }
                            }
                        }
                        else {
                            //alert(InputName+"1.type = "+InputElement.type);
                        }
                    }
                    else {
                        //alert("element '"+InputName+"' not found");	
                    }
                }
            }
        }
    }
    stopUpdate();
}


function startUpdate(postData, lookupType) {
    if (getElement("boxtext") != null) {

        if (lookupType == "account") {
            getElement("boxtext").innerHTML = "Looking up contact information for<br><b>" + postData + "</b>";
        }
        else if (lookupType == "address") {
            getElement("boxtext").innerHTML = "Looking up address information for<br><b>" + postData + "</b>";
        }
    }
    showBox();
}

function stopUpdate() {
    var form;
    hideBox();
    ajaxInProcess = false;
    if (formWaitingToBeSubmitted != null) {
        form = formWaitingToBeSubmitted;
        formWaitingToBeSubmitted = null;
        form.submit();
    }
}


/*Lightbox*/
function showBox() {

    var o = null;

    if (document.all) { o = document.all("overlay"); }
    else if (document.getElementById) { o = document.getElementById("overlay"); }

    if (o != null && o.style) {
        o.style.display = "block";
    }

    center('box');
    return false;
}

function hideBox() {

    var o1 = null;
    var o2 = null;

    if (document.all) {
        o1 = document.all("box");
        o2 = document.all("overlay");
    }
    else if (document.getElementById) {
        o1 = document.getElementById("box");
        o2 = document.getElementById("overlay");
    }

    if (o1 != null && o1.style && o2 != null && o2.style) {
        o1.style.display = "none";
        o2.style.display = "none";
    }

    return false;
}

function center(element) {

    var o = null;

    if (document.all) { o = document.all(element); }
    else if (document.getElementById) { o = document.getElementById(element); }


    var my_width = 0;
    var my_height = 0;

    if (typeof (window.innerWidth) == 'number') {
        my_width = window.innerWidth;
        my_height = window.innerHeight;
    } else if (document.documentElement &&
             (document.documentElement.clientWidth ||
               document.documentElement.clientHeight)) {
        my_width = document.documentElement.clientWidth;
        my_height = document.documentElement.clientHeight;
    }
    else if (document.body &&
            (document.body.clientWidth || document.body.clientHeight)) {
        my_width = document.body.clientWidth;
        my_height = document.body.clientHeight;
    }

    o.style.position = 'absolute';
    o.style.zIndex = 99;

    var scrollY = 0;

    if (document.documentElement && document.documentElement.scrollTop) {
        scrollY = document.documentElement.scrollTop;
    } else if (document.body && document.body.scrollTop) {
        scrollY = document.body.scrollTop;
    } else if (window.pageYOffset) {
        scrollY = window.pageYOffset;
    } else if (window.scrollY) {
        scrollY = window.scrollY;
    }

    var elementDimensions = getDimensions(element);

    var setX = (my_width - elementDimensions[0]) / 2;
    var setY = (my_height - elementDimensions[1]) / 2 + scrollY;

    setX = (setX < 0) ? 0 : setX;
    setY = (setY < 0) ? 0 : setY;

    o.style.left = setX + "px";
    o.style.top = setY + "px";

    o.style.display = 'block';
}


function getDimensions(element) {

    var dimensionArr = new Array;

    var o = null;

    if (document.all) { o = document.all(element); }
    else if (document.getElementById) { o = document.getElementById(element); }


    if (o.style.display != 'none') {
        dimensionArr[0] = o.offsetWidth;
        dimensionArr[1] = o.offsetHeight;
        return dimensionArr;
    }

    // All *Width and *Height properties give 0 on elements with display none,
    // so enable the element temporarily
    var els = o.style;
    var originalVisibility = els.visibility;
    var originalPosition = els.position;
    els.visibility = 'hidden';
    els.position = 'absolute';
    els.display = '';
    var originalWidth = o.clientWidth;
    var originalHeight = o.clientHeight;
    els.display = 'none';
    els.position = originalPosition;
    els.visibility = originalVisibility;

    dimensionArr[0] = originalWidth;
    dimensionArr[1] = originalHeight;
    return dimensionArr;
}








  /* refine search */
  $(document).ready(function () {


  	if ($('.category-primary ul li').length > 0) {

  		$('.category-primary ul li').has('.refine-supplemental').each(function () {


  			$('.refine-more', this).attr('style', 'display:block;');
  			$('.refine-more', this).click(function () {
  				if ($(this).attr('class') == 'refine-more') {
  					$(this).html('<span>Hide</span>');
  					$(this).attr('class', 'refine-less');
  					$('ul li.refine-supplemental', $(this).parent()).attr('style', 'display:block;');
  				}
  				else {
  					$(this).html('<span>+View All</span>');
  					$(this).attr('class', 'refine-more');
  					$('ul li.refine-supplemental', $(this).parent()).attr('style', 'display:none;');
  				}

  			});
  		});
  		$('.category-primary li.refine-supplemental').each(function () {
  			$(this).attr('style', 'display:none;');
  		});
  		$('.category-primary a.refine-more').each(function () {
  			$(this).attr('href', 'javascript:void(0);');
  			$(this).html('<span>+View All</span>');
  		});
  	}


  	if ($('.category-secondary ul li').length > 0) {

  		$('.category-secondary ul li').has('.refine-supplemental').each(function () {


  			$('.refine-more', this).attr('style', 'display:block;');
  			$('.refine-more', this).click(function () {
  				if ($(this).attr('class') == 'refine-more') {
  					$(this).html('<span>Hide</span>');
  					$(this).attr('class', 'refine-less');
  					$('ul li.refine-supplemental', $(this).parent()).attr('style', 'display:block;');
  				}
  				else {
  					$(this).html('<span>+View All</span>');
  					$(this).attr('class', 'refine-more');
  					$('ul li.refine-supplemental', $(this).parent()).attr('style', 'display:none;');
  				}

  			});
  		});
  		$('.category-secondary li.refine-supplemental').each(function () {
  			$(this).attr('style', 'display:none;');
  		});
  		$('.category-secondary a.refine-more').each(function () {
  			$(this).attr('href', 'javascript:void(0);');
  			$(this).html('<span>+View All</span>');
  		});
  	}

  });


