var showLog = true;
var browser = GetBrowser();
var jsAlert = alert;

/**
 * JQuery AJAX to submit parameters from form to grails action.
 * <p>
 * Created: 15/10/2008
 * 
 * <p>
 * Currently not implemented using formRemote - using JQuery plugin.
 * Use this until plugin has been released. 16/10/2008
 *
 * @param form id
 * @param update div id
 * @param display div id
 * 
 * @author: Manoj Gopal
 */
function formRemote(id_form, id_update, id_show)
{
    var attrs = $('#' + id_form).serialize()
    
    $.ajax({
        type:'POST',
        url:'/locator/locate/search',
        data: attrs + "",
        success:function(data,textStatus){
            $('#' + id_update).html(data);
        },
        error:function(XMLHttpRequest,textStatus,errorThrown){

        },
        complete:function(XMLHttpRequest,textStatus){
            showContent(id_show);
        }
    });
    
    return false;
}

/**
 * Updates DOM object with result of action call
 * <p>
 * Created: 23/10/2008
 * 
 * @param controller
 * @param action
 * @param attributes
 * @param update DOM Div id
 *
 * @author: Manoj Gopal
 */
function ActionRemote(controller, action, attrs, objId, onCompleteCallback)
{
    var result = "";

    $.ajax({
        type:'POST',
        url:'/locator/' + controller + '/' + action,
        data: attrs + "",
        success:function(data,textStatus){
            if ( !isEmpty(objId) ) {

                if ( typeof objId == "object" ) {
                    $(objId).html(data);
                }
                else {
                    $('#' + objId).html(data);
                }
            }
        },
        error:function(XMLHttpRequest,textStatus,errorThrown){

        },
        complete: onCompleteCallback
    });
    
    return result;
}

/**
 * Load all the JS into page. Load each subsequent script after the previous script has loaded
 * <p>
 * Created: 16/10/2008
 *
 * @param domain Domain of the system
 * @param cust_key KEY of customer
 * 
 * @author: Manoj Gopal
 */
function LoadScripts(domain, cust_key, location_key)
{
    $.getScript(domain + "/locator/js/JL/client.js", function() { 
        $.getScript(domain + "/locator/js/JL/statistics.js", function () {
            if (location_key == null)
                LoadContent(domain, cust_key);
            else
                LoadSingleLocation(domain, cust_key, location_key)
        }); 
    });
    // 12/11/2010 YY Tsoi added s_code.js for GDT GAC site
    $.getScript(domain + "/locator/js/JL/s_code.js");
}

/**
 * Load the main locator grails app into div - "jl_app"
 * Load each subsequent script after the previous script has loaded
 * <p>
 * Created: 16/10/2008
 *
 * @param domain Domain of the system
 * @param cust_key KEY of customer
 *
 * @author: Manoj Gopal
 */
function LoadContent(domain, cust_key)
{
    $.getScript(domain + '/locator/js/data/' + cust_key + '.js', function() {

        $('#jl_app').html(jsLoad.replace(/##SQ##/g, "\'"));

        $.getScript(domain + '/locator/js/JL/locate.js', function() {
            locator.init();
        }); 
    });
}

/**
 * Load a map with a single location into div - "jl_app"
 * <p>
 * Created: 21/11/2008
 *
 * @param domain Domain of the system
 * @param cust_key KEY of customer
 * @param location_key KEY of location within customer database
 *
 * @author: Robert Homewood
 */
function LoadSingleLocation(domain, cust_key, location_key)
{
    $('#jl_app').load(domain + "/locator/locate/loadLocation", {key:cust_key, location_key:location_key}, function() {
        $.getScript(domain + '/locator/js/JL/locate.js', function() { 
            locator.showLocation();
        });
    });
}

/**
 * Log output to console / status window.
 * Only displays in fiebug console.log if the browser if FF compliant
 * <p>
 * Created: 23/10/2008
 * 
 * @param output String to output
 *
 * @author: Manoj Gopal
 */
function log(output)
{
    if ( showLog && browser == "FF" ) {
        console.log(output);
    }
}

/**
 * This function extracts the param value for name => "name" and return it
 * <p>
 * Created: 24/10/2008
 * 
 * @param name
 * @return string
 *
 * @author: Manoj Gopal
 */
function GetUrlParamValue( name )
{
  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];
}

/**
 * Removes %20 used in spaces from str, used to get param string from URL
 * <p>
 * Created: 24/10/2008
 *
 * @param string
 * @return string
 *
 * @author: Manoj Gopal
 */
function ReFormatUrlString( str )
{
    while ( str.indexOf("%20") != -1 ){
        str = str.replace(/%20/," ");
    }
    return str;
}

/**
 * removes space or ', and replaces with -
 * <p>
 * Created: 24/10/2008

 * @param string
 * @return ID string
 *
 * @author: Manoj Gopal
 */
function stringToId(sString) { 
	var sId = sString.toLowerCase();
	sId = sId.replace(/[\s]+/gi, '-');
	sId = sId.replace(/[\']+/gi, '-');
	return sId;
}

/**
 * Checks if the value is empty in any sense
 * <p>
 * Created: 20/10/2008
 * 
 * @param value Value to check for
 * @return boolean If empty return true, else false
 *
 * @author: Manoj Gopal
 * 
 */
function isEmpty(value)
{
    if ( value == null || value == 'undefined' || value == undefined || value == '' || value == 'null' ) {
        return true;
    }

    return false;
}

/**
 * Extracts and return current browser type
 * <p>
 * Created: 24/10/2008
 * 
 * @return FF/IE FF - if firefox equivalent, IE for Internet Explorer
 *
 */
function GetBrowser() 
{
    var ua = window.navigator.userAgent;
    var msie = ua.indexOf ( "MSIE " );
    var ieVersion = msie > 0 ? parseFloat(ua.substring(msie + 5, ua.indexOf(";", msie))) : 0;    
    var browser = msie > 0 ? "IE" : "FF";
    
    return browser;
}

/**
 * Handler for panaorma object - shows error if flash not installed
 * <p>
 * Created: 25/10/2008
 *
 * @param errorCode Error code generated by panaroma street view
 */
function handleNoFlash(errorCode) {
    if (errorCode == 603) {
        ShowError("Error: Flash doesn't appear to be supported by your browser");
        return;
    }
}  

/**
 * Disable All JS Alerts
 */
function DisableAlerts(){ window.alert = null; }

/**
 * Enable All JS Alerts
 */
function EnableAlerts() { window.alert = jsAlert;  }

