function settingResettingCookie(cookieName,cookieValue,currentCookie){
/*-------------------------------------
Description:
sets the cookie if it hasnt been set but also will only set if its not the same.

Author: JL
Dependencies:
	jquery.cookie lib
Updates:
@Parameters: cookieName = name of the cookie
@Parameters: cookieValue = new cookie value to set if not present
@Parameters: currentCookie = current cookie value
-------------------------------------*/
	//chk if cookie present
	if($j.cookie(cookieName) == null && cookieValue != null){
		$j.cookie(cookieName,cookieValue);
	}
	//chk if cookie the same, if not then replace with this cookieValue
	else if ($j.cookie(cookieName) != currentCookie && cookieValue != null){
		$j.cookie(cookieName,cookieValue);
	}
	
}

function GetParameterFromURL(name){
/*-------------------------------------
Description:
Extracts a parameter from the url. eg xyz.com/test.php?msg=hello
GetParameterFromURL('msg') will get 'hello'

Author: http://www.netlobo.com/url_query_string_javascript.html
Dependencies:
Updates:
@Parameters: name = name of the parameter to extract
-------------------------------------*/
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function addTrackingToDeepLink(className,trackingVarName,trackingCode,noTrackValue,dcsDelimiter,dcsTracker){
	/*-------------------------------------
	Description: 
	Adds the tracking code to all links of "className"

	Author: JL
	Updates: 
	Dependencies: 
	   jq.cookie
	-------------------------------------*/	
		//check if any GET params exist
		var debug = 0;
		var paramChar; //either '?' or '&'
		if (debug){alert($j(className).attr('href'))};
		
		if (trackingCode === null){
			trackingCode = noTrackValue;
		}
		if (dcsTracker === null){
			dcsTracker = 'noDcsTrk'
		}

		$j(className).each(function(){
			if (debug){alert($j(this).attr('href'))};
			
			//chk if there's existing params
			if (String($j(this).attr('href')).indexOf('?') > 1){
				paramChar = '&';
			}
			//else there's no parameters 
			else{
				paramChar = '?';
			}
		
			if(debug){ alert($j(this).attr("href")+paramChar+trackingVarName+"="+trackingCode) };
		
			//change all the URLs 
			$j(this).attr("href",$j(this).attr("href")+paramChar+trackingVarName+"="+trackingCode+dcsDelimiter+dcsTracker)
		});

	}

function routePlanForMap(from,to){
/*-------------------------------------
Description:
preps the parameters ready for the map page

Author: JL Oct 09
Dependencies:
jquery.cookie
Updates:
@Parameters: string from 
@Parameters: string to
-------------------------------------*/
	//convert to url safe string
	from = Url.encode(from);
	to = Url.encode(to);
	
	var redirectString = 'http://' + document.domain + '/map.php?depart=' + from + '&to=' + to;
	
	window.location = redirectString;
}

function showOnMapLocation(locationSearch){
	/**
	 * Description:
		 Forwards the request to the map.php with the location
	 * Author: JL
	 * Changelog: 
	 * 	Rev1
	 * @param String locationSearch
	 * @return none
	 */

		var forwardLocation = 'http://'+document.domain+'/map.php?locSearch='+locationSearch;
		window.location = forwardLocation;			 
	}

function logLocSearch(locTerm,searchNumber,redirectToMapPage){
/**
 * Description:
    logs the location search
 * Author: JL
 * Changelog:
 * 	Rev1
 * @param String locTerm - the search location
 * @param int searchNumber - the n th search of the user
 * @param bool redirectToMapPage - indicate if we need to redirect to main map page
 * @return none
 */
    if(typeof(searchNumber)== 'undefined'){
        searchNumber = 'x';
    }
    if(typeof(redirectToMapPage)== 'undefined'){
        redirectToMapPage = false;
    }
    var ajxUrl = 'scripts/ajax.php?a=locSearchLog&loc=' + locTerm + '&ref=' + getThisPage(location.href) + '&searchNum=' + searchNumber;

    //decide if we need to redirect from homepage to map or just do the log as part of log
    if(redirectToMapPage){
        $j.ajax({
            url : ajxUrl,
            asynch : false,
            dataType : "json",
            success: function(locTerm) {
                    showOnMapLocation(locTerm.loc);
            },
            fail : function(locTerm){
                    showOnMapLocation(locTerm.loc);
            }
        })
    }
    else{
        $j.ajax({
            url : ajxUrl,
            asynch : false
        })
    }
    return false;
}

function logJourneySearch(fromLoc,toLoc,searchNumber,redirectToMapPage){
/**
 * Description:
    logs the location search
 * Author: JL
 * Changelog:
 * 	Rev1
 * @param String fromLoc - the starting point of journey
 * @param String toLoc - the ending point of journey
 * @param int searchNumber - the n th search of the user
 * @param bool redirectToMapPage - indicate if we need to redirect to main map page
 * @return none
 */
    if(typeof(searchNumber)== 'undefined'){
        searchNumber = 'x';
    }
    if(typeof(redirectToMapPage)== 'undefined'){
        redirectToMapPage = false;
    }
    
    var ajxUrl = 'scripts/ajax.php?a=jorneySearchLog&from=' + fromLoc + '&to=' + toLoc +'&ref=' + '---'+ '&searchNum=' + searchNumber;

    //decide if we need to redirect from homepage to map or just do the log as part of log
    if(redirectToMapPage){
        $j.ajax({
            url : ajxUrl,
            asynch : false,
            dataType : "json",
            success : function(data){
                    routePlanForMap(data.from,data.to);
            },
            fail : function(data){
                    routePlanForMap(data.from,data.to);
            }
        })
    }
    else{
        $j.ajax({
            url : ajxUrl,
            asynch : false
        })
    }
    return false;
}

function getThisPage(page){
    //returns whether homepage, map page or other
    if(page.search('map.php')){
        return 'map';
    }
    else{
        return 'homepage';
    }
    
}
/********************************************************************/
$j(document).ready(function() {
	var trackingCodeParam= 'A8ID';
	var DcStormCookie = '_#clkid';
	
    //chk if need to set cookie a8id, see if in url params.
    if(GetParameterFromURL(trackingCodeParam)){
            $j.cookie(trackingCodeParam,GetParameterFromURL(trackingCodeParam))
    }

    //clear fields when user clicks in the text field, but only if its the first time clcking into it.
    $j('input[type="text"]').bind('click',function(e){
           if($j(this).val() == 'Enter Destination' || $j(this).val() == 'Enter Location' || $j(this).val() == 'Departure Location' || $j(this).val() == 'Destination')
                $j(this).val('');
    });

    //bind the ajax logging code
    $j('#topSrchBtn').click(function(){
        logLocSearch($j('#topSearchLocInput').val(),0,true);
    })
    $j('#lowLocBtn').click(function(){
        logLocSearch($j("#searchLoc[type=text]").val(),0,true);
        return false;
    })

    $j('#journeyPlanner').click(function(){
        logJourneySearch($j("#departFrom").val(),$j("#routeTo").val(),0,true);
        //routePlanForMap($j("#departFrom").val(),$j("#routeTo").val());
        return false;
    })

})