﻿function track_location(src, dest, ispopup, isbank) {
    if (dest == '') {
        return;
    }
    var url = 'http://www.mweb.co.za/system/clstat.aspx?src=' + src + '&dest=' + escape(dest);
    if (ispopup) {
        options = 'location=yes,resizable=yes,height=500,width=780,screeny=0,screenx=0,left=0,top=0,scrollbars=yes,toolbar=yes,menu=yes';
        if (isbank) {
            options += ',status=yes';
        }
        winpop = window.open(url, '', options);
        if (winpop.focus) {
            setTimeout('winpop.focus();', 250);
        }
        return;
    }
    else {
        window.location.href = url;
        return;
    }
}

function openPopup(sender, windowTitle, width, height) {
    var options = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=" + width + " ,height=" + height;
    if (sender.rel != '' && sender.rel != undefined)
        window.open(sender.rel, windowTitle, options);
}

function OpenWindow(url, width, height, title) {
    var newWindow = window.open(url, title, 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=yes,left=100,top=150,width=' + width + ',height=' + height);
}

// Charl Marais (26 Feb 2010)
// Processes results returned from Fin24 REST services. error, warning and processDate are optional.
// PARAMETERS
// data: The "ServiceResult<>" json return result to process
// success: function(data : object) method that will be called if the ServiceResult is in the success state (0). 
// error: function(message : string) method that will be called if the ServiceResult is in the error state (1).
// warning: function(message : string) method that will be called if the ServiceResult is in the warning state (2).
// processDate: Boolean indicating whether the json object should be parsed for dates.
function processWcfResult(data, success, error, warning, processDate) {
    if (data) {
        if (data.Result == 2) {
            if (typeof (error) == 'function')
                error(data.ErrorMessage);
        }
        else if (data.Result == 3) {
            if (typeof (warning) == 'function')
                warning(data.ErrorMessage);
        }
        else if (typeof (success) == 'function') {
            if (processDate && data.Object) {
                var jsonString = JSON.stringify(data.Object);
                jsonString = jsonString.replace(/"\/Date\((\d+)(\+\d{4})\)\/"/g, "$1");
                data.Object = JSON.parse(jsonString);
            }
            success(data.Object);
        }
    }
    else
        error("'data' is null");
}

function numberFormat(value, numberFormat) {
    $("body").append("<input type='hidden' id='format'/>");
    $("#format").val(value).format({ format: numberFormat, thousandSeparator: ',', locale: "za" });
    value = $("#format").val();
    $("#format").remove();
    return value;
}


// Return string for the date in the format of yyyy-mm-dd
function GetShortDate(dateObj) {
    var date = dateObj.getDate();
    var month = dateObj.getMonth();
    date = date.toString().length == 1 ? "0" + date : date;
    month = month.toString().length == 1 ? "0" + month : month;
    return dateObj.getFullYear() + "-" + month + "-" + date;
}

function IsUrlValid(url) {
   var v = new RegExp(); 
    v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
    
    if (v.test(url)) {
        return true;
    } 
    else {
        return false;
    }
}


String.prototype.format = function (obj) {
	var s = this;

	for (prop in obj) {
		s = s.replace(new RegExp('\\{' + prop + '\\}', 'gm'), obj[prop]);
	}

	return s;
}
  
