/**
*Common declarations of global variables
*/
var months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var timeOfDay = new Array("Morning", "Afternoon", "Evening");
//expiry days for the zipcode cookie.
var ZIPCODECOOKIEEXPIREDAYS = 7;


/**
 * Common javascript functions.
 * @param myLocation the my location
 * @param nWidth the browser width
 * @param nHeight the browser height
 */
function callPopup(myLocation, nWidth, nHeight)
{
  var sOptions = "menubar=1,toolbar=1,location=1,directories=0,status=1,scrollbars=1,resizable=1,width=" + nWidth + ",height=" + nHeight;
  window.open(myLocation, 'popupWndow', sOptions);
}

/**
 * Sets a Cookie
 * @param c_name the cname
 * @param value the value
 * @param expiredays the expire days
 */
function setCookie(c_name,value,expiredays)
{
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

/**
 * A light representation of the Dealer java object.
 * @param strPACode the dealer PA code
 * @param strName the name
 * @param strPostalCode the postal code
 * @param strFax the fax number
 * @param strSubdomain the subdomain
 * @param strSourceOfData the source of data
 * @param strCountry the country
 * @param strState the state
 */
function Dealer(strPACode, strName, strPostalCode, strFax, strSubdomain, strSourceOfData, strCountry, strState)
{
  this.paCode = strPACode;
  this.name = strName;
  this.postalCode = strPostalCode;
  this.fax = strFax;
  this.subdomain = strSubdomain;
  this.sourceOfData = strSourceOfData;
  this.country = strCountry;
  this.state = strState;
}

/**
 * Object to hold some relevant information about a model
 * @param strId the ID
 * @param strModelName the model name
 * @param strYear the year of the model
 * @param strLocateId the locate id
 * @param strMake the make of the model
 * @param strBasePrice the base price of the model
 * @param defaultImage the default image of the model
 */
function ModelInfo(strId, strModelName, strYear, strLocateId, strMake, strBasePrice, defaultImage)
{
  this.id = strId;
  this.name = strModelName;
  this.year = strYear;
  this.locateId = strLocateId;
  this.make = strMake;
  this.basePrice = strBasePrice;
  this.defaultImage = defaultImage;
}

/*
 * Object to hold some relevant information about the vehicle details
 * obtained from locate service.
 * @param modelName the model name
 * @param price the price of the model
 * @param bodyDesc the description of the model
 * @param vin the VIN number of the model
 * @param trimCode the trim code of the model
 * @param autoTrans the auto transmission of the model
 * @param engineCode the engine code of the model
 * @param mileage the mileage of the model
 * @param make the make of the model
 * @param year the year of the model
 */
function LocateVehicleDetails(modelName, price, bodyDesc, vin, trimCode, autoTrans, engineCode, mileage, make, year)
{
  this.modelName = modelName;
  this.price = price;
  this.bodyDesc = bodyDesc;
  this.vin = vin;
  this.trimCode = trimCode;
  this.autoTrans = autoTrans;
  this.engineCode = engineCode;
  this.mileage = mileage;
  this.make = make;
  this.year = year;
}

/**
 * Format a number of any type to a style like XX,XXX
 * @param strValue the value of the currency to format
 */
function currencyFormat(strValue)
{
  return numberFormat(strValue,",");
}

/**
 * Format the mileage to a style like XX,XXX
 * @param strValue the value of the milage to format
 */
function mileageFormat(strValue)
{
  return numberFormat(strValue,",");
}

/**
 * Formats the number as XX<separator>XXX
 * @param strValue the numebr value to format
 * @param separator the separator
 */
function numberFormat(strValue, separator)
{
  // Force to an int
  var intVal = Math.round(strValue);
  var strCopy = new String(intVal);
  var arrChunks = [];
  while(strCopy.length > 3)
  {
        var chunk = strCopy.substr(strCopy.length-3);
        arrChunks.unshift(chunk);
        strCopy = strCopy.substr(0,strCopy.length-3);
  }
  if(strCopy.length > 0) { arrChunks.unshift(strCopy); }
  strCopy = arrChunks.join(separator);
  return strCopy;
}


/**
 * Show a iframe mask layer behind a floater div.  This is
 * required because in IE elements like select boxes will show
 * above absolutely positioned floater divs.
 * Make sure there is no padding on the div containing the IFrame
 * or else not everything will get masked.
 * @param strMaskId - ID of the div containing the iframe
 * @param strDivId - ID of the content div, used to copy it's position
 *  and size
 * @param intZIndex - zIndex of the mask div, should be less than the content.
 */
function showIFrameMask(strMaskId, strDivId)
{
  var layer = document.getElementById(strDivId);
  layer.style.display = 'block';
  // show iframe
  var iframe = document.getElementById(strMaskId);
  iframe.style.visibility = 'visible';
  iframe.style.width = layer.offsetWidth;
  iframe.style.height = layer.offsetHeight;
  iframe.style.left = layer.offsetLeft;
  iframe.style.top = layer.offsetTop;
}

/**
 * Hides the iframe for given id
 * @param strMaskId the html id to be hidden
 */
function hideIFrameMask(strMaskId)
{
  var iframe = document.getElementById(strMaskId);
  iframe.style.visibility = 'hidden';
}

/**
 * A function to build an associative array of request parameters.
 */
function getRequestParams()
{
  var params = new Array();
  if (window.location.search.length < 2)
  {
    return params;
  }
  // Split the string by & and then =
  var pairs = window.location.search.substring(1).split("&");
  for (var i = 0; i < pairs.length; i++)
  {
    var arrPair = pairs[i].split("=");
    if (arrPair.length == 1)
    {
      params[arrPair[0]] = null;
    }
    else
    {
      params[arrPair[0]] = arrPair[1];
    }
  }
  return params;
}

// RFP FDDS - 12202 - Improve UI for credit link ZIP popup begins
var finURL;
var newOrUsed;
var vehicleDetails;
var modelInfo;
var isPEorCA = null;

/**
 * To launch the payment estimator.
 * @param finURL the external base service url we use.
 * @param newOrUsed 'N' if a new vehicle, 'U' otherwise.
 * @param vehicleDetails the vehicle details obtained from locate service.
 * @param modelInfo the model information.
 * @param vIndex index of the selected vehicle.
 */
function openPaymentEstimator(finURL, newOrUsed, vehicleDetails, modelInfo, popUpPositionID)
{
  this.finURL=finURL;
  this.newOrUsed=newOrUsed;
  this.vehicleDetails=vehicleDetails;
  this.modelInfo=modelInfo;
  isPEorCA = "PE";
  if (dealer.country == 'us')
  {
    setPrompt(arrStrings['apps.inventory.validzipCode.prompt'],false);
    openPrompt(popUpPositionID);
  }
  if (dealer.country == 'ca')
  {
    setPrompt(arrStrings['apps.inventory.validstate.prompt'],false);
    openPrompt(popUpPositionID);
  }
}

/**
 * Opens the Payment Estimator application
 * @param finURL the external base service url we use.
 * @param newOrUsed 'N' if a new vehicle, 'U' otherwise.
 * @param vehicleDetails the vehicle details obtained from locate service.
 * @param modelInfo the model information.
 * @param zipCode the zip code .
 * @param stateCode the state code
 */
function openPaymentEstimatorURL(finURL, newOrUsed, vehicleDetails, modelInfo, zipCode, stateCode)
{
  var strURL = 'https://' + finURL + '/aaui?ATD_SERVLET_ACTION=PE&ATD_SERVLET_PAGE_CLASS=AAExternal&AirCond=Y&CountryCode=' + getFMCCCountryCode(dealer) + '&LanguageCode=' + userLanguage + '&InterfaceHomeURL=http://' + dealer.subdomain + '.' + dealersitesSubdomainBase + '&SourceOfData=' + dealer.sourceOfData;
  strURL = createURL(strURL, newOrUsed, vehicleDetails, modelInfo, zipCode, stateCode);
  callPopup(strURL, 800, 600);
}

/**
 * Opens up the credit application in a popup.
 * @param finURL the external base service url we use.
 * @param newOrUsed 'N' if a new vehicle, 'U' otherwise.
 * @param vehicleDetails the vehicle details obtained from locate service.
 * @param modelInfo the model information.
 */
function openCreditApplication(finURL, newOrUsed, vehicleDetails, modelInfo, popUpPositionID)
{
  this.finURL=finURL;
  this.newOrUsed=newOrUsed;
  this.vehicleDetails=vehicleDetails;
  this.modelInfo=modelInfo;
  isPEorCA = "CA";
  if (dealer.country == 'us')
  {
    setPrompt(arrStrings['apps.inventory.validzipCode.prompt'],false);
    openPrompt(popUpPositionID);
  }
  if (dealer.country == 'ca')
  {
    setPrompt(arrStrings['apps.inventory.validstate.prompt'],false);
    openPrompt(popUpPositionID);
  }
}
/**
 * Opens the finance credit application
 * @param finURL the external base service url we use.
 * @param newOrUsed 'N' if a new vehicle, 'U' otherwise.
 * @param vehicleDetails the vehicle details obtained from locate service.
 * @param modelInfo the model information.
 * @param zipCode the zip code .
 * @param stateCode the state code
 */
function openCreditApplicationURL(finURL, newOrUsed, vehicleDetails, modelInfo, zipCode, stateCode)
{
  var dynLink = '/ovfui/fcovfui';
  var atdServletAction = 'CA';
  if (newOrUsed == 'U')
  {
    atdServletAction='PE';
    dynLink = '/aaui';
  }
  var strURL = "https://" + finURL + dynLink + "?ATD_SERVLET_ACTION=" +atdServletAction + "&ATD_SERVLET_PAGE_CLASS=AAExternal&AirCond=Y&CountryCode=" + getFMCCCountryCode(dealer) + "&LanguageCode=" + userLanguage + "&InterfaceHomeURL=http://" + dealer.subdomain + "." + dealersitesSubdomainBase + "&SourceOfData=" + dealer.sourceOfData;
  strURL = createURL(strURL, newOrUsed, vehicleDetails, modelInfo, zipCode, stateCode);
  callPopup(strURL, 800, 600);
}
/**
 * Closes the prompt
 */
function closePrompt()
{
  document.getElementById("popUp").style.visibility = "hidden";
  document.getElementById("maskedLayer").style.display = "none";
  disableAllSelectsForIE(false);
}
/**
 * Set the prompt
 */
function setPrompt(message,error)
{
  var textPrompt = document.getElementById('popUpText');
  textPrompt.innerHTML = "";
  if(isPEorCA == "PE")
  {
    textPrompt.innerHTML = arrStrings['apps.inventory.monthlypayment.prompt'];
  }
  else if(isPEorCA == "CA")
  {
    textPrompt.innerHTML = arrStrings['apps.inventory.applycredit.prompt'];
  }
  textPrompt.innerHTML += ",</br>";
  textPrompt.innerHTML += " " + arrStrings['apps.inventory.monthlypaymentcommon.prompt'];
  if(!error)
  {
    textPrompt.innerHTML += " " + message;
  }
  else{
    textPrompt.innerHTML += " " + "<font color='red'>"+message+"</font>";
  }
  if(document.getElementById('popUp').style.visibility == 'visible')
  {
    document.getElementById('promptText').focus();
  }
}
/**
 * Open the prompt
 */
function openPrompt(popUpPositionID)
{
  var obj = document.getElementById('maskedLayer');
  var dims = getScreenCordinates();
  var divPrompt = document.getElementById('popUp');
  obj.style.display = "block";
  obj.style.height = dims[1];
  obj.style.width = dims[0];
  obj.style.background = "#BBBBBB";
  disableAllSelectsForIE(true);
  divPrompt.style.visibility = "visible";
  document.getElementById('promptText').focus();
}
/**
 * Collect the text
 */
function collectText()
{
  var zipCode, stateCode;
  if(dealer.country == 'us')
  {
    zipCode = document.getElementById('promptText').value;
    isValidZipCode = validateUSZipCode(zipCode);
    if (!isValidZipCode)
    {
      setPrompt(arrStrings['apps.inventory.validzipCode.prompt'],true);
      return;
    }
    setCookie("zipCodeCookie", zipCode, ZIPCODECOOKIEEXPIREDAYS);
  }
  if(dealer.country == 'ca')
  {
    stateCode = document.getElementById('promptText').value;
    isValidState = validateCAState(stateCode);
    if (!isValidState)
    {
      setPrompt(arrStrings['apps.inventory.validstate.prompt'],true);
      return;
    }
    stateCode = stateCode.toUpperCase();
    setCookie("provinceCodeCookie", stateCode, ZIPCODECOOKIEEXPIREDAYS);
  }
  // if both are null, then cancel was pressed in popup
  if (zipCode == null && stateCode == null)
  {
    return;
  }
  closePrompt();
  if(isPEorCA == "PE")
  {
    openPaymentEstimatorURL(finURL, newOrUsed, vehicleDetails, modelInfo, zipCode, stateCode);
  }
  else if(isPEorCA == "CA")
  {
    openCreditApplicationURL(finURL, newOrUsed, vehicleDetails, modelInfo, zipCode, stateCode);
  }
}
/**
 * Handle the prompt when the key is pressed
 */
function handlePromptOnkeyPress(e)
{
  divPrompt = document.getElementById("popUp");
  if(divPrompt!= null){
    var evtobj = window.event? event : e;
    var unicode = evtobj.charCode? evtobj.charCode : evtobj.keyCode;
    if(unicode == 27 && divPrompt.style.visibility == "visible")
    {
      closePrompt();
    }
    else if(unicode == 13 && divPrompt.style.visibility == "visible")
    {
      collectText();
    }
  }
}
document.onkeypress=handlePromptOnkeyPress;
// RFP FDDS - 12202 - Improve UI for credit link ZIP popup ends

/**
 * Prompts the Applicant for his ZipCode.
 * @return the user's zipcode if entered, else, returns null
 */
function getUserZipCode()
{
  var zipCode = null;
  if (dealer.country == 'us')
  {
    var isValidZipCode = false;
    while (!isValidZipCode)
    {
      zipCode = prompt (arrStrings['apps.inventory.zipCode.prompt'],'');
      // return if the user preses cancel
      if (zipCode == null)
      {
        return null;
      }
      isValidZipCode = validateUSZipCode(zipCode);
      if (!isValidZipCode)
      {
        alert(arrStrings['apps.inventory.zipCode.invalid']);
      }
    }
  }
    return zipCode;
}
/**
 * Checks for a valid US Zip code
 * @param zipCode the zipCode to validate
 * @return true if its a valid US Zip Code
 */
function validateUSZipCode(zipCode)
{
  var usZipCode = /^\d{5}$/;
  return usZipCode.test(zipCode);
}
/**
 * Prompts the Applicant for his State.
 * @return the user's state if entered, else, returns null
 */
function getUserState()
{

  var state = null;
  if (dealer.country == 'ca')
  {
    var isValidState = false;
    while (!isValidState)
    {
      state = prompt (arrStrings['apps.inventory.state.prompt'],'');
      // return if the user preses cancel
      if (state == null)
      {
        return null;
      }
      isValidState = validateCAState(state);
      if (!isValidState)
      {
        alert(arrStrings['apps.inventory.state.invalid']);
      }
    }
  }
  return (null == state)?null : state.toUpperCase();
}
/**
 * Checks for a valid Canada State code
 * @param state the state to validate
 * @return true if its a valid Canada State Code
 */
function validateCAState(state)
{
  var canStateCode = /^[a-zA-Z]{2}$/;
  return canStateCode.test(state);
}

/**
 * Get the country code to pass to FMCC.
 * @param objDealer the dealer
 * @return countryCode the country code
 */
function getFMCCCountryCode(objDealer)
{
  var countryCode = "USA";
  if (objDealer.country == "ca")
  {
    countryCode = "CAN";
  }
  return countryCode;
}
/**
 * Attaches vehicle & model parameters to the finance url.
 * @param finURL the external base service url we use.
 * @param newOrUsed 'N' if a new vehicle, 'U' otherwise.
 * @param vehicleDetails the vehicle details obtained from locate service.
 * @param modelInfo the model information.
 * @param zipCode the applicant's postal code
 */
function createURL(finURL, newOrUsed, vehicleDetails, modelInfo, zipCode, stateCode)
{
  // attach common parameters
  strURL = attachCommonParameters(finURL, newOrUsed);
  // attach regional parameters
  if (dealer.country == 'us')
  {
    strURL += attachUSRegionalParameters(zipCode);
  }
  else if (dealer.country == 'ca')
  {
    strURL += attachCARegionalParameters(stateCode);
  }
  // attach model/vehicle parameters
  if (null == vehicleDetails)
  {
    //attach VIN parameter with blank value if vehicleDetails is null
    strURL += '&VIN=';
    if (null != modelInfo)
    {
     return attachModelParameters(strURL, newOrUsed, modelInfo);
    }
    //Since we dont have both vehicle & model details we send back the base url with just dealer data.
    return strURL;
  }
  else
  {
    return attachModelVehicleParameters(strURL, newOrUsed, modelInfo, vehicleDetails);
  }
}

/*
 * Removes characters from the number string which are not digits. Is used to remove extra formats in a number string.
 * @param number formatted number string. ex : '312(1121)+22'
 * @return the digit sequence after removing the formatting. ex: 312112122
 */
function extractNumber(number)
{
  return number.replace(/[^0-9]/g, '');
}
/*
 * Removes all extra formattings from a zip code. Zip code can have alphabets in them. ex : canada zipcodes.
 * @param zip formatted zip string. ex : 'aaa 1223'
 * @return the digit sequence after removing the formatting. ex: aaa1223
 */
function extractZip(zip)
{
  return zip.replace(/[^0-9a-zA-Z]/g, '');
}

/*
 * To extract the bodystyle code from vin. Currently characters 5-7 of the VIN code
 * form the bodystyle code.
 * @param vin the complete vin of the vehicle.
 *
 * @return the bodystyle code of the vehicle.
 */
function extractBodyStyleCodeFromVIN(vin)
{
  if(null != vin && 7 <= vin.length)
  {
    return vin.substr(4,3);
  }
  return '';
}
/*
 * Appends to the base url, the common parameters for a finance request.
 * @param strURL the URL
 * @param newOrUsed the new or used
 * @return strURL the URL
 */
function attachCommonParameters(strURL, newOrUsed)
{
  strURL += '&DealerPACode=' + dealer.paCode;
  strURL += '&DealerName=' + escape(dealer.name);
  strURL += '&DealerFullFax=' + extractNumber(dealer.fax);
  strURL += '&DealerFullPhone=';
  strURL += '&DealerEmailAddr=';
  strURL += '&ePrice=N';
  strURL += '&PEPDiscount=0';
  strURL += '&NewUsedIndicator=' + newOrUsed;
  return strURL;
}
/*
 * Appends to the base url, the regional US specific parameters for a finance request.
 * @param zipCode the zip code
 * @return html attribute and velue
 */
function attachUSRegionalParameters(zipCode)
{
  return '&paFullZipCode=' + zipCode;
}
/*
 * Appends to the base url, the regional Canada specific parameters for a finance request.
 * @param stateCode the state code
 * @return html attribute and velue
 */
function attachCARegionalParameters(stateCode)
{
  return '&paCurResState=' + stateCode;
}
/*
 * Appends to the base url various model parameters involved in a finance request.
 * @param strURL the external base service url we use.
 * @param newOrUsed 'N' if a new vehicle, 'U' otherwise.
 * @param modelInfo the model information.
 */
function attachModelParameters(strURL, newOrUsed, modelInfo)
{
  strURL += '&ModelName=' + modelInfo.name;
  strURL += '&ModelKey=' + modelInfo.year + '|' + modelInfo.make + '|999';
  strURL += '&ModelPrice=' + modelInfo.basePrice;
  strURL += '&SellingPrice=' + modelInfo.basePrice;
  return strURL;
}
/*
 * Appends some vehicle details params onto the url.
 *
 * @param strURL the external base service url we use.
 * @param vehicleDetails the vehicle details obtained from locate service.
 *
 * @return the modified url.
 */
function attachVehicleParameters(strURL, vehicleDetails)
{
  strURL += '&ModelName=' + vehicleDetails.modelName;
  strURL += '&ModelPrice=' + vehicleDetails.price;
  strURL += '&SellingPrice=' + vehicleDetails.price;
  strURL += '&EngineCode=' + vehicleDetails.engineCode;
  strURL += '&AutoTrans=' + vehicleDetails.autoTrans;
  strURL += '&TrimCode=' + vehicleDetails.trimCode;
  strURL += '&TrimDesc=' + escape(vehicleDetails.bodyDesc);
  return strURL;
}
/*
 * Appends to the base url various model & vehicle parameters involved in a finance request.
 * @param strURL the external base service url we use.
 * @param newOrUsed 'N' if a new vehicle, 'U' otherwise.
 * @param modelInfo the model information.
 * @param vehicleDetails the vehicle details obtained from locate service.
 *
 * @return the modified url.
 */
function attachModelVehicleParameters(strURL, newOrUsed, modelInfo, vehicleDetails)
{
  var bodyStyleCode = extractBodyStyleCodeFromVIN(vehicleDetails.vin);
  //For old vehicles we use the locate service data.
  if ('U' == newOrUsed)
  {
    strURL += '&Mileage=' + vehicleDetails.mileage;
    strURL += '&ModelKey=' + vehicleDetails.year + '|' + vehicleDetails.make + '|' + bodyStyleCode;
  }
  else
  {
    strURL += '&ModelKey=' + modelInfo.year + '|' + modelInfo.make + '|' + bodyStyleCode;
    strURL += '&Mileage=0';
  }
  //adding vehicle VIN to the URL
  strURL += '&VIN=' + vehicleDetails.vin;
  return attachVehicleParameters(strURL, vehicleDetails);
}
/**
 * Trims a string, returning the trimmed value
 * @param str the string to be trimmed
 * @return str the string that is trimmed
 */
function Trim(str)
{
  while(str.charAt(0) == (" ") )
  {
    str = str.substring(1);
  }
  while(str.charAt(str.length-1) == " " )
  {
    str = str.substring(0,str.length-1);
  }
  return str;
}
/**
 * A better implementation of trim
 */
String.prototype.trim = function()
{
  return this.replace(/^\s+|\s+$/, '');
};
/**
 * Selects a value from 'selectName' selection box in 'formElement'. The value to set is passed in setValue.
 * optionProperty is typically 'text' or 'value' and indicates whether the text or the value should
 * be matched
 */
function selectValue(formElement,selectName,setValue,optionProperty,objComparator)
{
  var options = document.forms[formElement][selectName].options;
  for(var i = 0; i < options.length; i++)
  {
    var optionValue = options[i][optionProperty];
    if((objComparator && objComparator(optionValue,setValue)) || optionValue == setValue)
    {
      document.forms[formElement][selectName].selectedIndex = i;
      return true;
    }
  }
  return false;
}
/**
 * Function to fire an explicit omniture call.
 * Requires: s_code.js to be included
 */
function fireOmnitureEvent()
{
  if (s != null)
  {
    // t is defined in the Omniture API, if any event is to be appended
    // to the event already defined on the page then it should be passed as an argument
    // to it. This is not required in the current specs.
    s.t("");
  }
}
/*
 * Takes in a string where values have been templatized using '__'. It picks up the variable names from
 * ns (JSON object) and substitutes them, returning the processed string.
 * eg processTemplate('<a href="__LINK"/>',{"LINK":"test.com"}) will return <a href="test.com"/>
 * @param tmpl the template to be processed
 * @param ns the ns
 * @param string the processed template
 */
function processTemplate(tmpl, ns)
{
  var fn = function(w, g)
  {
    var cnt = ns[g];
    return cnt || "";
  };
  return tmpl.replace(/__([A-Za-z0-9_]*)/g, fn);
}

/**
 * get the cookie.
 * @param {Object} cookie name.
 */
function getCookie(cookieName)
{
  if (document.cookie.length > 0)
  {
    var cookieStart = document.cookie.indexOf(cookieName + "=");
    if (cookieStart != -1)
    {
      cookieStart = cookieStart + cookieName.length + 1;
      var cookieEnd = document.cookie.indexOf(";", cookieStart);
      if (cookieEnd == -1)
      {
        cookieEnd = document.cookie.length;
      }
      return unescape(document.cookie.substring(cookieStart, cookieEnd));
    }
  }
  return "";
}
/**
 * Disables or enables all the select boxes in the newvehicles-searchinventory page.
 * @param the options
 */
function disableAllSelectsForIE(option)
{
  var browser = navigator.appName;
  var b_version = navigator.appVersion;
  var version = parseFloat(b_version);
  if( browser == "Microsoft Internet Explorer" && version < 7)
  {
    var selectBoxArray = document.getElementsByTagName('select');
    for(i=0; i < selectBoxArray.length; i++)
    {
      selectBoxArray[i].style.background = (option ? "#DDDDDD" : "#FFFFFF");
      selectBoxArray[i].disabled = option;
    }
  }
}
/**
 * Get the screen cordinates
 */
function getScreenCordinates()
{
  var yWithScroll, xWithScroll;
  if (window.innerHeight && window.scrollMaxY) {// Firefox
    yWithScroll = window.innerHeight + window.scrollMaxY;
    xWithScroll = window.innerWidth + window.scrollMaxX;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    yWithScroll = document.body.scrollHeight;
    xWithScroll = document.body.scrollWidth;
  } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
    yWithScroll = document.body.offsetHeight;
    xWithScroll = document.body.offsetWidth;
  }
  var cordinates = [xWithScroll,yWithScroll];
  return cordinates;
}
/**
 * Find the position of x and y for a given html element
 * @param oElement the html element to which the positions to be found out
 * @return [x,y] the positions of x,y for a given html element
 */
function findPosition( oElement )
{
  if( typeof( oElement.offsetParent ) != 'undefined' )
  {
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent )
    {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
    return [ posX, posY ];
  } else
  {
    return [ oElement.x, oElement.y ];
  }
}
/**
 * Shift the 'div' relative to 'rootDiv'.
 * @param divId the division id
 * @param rootDivId the root division id
 */
function shiftRelativeX(divId, rootDivId)
{
  //Re position the left panel relative to rootDiv.
  var rootDiv = document.getElementById(rootDivId);
  var rootCoordinates = findPosition(rootDiv);
  var div = document.getElementById(divId);
  var divCoordinates = findPosition(div);
  div.style.left = (rootCoordinates[0] + divCoordinates[0]) + "px";
}
/**
* Returns the size of the browser window
*/
function getWindowSize()
{
   var w = 0;
   var h = 0;
    //IE
    if(!window.innerWidth)
    {
        //strict mode
        if(!(document.documentElement.clientWidth == 0))
        {
            w = document.documentElement.clientWidth;
            h = document.documentElement.clientHeight;
        }
        //quirks mode
        else
        {
            w = document.body.clientWidth;
            h = document.body.clientHeight;
        }
    }
    //w3c
    else
    {
        w = window.innerWidth;
        h = window.innerHeight;
    }
    return [w,h];
}
/**
* Move the element to the center of the browser's visible area
* @param elem the element
* @param width the width
* @param height the height
*/
function centerElement(elem, width, height)
{
  var coordinates = getCenterXY(width, height);
  var Top = coordinates[0];
  var Left = coordinates[1];
  elem.style.top = Top + "px";
  elem.style.left = Left + "px";
}
/**
 * Get the browser name
 */
function getBrowserName()
{
  return navigator.appName;
}
/**
* Get the x,y coordinated to move an element to the center of the browser area
* @param width the width
* @param height the height
*/
function getCenterXY(width, height)
{
  var scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
  var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
  var screenSize = getWindowSize();
  var screenWidth = screenSize[0];
  var screenHeight = screenSize[1];
  var coordinates = [];
  coordinates[0]= scrollTop + (screenHeight - height)/2;
  coordinates[1]= scrollLeft + (screenWidth - width )/2;
  return coordinates;
}
/**
 * Function to insert line-breaks in a word longer than a specified length
 * @param word the word
 * @param maxWordLength the maximum word length
 * @param specialCharCodes the special character codes
 */
function getResizedWords(word, maxWordLength, specialCharCodes)
{
  if(word.length <= maxWordLength)
  {
    //return the same word if it is not longer than the maxlength
    return word;
  }
  var cleanWord = removeSpecialChars(word, specialCharCodes);
  if(cleanWord.length <= maxWordLength)
  {
    //return the same word if it's display length is not longer than maxlength
    return word;
  }
  //Get the array containing order if occurence of special characters
  var specialIndexArray = getIndices(word, specialCharCodes);
  word = cleanWord;
  var startIndex;
  var endIndex;
  var wordWithBreak = "";
  var numWords = parseInt(word.length/maxWordLength) + 1;
  for(j=0;j<numWords;j++)
  {
    //If this the last word fragment, the endIndex should be the index of last character
    startIndex = j*maxWordLength;
    endIndex = Math.min(word.length, startIndex + maxWordLength) - 1;
    wordWithBreak += word.substring(startIndex , endIndex) + "<br/>";
  }
  //remove <br/> from the end of the string
  wordWithBreak = wordWithBreak.substring(0, wordWithBreak.lastIndexOf("<br/>")-1);
  return restoreSpecialChars(wordWithBreak , specialCharCodes, specialIndexArray) ;
}
/**
 * Function to replace the special characters by a single space each
 * @param word the word
 * @param specialCharCodes the special character codes
 */
function removeSpecialChars(word, specialCharCodes)
{
  var wordWithoutSpecial =  word;
  //Replace special characters by a space
  for(var i=0; i<specialCharCodes.length; i++)
  {
    var position = 0;
    while(position>=0)
    {
      position = wordWithoutSpecial.indexOf(specialCharCodes[i]);
      if(position == 0)
      {
        wordWithoutSpecial = " " + wordWithoutSpecial.substring(position + specialCharCodes[i].length);
      }
      if(position > 0)
      {
        wordWithoutSpecial = wordWithoutSpecial.substring(0, position) + " " + wordWithoutSpecial.substring(position + specialCharCodes[i].length);
      }
    }
  }
  return wordWithoutSpecial;
}
/**
 * Function to record the order of occurence of special characters.
 * The returned Array contains the order of occurence of each of specialCharacters.
 * A sequence of 0 0 1 1 2 2 in the array would be that first spcecial char was found
 * twice then two occurences of the second special char and so on.
 * @param word the word
 * @param specialCharCodes the special character codes
 */
function getIndices(word, specialCharCodes)
{
  var associativePositionArray = [];
  var indexArray =  [];
  for(var codeIndex=0; codeIndex<specialCharCodes.length; codeIndex++)
  {
    var position = 0;
    while(position >= 0 && position < word.length)
    {
      position =  word.indexOf(specialCharCodes[codeIndex], position);
      if(position >=0)
      {
        indexArray.push(position);
        //associative array that holds position -> specialCharacter number mapping
        associativePositionArray[position + "code"] = codeIndex;
        position += specialCharCodes[codeIndex].length;
      }
    }
  }
  indexArray.sort(function(a,b){return a - b;});
  for(var arrIndex=0;arrIndex<indexArray.length;arrIndex++)
  {
    indexArray[arrIndex] = associativePositionArray[indexArray[arrIndex]+ "code"];
  }
  var arr = indexArray.join(" ");
  return indexArray;
}
/**
 * Function to restore the special characters by replacing spaces in a specific order
 * @param word the word
 * @param specialCharCodes the special character codes
 * @param orderArray the order array
 */
function restoreSpecialChars(word, specialCharCodes, orderArray)
{
  var position = 0;
  var orderIndex = 0;
  while(position >=0)
  {
    position = word.indexOf(" ");
    //Replace spaces in order. OrderArray contains the order in which special characters were there in the original array.
    //It refers to the special characters by its index in the specialCharCodes Array
    if(position == 0)
    {
      word = specialCharCodes[orderArray[orderIndex]] + word.substring(position+1);
      orderIndex++;
    }
    else if(position > 0)
    {
      word = word.substring(0,position) + specialCharCodes[orderArray[orderIndex]] + word.substring(position+1);
      orderIndex++;
    }
  }
  return word;
}
/**
 * Function to modify a text such that line-breaks are inserted in words
 * longer than a specified length, forcing a wrap on the text. Only the long
 * words are broken.
 * @param longText the long text
 * @param maxWordLength the maximum word length
 * @param specialCharCodes the special character codes
 */
function wrapLongText(longText, maxWordLength, specialCharCodes)
{
  if(longText == null)
  {
    return;
  }
  var resizedWords = [];

  //Ensure that a line break is not split while wrapping
  var words = longText.split("<br>");
  if(words.length > 0)
  {
    longText = words.join(" " + "<br>" + " ");
  }
  words = longText.split("<br/>");
  if(words.length > 0)
  {
    longText = words.join(" " + "<br/>" + " ");
  }
  //Get individual words from the disclaimer string
  words = longText.split(" ");
  for(i=0;i<words.length; i++)
  {
    //get the resized words.. short words are unaffected
    resizedWords.push(getResizedWords(words[i], maxWordLength, specialCharCodes));
  }
  return resizedWords.join(" ");
}

/**
 * functions to open a new popup without menubar and address bar.
 * @param myLocation the my location
 * @param nWidth the browser width
 * @param nHeight the browser height
 */
function popUp(myLocation, nWidth, nHeight)
{
  var sOptions = "menubar=0,toolbar=0,location=0,directories=0,status=1,scrollbars=1,resizable=1,width=" + nWidth + ",height=" + nHeight;
  window.open(myLocation, 'popupWndow', sOptions);
}