// *****************************************************************************************
// common.js
// *****************************************************************************************
// returns an object if it exists in the current DOM displayed, or null if it doesn't
	function buildObj(sObject){
		// if netscape build the correct path to the element in the DOM
		if (!document.all){
			var aObjTree = sObject.split("."); // split and store the ie version of the DOM tree
			var sTree = "";

			// when the tree is long, construct a new tree out of the split apart ie tree
			if (aObjTree.length > 1){
				for (var i=0; i<aObjTree.length; i++){
					if ((i+1)%2 == 0) sTree += ".document."; // inserts ref to the DOM document
					if (i < aObjTree.length -1) sTree += aObjTree[i]; // skips the last element
				}

			// just start a basic NS tree
			} else sTree += "document.";

			// reconstruct the sObject for a correct NS path to the element
			sObject = (sTree + "getElementById('"+ aObjTree[aObjTree.length -1] +"')");
		}

		// return the element or null if it doesn't exist
		return oCreatedObj = (eval("typeof("+sObject+")") != "undefined")? (eval(sObject)):null;
	}

    //  Legacy Javascript for enforcing maxlength 
    function maxlengthCheck(txt, maxlength){
        if(txt.value.length > maxlength){
            txt.value = txt.value.substring(0,maxlength);
        }
    }
    
    
// ************************************************
// Added 5.20.08 - bjackson - implementing a global replacement of umlaut 
// characters on all site form input and textarea elements
// ************************************************

// function to swap unlaut characters for their roman counterparts
// this replaces in ALL text fields within the document
// not just the submitted form to handle any javascript form submissions that
// may be in place with inline submit handlers
function SwapUmlauts() {
   var r  = new Object();
   r["ä"] =  "ae";
   r["ö"] =  "oe";
   r["ü"] =  "ue";
   r["Ä"] =  "Ae";
   r["Ö"] =  "Oe";
   r["Ü"] =  "Ue";
   r["ß"] =  "ss";   
   
   var inputs = document.getElementsByTagName('input');
   for(var i=0;i<inputs.length;i++){
      var input = inputs[i];
      if(input.type == 'text'){
        for(c in r){
            if(input.value.indexOf(c) != -1){
                var re = new RegExp(c,"g");
                var v = input.value.replace(re,r[c]);
                input.value = v;
            }
        }           
      }
   }
}

// Cross-browser implementation of element.addEventListener()
function addListener(element, type, expression, bubbling){
      bubbling = bubbling || false;
      if(window.addEventListener) { // Standard
              element.addEventListener(type, expression, bubbling);
              return true;
      } else if(window.attachEvent) { // IE
              element.attachEvent('on' + type, expression);
              return true;
      } else return false;
}

// Sets event handlers for all form submits to the umlaut replacement script
function SetGlobalSubmitHandle(){
     for(var i=0;i<document.forms.length;i++){
         addListener(document.forms[i],'submit',SwapUmlauts);
     }       
}

// on document load, add submit listeners to any form to call umlaut repalcement script globally
addListener(window,'load',SetGlobalSubmitHandle);


// Plugin to the jquery base script for parsing csv/tab delimited files
/* Usage:
 *  jQuery.csv()(csvtext)               returns an array of arrays representing the CSV text.
 *  jQuery.csv("\t")(tsvtext)           uses Tab as a delimiter (comma is the default)
 *  jQuery.csv("\t", "'")(tsvtext)      uses a single quote as the quote character instead of double quotes
 *  jQuery.csv("\t", "'\"")(tsvtext)    uses single & double quotes as the quote character
 *  Bjackson 9.22.09
 * */
jQuery.extend({
    csv: function(delim, quote, lined) {
        delim = typeof delim == "string" ? new RegExp( "[" + (delim || ","   ) + "]" ) : typeof delim == "undefined" ? ","    : delim;
        quote = typeof quote == "string" ? new RegExp("^[" + (quote || '"'   ) + "]" ) : typeof quote == "undefined" ? '"'    : quote;
        lined = typeof lined == "string" ? new RegExp( "[" + (lined || "\r\n") + "]+") : typeof lined == "undefined" ? "\r\n" : lined;

        function splitline (v) {
            // Split the line using the delimiter
            var arr  = v.split(delim),
                out = [], q;
            for (var i=0, l=arr.length; i<l; i++) {
                if (q = arr[i].match(quote)) {
                    for (j=i; j<l; j++) {
                        if (arr[j].charAt(arr[j].length-1) == q[0]) { break; }
                    }
                    var s = arr.slice(i,j+1).join(delim);
                    out.push(s.substr(1,s.length-2));
                    i = j;
                }
                else { out.push(arr[i]); }
            }

            return out;
        }

        return function(text) {
            var lines = text.split(lined);
            for (var i=0, l=lines.length; i<l; i++) {
                lines[i] = splitline(lines[i]);
            }
            return lines;
        };
    }
});


// Netpromoter display script, parses a given number of reviews from
// the specified tab delimited file and shows them. Developed for easy
// fill slot use - file must reside on the same domain as calling script.
// bjackson 9.22.09
function showNetReviews(num,file){

    //Ajax call to retrieve file and parse
    jQuery.get(file, function(data) {
        reviewData  = jQuery.csv()(data);
        var rdiv     = $('#__netreviews');
       var stop     = Math.min(num, reviewData.length-1)
        if(rdiv != null){
            rdiv.html('');
            //ignore first line headers
            for(var i=1;i<=stop;i++){
                var date        = reviewData[i][0] || "9/28/2009";
                var user        = reviewData[i][1] || "Unknown User";
                var location    = reviewData[i][2] || "Unkown Location";
                var rating      = reviewData[i][3] || "10";
                var comment     = reviewData[i][4] || "";
                var insert      = "<div class=\"netpromoter-review\"><div class=\"rating-static rating-"+((rating/2)*10)+"\"></div>";
                    insert      +="By <strong>"+user+"</strong> from <strong>"+location+"</strong> on <strong>"+date+"</strong><p>"+comment+"</p></div>";
                rdiv.append(insert);

        }
        }
    });
}


// *****************************************************************************************
// flyopen.js
// *****************************************************************************************
/*
(C) Copyright MarketLive. 2006. All rights reserved.
MarketLive is a trademark of MarketLive, Inc.
Warning: This computer program is protected by copyright law and international treaties.
Unauthorized reproduction or distribution of this program, or any portion of it, may result
in severe civil and criminal penalties, and will be prosecuted to the maximum extent
possible under the law.
*/
function flyopen(width, height){
    width = (width && !isNaN(width))? width:null;
    height = (height && !isNaN(height))? height:null;
    var winURL = (arguments[2])? arguments[2]:null;
    var winName = (arguments[3])? arguments[3]:"generic";

	//only launch the window if we've got a width, height and url
	if (width && height && winURL){
        var ieIncrement = ((navigator.appName+"").indexOf("Netscape") == -1)? 15:0;
        eval(winName+"=window.open('"+ winURL +"','"+ winName +"','resizable=yes,scrollbars=yes,width="+ (width + ieIncrement) +",height="+ (height + ieIncrement) +",top=5,left=75')");
        eval("window."+ winName +".focus()");
	}
}


// *****************************************************************************************
// rollover.js
// *****************************************************************************************
/** Img rollover function. **/
function rollover(name,source){
    document.images[name].src=source;
}

/** Preload image function: takes an array of img srouces. **/
function preload(aImgSrc){
    for (var i=0; i<aImgSrc.length; i++){
        var newImg = new Image();
        newImg.src = aImgSrc[i];
        aImgSrc["img_"+i] = newImg;
     }
}



// *****************************************************************************************
// cleartext.js
// *****************************************************************************************
/* clears text of passed in object */
function clearIt(txt) {
    txt.value = "";
}
