function fixPNG(element){
	if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
		var src;
		if(element.tagName == 'IMG') {
			if(/\.png$/.test(element.src)) {
				src = element.src;
				element.src = '/i/t/spacer.gif';
			}
		}
		else {
			src = element.currentStyle.backgroundImage.match(/([^"'\(]+\.png)/i);
			if(src) {
				src = src[1];
				element.runtimeStyle.backgroundImage = 'none';
			}
		}
		if(src) {
			element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
		}
	}
}
function lbover(obj) {
	$(obj+'lt').style.background = "url(/i/t/bg_txt_blu2-left.jpg) no-repeat";	
	$(obj+'mid').style.background = "url(/i/t/bg_txt_blu2-repeat.jpg) repeat-x";
	$(obj+'rt').style.background = "url(/i/t/bg_txt_blu2-right.jpg) no-repeat";
	$(obj).style.background = "#f5f5f5";
	$(obj).style.color = "#6fbee6";
	$(obj).style.border = "#7ab9eb solid 1px";
}

function lbout(obj) {
	$(obj+'lt').style.background = "url(/i/t/bg_txt_grey1-left.jpg) no-repeat";	
	$(obj+'mid').style.background = "url(/i/t/bg_txt_grey1-repeat.jpg) repeat-x";
	$(obj+'rt').style.background = "url(/i/t/bg_txt_grey1-right.jpg) no-repeat";
	$(obj).style.background = "none";
	$(obj).style.color = "#8a8c8e";
	$(obj).style.border = "#fff solid 1px";
}
var AllowNewPopups = true; // Allow popup to happin
/**
 * This function allows the centering of objects based on where the client is viewing
 */
Position.GetWindowSize = function(w){
    w = w ? w : window;
    var width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
    var height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
    return [width, height];
};
Position.center = function(element){
    var copartCenterX = 260; // Added to adjust what 'center' was
    var copartCenterY = 220;
    var options = Object.extend({
        zIndex: 999,
        update: false
    }, arguments[1] ||
    {});
    element = $(element);
    if (!element._centered) {
        Element.setStyle(element, {
            position: 'absolute',
            zIndex: options.zIndex
        });
        element._centered = true;
    }
    var dims = Element.getDimensions(element);
    Position.prepare();
    var winWidth = self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0;
    var winHeight = self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0;
    var offLeft = (Position.deltaX + Math.floor((winWidth - dims.width) / 2)) - copartCenterX;
    var offTop = (Position.deltaY + Math.floor((winHeight - dims.height) / 2) - copartCenterY);
    element.style.top = ((offTop !== null && offTop > 0) ? offTop : '0') + 'px';
    element.style.left = ((offLeft !== null && offLeft > 0) ? offLeft : '0') + 'px';
    if (options.update) {
        Event.observe(window, 'resize', function(evt){
            Position.center(element);
        }, false);
        Event.observe(window, 'scroll', function(evt){
            Position.center(element);
        }, false);
    }
};

/**
 *  This function will remove a comma seperated collection of elements from the page's DOM
 *  @param obj - is the comma separated object ids that will be removed from DOM
 *
 */
function removeElement(objs){
	// Force selects visible.
    toggleSelects('visible');
    
    if (objs.length > 0) {
        var splitObj = objs.split(",");
        for (var i = 0; i < splitObj.length; i++) {
            if ($(splitObj[i]) !== null) {
                document.body.removeChild($(splitObj[i]));
            }
        }
    }
}


/**
 Show/Hide selects in IE.
 This allows the divs to be displayed above select elements.
 If 'hidden' is specified, the selects will be hidden.
 If 'visible' is specified, the selects will be visible.
 
 Default behavior is to toggle between visible and hidden.
 */
function toggleSelects(f){
    var action;
    f = f ? f : '';
    
    if (navigator.userAgent.indexOf("MSIE") !== -1) {
        for (var S = 0; S < document.forms.length; S++) {
            for (var R = 0; R < document.forms[S].length; R++) {
                if (document.forms[S].elements[R].options) {
                
                    if (f !== '' && f == 'hidden') {
                        action = 'hidden';
                    }
                    if (f !== '' && f == 'visible') {
                        action = 'visible';
                    }
                    
                    if (f === '') {
                        if (document.forms[S].elements[R].style.visibility == 'visible' ||
                        document.forms[S].elements[R].style.visibility === '') {
                            action = 'hidden';
                        }
                        else {
                            action = 'visible';
                        }
                    }
                    document.forms[S].elements[R].style.visibility = action;
                }
            }
        }
    }
}
function $E(data){
    var el;
    if ('string' == typeof data) {
        el = document.createTextNode(data);
    }
    else {
        //create the element
        el = document.createElement(data.tag);
        delete (data.tag);
        
        // append the children
        if ('undefined' != typeof data.children) {
            if ('string' == typeof data.children ||
            'undefined' == typeof data.children.length) {
                //strings and single elements
                el.appendChild($E(data.children));
            }
            else {
                //arrays of elements
                for (var i = 0, child = null; 'undefined' != typeof(child = data.children[i]); i++) {
                    el.appendChild($E(child));
                }
            }
            delete (data.children);
        }
        
        //any other data is attributes
        for (attr in data) {
            el[attr] = data[attr];
        }
    }
    
    return el;
}

function getFormattedXMLContent(xmlString, xslString){
	var xmlDoc;
	var xslDoc;
    //If Moz/Opera
    if (document.implementation && document.implementation.createDocument) {
        if ("string" == typeof xmlString) {
            xmlDoc = returnXMLDocument(xmlString);
        }
        else {
            xmlDoc = xmlString;
        }
        if ("string" == typeof xslString) {
            xslDoc = returnXMLDocument(xslString);
        }
        else {
            xslDoc = xslString;
        }
        if (isError(xmlDoc, xslDoc)) {
            return errorNotSupported;
        }
        var xsltProcessor = new XSLTProcessor();
        xsltProcessor.importStylesheet(xslDoc);
        return xsltProcessor.transformToFragment(xmlDoc, document);
    }
    //If IE
    else 
        if (window.ActiveXObject) {
            if ("string" == typeof xmlString) {
                xmlDoc = returnXMLDocument(xmlString);
            }
            else {
                xmlDoc = xmlString;
            }
            if ("string" == typeof xslString) {
                xslDoc = returnXMLDocument(xslString);
            }
            else {
                xslDoc = xslString;
            }
            if (isError(xmlDoc, xslDoc)) {
                return errorNotSupported;
            }
            return xmlDoc.transformNode(xslDoc);
        }
        else {
            return errorNotSupported;
        }
    
    function isError(xml, xsl){
        //Check that both items are objects
        if ((xml && "object" == typeof xml) &&
        (xsl && "object" == typeof xsl)) {
            return false;
        }
        return true;
    }
	return '';
}
/**
 * This function takes a string and attempts to convert it to an xml document.
 */
function returnXMLDocument(strDocument){
	var xmlDocument = '';
	//If Mozilla/Opera
    if (document.implementation && document.implementation.createDocument) {
        var domParser = new DOMParser();
        xmlDocument = domParser.parseFromString(strDocument, "text/xml");
        return xmlDocument;
    }
    //If IE
    else 
        if (window.ActiveXObject) {
            xmlDocument = new ActiveXObject("Microsoft.XMLDOM");
            xmlDocument.async = "false";
            xmlDocument.loadXML(strDocument);
            return xmlDocument;
        }
        //Not Supported
        else {
            return errorNotSupported;
        }
}
/**
 * View PDF Files in new Window
 */
function viewPdf(url){
    var winPdf = window.open(url, 'ViewPDF', "width=680, height=500, scrollbars=yes, menubar=no, location=no, status=no, resizable=yes, toolbars=no");
}
/**
 *  This function is a wrapper for the facility info div.
 *
 * @param yardNumber - The yard to show the facility info for.
 */
function getFacilityInfo(yardNumber){
    getInfoDivPage('carfinder-online-auto-auctions/ajax/facility/' + yardNumber, 'Divs/facilityInfo');
}
/**
 *  This function is to be used to create informational/error message div/popups.
 * @param controller - passable url to a different controller, because some
 *                     div will need to do some processing and wont be able to
 *                     use the standard static controller.
 * @param view       - path and file name of the ftl to use as the view
 * @return           - none
 */
function getInfoDivPage(controller, view, method){
    method = method ? method : "get";
    var params = null;
    createInfoDivPopup(controller, view, params, false, method);
}
/**
 *  This function is responsible for creating the actual ajax divs.
 *  This function will create 2 divs in the DOM, the msgDisable that captures page
 *  clicks (not on the message div) and closes the dynamic divs, a info-div-global
 *  div that is a container that the AJAX (H) response will be placed into.
 */
function createInfoDivPopup(controller, view, params, isLogout, method, callBack){
    if (!AllowNewPopups) {
        return;
    }
    method = method ? method : "get";
    // If there is already a popup open, close it
    if ($("info-div-global") !== null) {
        removeElement('info-div-global,msgDisabler');
    }
    
    // Hide selects for IE.
    toggleSelects('hidden');
    
    if (controller === null) {
        controller = "info";
    }
    if (params === null) {
        params = "";
    }
    else {
        params = "&" + params;
    }
    
    var msgBg = $E({
        tag: "div",
        id: "msgDisabler"
    });
    if (isLogout) {
        msgBg.setAttribute("onclick", "logoff('info-div-global,msgDisabler');");
        msgBg.onclick = function(){
            logoff('info-div-global,msgDisabler');
        };
    }
    else {
        msgBg.setAttribute("onclick", "removeElement('info-div-global,msgDisabler');");
        msgBg.onclick = function(){
            removeElement('info-div-global,msgDisabler');
        };
    }
    new Ajax.Request('/'+controller+'/', {
        method: method,
        parameters: "view=" + view + params,
        onSuccess: function(transport){
            var infoContainer = $E({
                tag: "div",
                id: "info-div-global"
            });
            document.body.appendChild(msgBg);
            document.body.appendChild(infoContainer);
            $("info-div-global").innerHTML = transport.responseText;
            var draggableMsg = new Draggable("information-div", {
                handle: "information-div-titleBar"
            });
            Position.center(infoContainer);
            
            if (callBack) {
                callBack();
            }
        },
        onFailure: function(transport){
            redirectOnFailure(view);
        },
        onException: function(transport){
            redirectOnFailure(view);
        }
    });
}
function redirectOnFailure(view){
    if (view == 'Divs/logoff') {
        logoff('info-div-global,msgDisabler');
    }
    else {
        redirect('info-div-global,msgDisabler', 'carfinder-online-auto-auctions/');
    }
}
function redirect(objs, url){
/*
	removeElement(objs);
    var logoutURL = url;
    var tempURL = window.location.href;
    var locationOfQueryString = tempURL.indexOf("?");
    
    if (locationOfQueryString > -1) {
        tempURL = tempURL.substring(0, locationOfQueryString);
    }
    var splitURL = tempURL.split("/");
    var urlLength = splitURL.length; // Split the URL cause we dont need the
    // page url
    splitURL[urlLength - 1] = logoutURL; // Set the last element to the
    // logout url
    var endURL = "";
    // Loop through the items in the split url array
    for (var i = 0; i < urlLength; i++) {
        // Rebuild the url
        endURL = endURL + splitURL[i];
        if (i === 0) // If we are at first element 'http' the add an additional /
        {
            endURL = endURL + "/";
        }
        else // Just add / to create a properly formed url
        {
            endURL = endURL + "/";
        }
    }
    //Trim off the last /
    var trimmedURL = endURL.substring(0, endURL.length - 1);
    // Set window url to logoff user
    window.location.href = trimmedURL;
*/
	return;
}
function trimWS(strValue){
    var objRegExp = /^(\s*)$/;
    // check for all spaces
    if (objRegExp.test(strValue)) {
        strValue = strValue.replace(objRegExp, '');
        if (strValue.length === 0) {
            return strValue;
        }
    }
    //check for leading & trailing spaces
    objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
    if (objRegExp.test(strValue)) {
        //remove leading and trailing whitespace characters
        strValue = strValue.replace(objRegExp, '$2');
    }
    return strValue;
}
function setSelectValue(SelectName, value){
    var selectObj = $(SelectName);
    if ((selectObj.selectedIndex > -1) && (selectObj[selectObj.selectedIndex].value == value)) {
        return false;
    }
    for (var i = 0; i < selectObj.length; i++) {
        var m = selectObj[i].value;
        if (selectObj[i].value == value) {
            selectObj.selectedIndex = i;
            return true;
        }
    }
    return false;
}
function getSelectedValue(SelectName){
    var selectObj = $(SelectName);
    var returnValue = "";
    try {
        returnValue = selectObj[selectObj.selectedIndex].value;
    } 
    catch (err) {
    }
    return returnValue;
}
function setCookie(name, value, expires, path, domain, secure){
    var today = new Date();
    today.setTime(today.getTime());
    
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));
    
    document.cookie = name + "=" + escape(value) +
    ((expires) ? ";expires=" + expires_date.toGMTString() : "") +
    ((path) ? ";path=" + path : "") +
    ((domain) ? ";domain=" + domain : "") +
    ((secure) ? ";secure" : "");
    
}

function getCookie(c_name){
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) {
            	c_end = document.cookie.length;
			}
		   return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function isZipValid(zip){
    // strip leading trailing spaces
    var objRegExp;
    zip = trimWS(zip);
    
    // check for US zipcode
    objRegExp = new RegExp("^[0-9]{5}$");
    if (objRegExp.test(zip)) {
        return true;
    }
    
    // check for canada zipcode short
    objRegExp = new RegExp("^[a-z]\\d[a-z]$", "i");
    if (objRegExp.test(zip)) {
        return true;
    }
    
    // check for canada zipcode long
    objRegExp = new RegExp("^[a-zA-Z]\\d[a-zA-Z]\\d[a-zA-Z]\\d$", "i");
    if (objRegExp.test(zip)) {
        return true;
    }
    
    // not us or canada zipcode
    return false;
}

function login_show(){
	$('login_container').style.display='block';
}
function login_hide(){
	$('login_container').style.display='none';
}
var preloaded = new Array();
function preload_images() {
    for (var i = 0; i < arguments.length; i++){
        preloaded[i] = document.createElement('img');
        preloaded[i].setAttribute('src',arguments[i]);
    }
}
preload_images(
    '/i/t/bg_m1_blue.png',
    '/i/t/bg_m1_blue2.png',
    '/i/t/bg_m3_red.png');

Ajax.Responders.register({
	  onCreate: function() {
		//$("loading_container").style.display = 'block';
	  },
	  onComplete: function() {
		//$("loading_container").style.display = 'none';
	  }
});
function CopartVideo()
{
	$('flashVideo').style.background = 'url(/i/t/spacer.gif)';
	var so2 = new SWFObject('/i/flash/copartHome.swf', 'copartVideo', '350', '260', '8.0.23', "#FFFFFF");
	so2.addParam("wmode", "transparent");
	so2.write("flashVideo");
}
Event.observe(window, "load", function() {
	if ($('languageFilterDiv') !== null && $('languageFilterDiv') !== undefined) {
		Event.observe($('languageFilterPopup'), 'change', languageFilter);
		Event.observe($('languageSelector'), 'mouseover', languageFilterOver);
		Event.observe($('languageSelector'), 'mouseout', languageFilterOut);
	}
});