//contains common displays (address, distance, etc)
//common_dom.js also needs to be included

var ExtraInfo_Array = new Array(2)    // increase if needed
var ExtraInfo_TxtArray = new Array(2)    // must be of same array size as above

ExtraInfo_Array[0] = "212185,212186,304566,212184,308512,212188,212189,212190,212298,212187"
ExtraInfo_Array[1] = "212367,212364,212362,212366,304578"

ExtraInfo_TxtArray[0] = "http://www.xpresslubealabama.com/"
ExtraInfo_TxtArray[1] = "http://www.xpresslubetupelo.com/"

function isCurrentPromo(locHandle) {
    // should only check for the current promotion array here
		   var curPromo = locHandle.recordId
		   for (var k=0; k < ExtraInfo_Array.length; k++) {
		       if (ExtraInfo_Array[k].length > 0) {
		          if (ExtraInfo_Array[k].lastIndexOf(curPromo) > -1) {  return ExtraInfo_TxtArray[k] }
			   }
			}   
			return ""
}

if (!Array.prototype.push) Array.prototype.push = function() {
    for (var i=0; i<arguments.length; i++) this[this.length] = arguments[i];
    return this.length;
}

function mq_ParamExists (varname) {
    var undef;
    return (varname !== undef);
}

//function used to display the outputting of a location in block format
function mq_display_address_location(container, loc, strURL) {
    var bSpace=false;

    if(!mq_ParamExists(loc))
        return;

    var div = createDiv (container, '', '');

    if (mq_ParamExists(loc.name) && loc.name.length) {
        if(strURL != "") {		
            var a = createA (div, strURL + '&ph=350&pw=425&postalCode=' + loc.postalCode, '');
            var ex = a.appendChild(document.createElement ('em'));
			ex.appendChild(document.createTextNode(loc.name));
			div.appendChild (document.createElement ('br')); 
			var em = div.appendChild (document.createElement ('strong'));
			em.appendChild (document.createTextNode ('Texaco XpressLube'));
			div.appendChild (document.createElement ('br'));
		} else {
            var span = createSpan (div, '', '');
            var em = span.appendChild (document.createElement ('strong'));
                // loc.name
			em.appendChild (document.createTextNode ('Texaco XpressLube'));
            div.appendChild (document.createElement ('br'));
        }
     }

     if (mq_ParamExists(loc.address) && loc.address.length) {
         div.appendChild (document.createTextNode (loc.address));
         div.appendChild (document.createElement ('br'));
     }

     if (mq_ParamExists(loc.city) && loc.city.length)
         div.appendChild (document.createTextNode (loc.city + ','));

     if (mq_ParamExists(loc.stateProvince) && loc.stateProvince.length) {
         div.appendChild (document.createTextNode (loc.stateProvince));
         bSpace=true;
     }
     if (mq_ParamExists(loc.postalCode) && loc.postalCode.length) {
         if (bSpace) {
             div.appendChild (document.createTextNode (' '));
         }
         div.appendChild (document.createTextNode (loc.postalCode));
     }

     if (mq_ParamExists(loc.country) && loc.country.length) {
         if(bSpace) {
             div.appendChild (document.createTextNode (' '));
         }
         div.appendChild (document.createTextNode (loc.country));
     }

     if (mq_ParamExists(loc.userFields) && mq_ParamExists(loc.userFields.user1) && loc.userFields.user1.length) {	     
	     div.appendChild (document.createElement ('br'));
         div.appendChild (document.createTextNode ('Tel: ' + loc.userFields.user1));
     } 
	 	 
        // checking for special output text content for certain stations
		var retTEXT = isCurrentPromo(loc);
	     var a = ""
	  if ( retTEXT != "") {
         div.appendChild (document.createElement ('br'));
		 a = createA (div, retTEXT, 'link to website');
		a.appendChild(document.createTextNode ("visit website"));
		div.appendChild(a); 	  
	  }
		
				 
}

//this function is used to display the address in the single line format
function mq_display_address_location_single_line(container, loc, image, width, height, label) {
    var bSpace=false;

    if (arguments.length < 6)
        label = '';

    if (!mq_ParamExists(loc))
        return;

    var div = createDiv (container, '', '');

    if (image != "")
        createImg (div, "/images/" + image, width, height, '', '', 'address icon');

    if (label != "")
        div.appendChild (document.createTextNode (' ' + label));

    if (mq_ParamExists(loc.address) && loc.address.length)
        div.appendChild (document.createTextNode (' ' + loc.address));

    if (mq_ParamExists(loc.city) && loc.city.length)
        div.appendChild (document.createTextNode (' ' + loc.city + " "));

    if (mq_ParamExists(loc.stateProvince) && loc.stateProvince.length) {
        div.appendChild (document.createTextNode (loc.stateProvince));
        bSpace=true;
    }
    if (mq_ParamExists(loc.postalCode) && loc.postalCode.length) {
        if(bSpace)
        {
            div.appendChild (document.createTextNode (" "));
        }
        div.appendChild (document.createTextNode (loc.postalCode));
    }
    if (mq_ParamExists(loc.country) && loc.country.length) {
        if(bSpace) {
            div.appendChild (document.createTextNode (" "));
        }
        div.appendChild (document.createTextNode (loc.country));
    }
}

//function used to format a number (num) to x(dec) decimal places
function formatNumber(num,dec) {
   return Math.floor(num * Math.pow(10,dec))/Math.pow(10,dec);
}

//function displays the time in format x hours, x.xx minutes or x.xx minutes
function mq_display_time(totalTime) {
   var newTime;
   // more than a minute
   if(totalTime > 3600)
   {
      newTime = totalTime/3600;
      var result = (" " + Math.floor(newTime) + " hours,");
      newTime = (totalTime/60)%60;
      result += (" " + Math.floor(newTime) + " minutes");
      return result;
   }
   if(totalTime > 60)
   {
      newTime = totalTime/60;
      return (" " + Math.floor(newTime) + " minutes");
   }
}

//function used to display distance formated in a div with right alignment.
//outputs miles or kilometers with 2 digits of precision
function mq_display_distance(totalDistance) {
   return (" " + totalDistance.value + (totalDistance.units == "mi" ? " miles" : " kilometers"));
}

/*
 * functions to make non-dom compliant browsers behave
 */
function W3CDOM_Event(currentTarget) {
    this.currentTarget  = currentTarget;
    this.preventDefault = function() { window.event.returnValue = false }
    this.clientX = window.event.clientX;
    this.clientY = window.event.clientY;
    this.which = window.event.button;
    return this;
}

function add_listener (object, event, callback)
{
    if (object != null) {
        if (object.addEventListener) {  // w3c DOM
            object.addEventListener (event, callback, false);
        } else if (object.attachEvent) { // IE
            object.attachEvent ('on' + event, function () {callback (new W3CDOM_Event (object))});
        }
    }
}