// -*- Mode: JavaScript; tab-width: 4; indent-tabs-mode: nil; -*-
// GeoCode
// Inserts a script tag that runs WriteCookie with data EG:
// WriteCookie({
//     "IP"          : "123.123.123.123",
//     "countrycode" : "US",
//     "country"     : "United States",
//     "regioncode"  : "R_HI",
//     "region"      : "High income: OECD"
// })
function GeoCode()
{
        var url = 'http://service.clockfour.net/Service.asmx/GeoCode?type=jsonp';
  
  		// var script = document.createElement("script");        
        // script.setAttribute("src", url);
        // script.setAttribute("type", "text/javascript");     
        // var tagheader = document.getElementsByTagName("head")[0];
        // tagheader.appendChild(script);  
        
        document.write('<scr'+'ipt type="text/javascript" src="'+ url +'"></scr'+'ipt>');
}

function WriteCookie(JSONResults){
	//alert(JSONResults.countrycode);
	var cc = JSONResults.countrycode;
	if(cc=="AU"){
		window.location="http://www.whatwouldyouliketochange.com.au/";
	}
	
	
//	alert(JSONResults.countrycode);    //write cookies
//    setCookie('geolocator:IP', JSONResults.IP);
//    setCookie('geolocator:countrycode', JSONResults.countrycode);
//    setCookie('geolocator:country', JSONResults.country);
//    setCookie('geolocator:regioncode', JSONResults.regioncode);
//    setCookie('geolocator:region', JSONResults.region);
}

//set the cookie
//c_name is the cookie name
//value is the value of the cookie
//expiredays is the number of days to let the cookie live
function setCookie(c_name, value, expiredays)
{
    expiredays = 365;
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

//get the value of the cookie
//c_name is the cookie to search for
function getCookie(c_name)
{
    //check if there are any cookies
    if (document.cookie.length > 0)
    {
        //get the cookie
        c_start = document.cookie.indexOf(c_name + "=");
        //if it exists, get the value
        if (c_start != -1)
        { 
            //parse the cookie to find the value
            c_start = c_start + c_name.length + 1; 
            //find the expiration date
            c_end = document.cookie.indexOf(";", c_start);
            //if there is none, use the length
            if (c_end == -1)
            {
                c_end = document.cookie.length;
            }
            //return the value
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}


// Pass in an object. 
// The keys will be used as the flashvar keys.
// The values as the flashvar values.
// EG: HPModBuild({ cow:'moo' }) would result in flashvars of "cow=moo"
function HPModBuild(varsObject){
    var varString = [];
    for (var varName in varsObject) {
        varString.push( varName + '=' + encodeURIComponent(varsObject[varName]) );
    }
    varString = varString.join('&');
    
    if (AC_FL_RunContent == 0) {
        alert("This page requires AC_RunActiveContent.js.");
    } else {
        AC_FL_RunContent(
            'codebase' , 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
            
            'width'   , '570',
            'height'  , '213',
            'id'      , 'hp_module',
            'align'   , 'middle',
            'src'     , 'hp_module',
            'quality' , 'high',
            'bgcolor' , '#003150',
            'name'    , 'hp_module',
            
            'allowscriptaccess' , 'sameDomain',
            'allowfullscreen'   , 'false',
            'pluginspage'       , 'http://www.macromedia.com/go/getflashplayer',
            'movie'             , 'hp_module',
            'flashvars'         , varString
        );
    }
}

