/**
 * @author Naveed Ul Islam
 */
var cookieCollection = "Adhan";
var nDays = 365; // nDays = No. of days in future to expire
function loadCookies(type){
    var callback = "loadData";
    var allCookies = document.cookie;
    var CookieArray = allCookies.split(';');
    var breakLoop = false;
    for (var j = CookieArray.length - 1; j >= 0; j--) {
        cookieParamenter = extractSubstring(CookieArray[j], cookieCollection + '_', '=');
        cookieValue = extractSubstring(CookieArray[j], cookieParamenter + '=', ';');
        if (cookieParamenter != -1) {
            if (cookieValue != "") {
                switch (type) {
                    case "callLastObject": //Load Last Value with callback
                    	dataSource = "cookie";
                        eval(callback + "(" + cookieValue + ");");
                        breakLoop = true;
                        break;
                    case "checkExistance":
                        breakLoop = true;
                        break;
                    default:
                        if (type) { // Load Specific Value with callback
                            if (cookieParamenter == type) {
                                eval(callback + "(" + cookieValue + ");");
                                breakLoop = true;
                                break;
                            }
                        }
                        else { //Load all values
                            alert(cookieParamenter + " | " + cookieValue);
                        }
                }
            }
        }
        
        if (breakLoop == true) {
            if (type == "checkExistance") {
                return true;
            }
            break;
        }
        
    }
}

function cookieObjStr(){
    var str = "{";
    str += "Latitude:'" + latitude;
    str += "',Longitude:'" + longitude;
    str += "',Dstoffset:'" + dstOffset;
	str += "',Gmtoffset:'" + gmtOffset;
    str += "',City:'" + city;
    str += "',RegionName:'" + region;
    str += "',CountryName:'" + country;
    str += "',dstMismatchAdjust:" + dstMismatchAdjust;
    str += "}";
    return str;
}

function expireCookie(parameter){
    storeCookie(-100, parameter, '');
}

function storeCookiePair(parameter, value){
    storeCookie(nDays, parameter, value);
}

function storeCookie(time, parameter, value){
    var cookie = cookieCollection + '_' + parameter + '=' + value + '; expires=' + expireDate(time) + '; path=/';
    document.cookie = cookie;
}

function extractSubstring(myString, delim1, delim2){
    var substr1 = myString.split(delim1)
    if (substr1 == myString) 
        return -1 // delim1 not found
    var substr2 = substr1[1].split(delim2)
    var len1 = substr1[0].length + delim1.length
    var len2 = substr2[0].length
    return myString.substring(len1, len1 + len2)
}

function expireDate(nDays){
    var one_second = 1000;
    var one_hour = 3600 * one_second;
    var one_day = 24 * one_hour;
    var expire = new Date((new Date()).getTime() + nDays * one_day);
    return expire.toGMTString(); // Time to expire cookie
}
