/*******************************************************************************
* Name         = findlocations.js
*
* Description  = Javascript functions used in the find Locations pages.
*
* Revision History:
*
*  Date        Developer           Description
* ------------------------------------------------------------------------------
*  03/27/06    Maria Kaufmann      Initial implementation.
*  04/01/06    Tim Dennis (Seisan) Coded full implementation.
*  06/29/06    Tim Dennis (Seisan) Added function validatFindClosestMap(frm).
*  07/05/06    Tim Dennis (Seisan) Added function validatDrivingDirections(frm).
*  07/06/06    Maria Kaufmann      Changed validation message displayed by
*                                  validatDrivingDirections(frm).
*  07/08/06    Tim Dennis (Seisan) Modified validateFindClosestMap(frm) function
*                                  to validate search radius is provided.
*  07/11/06    Tim Dennis (Seisan) Added functions BrowserCheck() and
*                                  getImageXfromLeft(e) to support display of
*                                  pop-up text on state/province map page
*                                  (rydermap.jsp) when user does a mouse-over a
*                                  location icon.
*
*******************************************************************************/

/*******************************************************************************
* Get browser Type.
*******************************************************************************/

function BrowserCheck () {
	var b = navigator.appName;
	if (b=="Netscape") this.b = "ns";
	else if (b=="Microsoft Internet Explorer") this.b = "ie";
	else this.b = b
	this.v = parseInt(navigator.appVersion);
	this.ns = (this.b=="ns" && this.v >= 4);
	this.ns4 = (this.b=="ns" && this.v == 4);
	this.ns5 = (this.b=="ns" && this.v == 5);
	this.ie = (this.b=="ie" && this.v >= 4);
	this.ie4 = (navigator.userAgent.indexOf('MSIE 4')>0);
	this.ie5 = (navigator.userAgent.indexOf('MSIE 5')>0);
	this.ie6 = (navigator.userAgent.indexOf('MSIE 6')>0);
	if (this.ie5) this.v=5;
	if (this.ie6) this.v=6;
	this.min = (this.ns || this.ie);
}

var browserCheck = new BrowserCheck();


/*******************************************************************************
* Get x/y coordinates based on events.
*******************************************************************************/
function getImageXfromLeft(e) {
  if (!browserCheck.ie) return e.layerX;
  else return event.offsetX;
}

function getImageYfromTop(e) {
  if (!browserCheck.ie) return e.layerY;
  else return event.offsetY;
}

function getClientXfromLeft(e) {
	var scrollx = (document.all)?document.body.scrollLeft:window.pageXOffset;
	if (!browserCheck.ie) return e.clientX + scrollx;
	else return event.clientX + scrollx;
}

function getClientYfromTop(e) {
	 var scrolly = (document.all)?document.body.scrollTop:window.pageYOffset;
	if (!browserCheck.ie) return e.clientY + scrolly;
	else return event.clientY + scrolly;
}



/*******************************************************************************
* Validates Driving Direction form.
*******************************************************************************/
function validatDrivingDirections(frm){
	// Validate required fields were provided.
	if( isEmpty(frm.city) && isEmpty(frm.postalCode)){
		alert("Origin City, State/Province, or Postal Code must be provided.");
		return false;
	}
	if( isEmpty(frm.destCity) && isEmpty(frm.destPostalCode)){
		alert("Destination City, State/Province, or Postal Code must be provided.");
		return false;
	}

    if ( !validateField(frm.country, "Origin Country") || ! validateField(frm.destCountry, "Destination Country") )
    {
        return false;
    }

    // Success
    return true;

}

/*******************************************************************************
* Validates Find a Street Map form.
*******************************************************************************/
function validatFindStreetMap(frm)
{
	// Validate required fields were provided.
    if ( ! validateField(frm.country, "Country") )
    {
        return false;
    }

    // Success
    return true;
}

/*******************************************************************************
* Validates Find a Closest Location Map form.
*******************************************************************************/
function validateFindClosestMap(frm)
{
	// Validate required fields were provided.
    if ( ! validateField(frm.country, "Country") )
    {
        return false;
    }

	// Validate required fields were provided.
    if ( ! validateField(frm.searchRadius, "Search Radius") )
    {
        return false;
    }

    // Success
    return true;
}

/*******************************************************************************
* Submits the form for the command specified.
* Note: this method performs form validation.
*******************************************************************************/
function submitWithCommand(frm, command)
{
    frm.command.value = command;
}

/*******************************************************************************
* Submits the form for the command specified.  This method does an explicit
* form.submit(), which is necessary in order to submit the form when the
* onClick event is triggered by an element other than a button.
*
*******************************************************************************/
function submitWithCommandForceSubmit(frm, command)
{
    submitWithCommand(frm, command);
    frm.submit();
}
