﻿function ajaxPostRequest(url, data, successFN, errorFN, busyHolder) {
    showBusy(busyHolder);
    $.ajax({
        url: url,
        type: "POST",
        data: JSON.stringify(data),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            removeBusy();
            if (successFN != null) {
                successFN(data);
            }
        },
        error: function (data) {
            if (errorFN != null) {
                errorFN(data);
            }
        }
    });
}

function ajaxGetRequest(url, successFN, errorFN, busyHolder) {
    
    xdr = new XDomainRequest();
    xdr.onload = function () {
        alert(xdr.responseText);
    }
    xdr.open("GET", url); //thisURl ->your cross domain request URL 
    //pass your data here
    xdr.send(); 
}

function showBusy(holder) {    
    $('#' + holder).append('<img id="busy" src="/images/busy.gif" alt="busy" />');
}

function removeBusy() {
    $('#busy').remove();
}

function growl(message, header, life, beforeCloseFN) {
    lifeValue = 3000;
    if (life != undefined || life != null) {
        lifeValue = life;
    }
    $.jGrowl(message, {
        beforeClose: beforeCloseFN,
        animateOpen: {
            height: 'show'
        },
        life: lifeValue,
        header: header
    });
}

function submitForm(form) {
    $('#' + form).submit();
}

// Original script by Josh Fraser (http://www.onlineaspect.com)
// Some customization applied in this script code
var minutes;
function calculate_time_zone() {
    var rightNow = new Date();
    var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);  // jan 1st
    var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); // june 1st
    var temp = jan1.toGMTString();
    var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));
    temp = june1.toGMTString();
    var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));
    var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
    var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
    var dst;
    if (std_time_offset == daylight_time_offset) {
        dst = "0"; // daylight savings time is NOT observed
    } else {
        // positive is southern, negative is northern hemisphere
        var hemisphere = std_time_offset - daylight_time_offset;
        if (hemisphere >= 0)
            std_time_offset = daylight_time_offset;
        dst = "1"; // daylight savings time is observed            
    }
    var i;
    // Here set the value of hidden field to the ClientTimeZone.
    minutes = convert(std_time_offset) + "_" + dst;
    $.cookie("timezone", minutes);
}
// This function is to convert the timezoneoffset to Standard format
function convert(value) {
    var hours = parseInt(value);
    value -= parseInt(value);
    value *= 60;
    var mins = parseInt(value);
    value -= parseInt(value);
    value *= 60;
    var secs = parseInt(value);
    var display_hours = hours;
    // handle GMT case (00:00)
    if (hours == 0) {
        display_hours = "00";
    } else if (hours > 0) {
        // add a plus sign and perhaps an extra 0
        display_hours = (hours < 10) ? "+0" + hours : "+" + hours;
    } else {
        // add an extra 0 if needed
        display_hours = (hours > -10) ? "-0" + Math.abs(hours) : hours;
    }
    mins = (mins < 10) ? "0" + mins : mins;
    return display_hours + ":" + mins;
}

function setCity(city) {
    $.cookie("city", city);
}
function setRegion(region) {
    $.cookie("region", region);
}
function setCountry(country) {
    $.cookie("country", country);
}
function setLatitude(latitude) {
    $.cookie("latitude", latitude);
}
function setLongitude(longitude) {
    $.cookie("longitude", longitude);
}

function setNoGeo() {
    setCity('');
    setRegion('');
    setCountry('');
    setLatitude(-1);
    setLongitude(-1);
}

function handleRegionData(data) {
    if (data.Status.code == 200) {
        var country = data.Placemark[0].AddressDetails.Country.CountryName;
        setCountry(country);
        var address = data.Placemark[0].address;
        $.cookie("address", address);        
    }
}

function calculateRegionData(lat,lng, handleData) {
    var point = new GLatLng(lat, lng);
    var geocoder = new GClientGeocoder();
    if (handleData == null) {
        geocoder.getLocations(point, handleRegionData);
    } else {
        geocoder.getLocations(point, handleData);
    }
}

function geo_success(position) {
    setLatitude(position.coords.latitude);
    setLongitude(position.coords.longitude);
    calculateRegionData(position.coords.latitude, position.coords.longitude);
}

function geo_error() {
    setNoGeo();
}

function calculateLocation(locationSuccess,locationFailure) {
    if (geo_position_js.init()) {
        if (locationSuccess == null) {
            geo_position_js.getCurrentPosition(geo_success, geo_error);
        } else {
            geo_position_js.getCurrentPosition(locationSuccess, locationFailure);
        }
    }
}


function DisplayError(targetId, errorStr) {
    $("#" + targetId).html('');
    $("#" + targetId).append("<ul><li>" + errorStr + "</li></ul>");
}

function getParameterByName(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 decodeURIComponent(results[1].replace(/\+/g, " "));
}
