//Test version of javscript utils


function AJAXInteraction(url, callback, p_passthru) {

    var req = reqInit();
//req.overrideMimeType('text/xml');

    req.onreadystatechange = function() {
      if (req.readyState == 4) {
        if (req.status == 200) {
        
          if (callback) {callback(req.responseXML, p_passthru);}
        }
      }
    };
    
    function reqInit() 
    {
        if (window.XMLHttpRequest)
        {
            return new XMLHttpRequest();
        }
        else if (window.ActiveXObject) 
        {
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
    }


    this.doGet = function() {
   
      req.open("GET", url, true);
     
      req.send();
    };

    this.doPost = function(body) {
      req.open("POST", url, true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      req.send(body);
    };
}

//Post asynchronous request for the spaces.live.com/changes.aspx feed via php proxy
function getBaseFeed(p_path,p_hostName)
{
	var errorstate="";
    var url="http://www.spacesbattles.com/proxy_next.php?subdomain=0&hostname="+p_hostName+"&path="+p_path;
	var xmlhttp=new AJAXInteraction(url, getBaseFeedComplete, p_hostName);
    xmlhttp.doGet();

	return errorstate;
}


//Post asynchronous request for profilename.spaces.live/profile/feed/rss profile feed via php proxy
function getSubFeed(p_subDomain,p_hostName)
{

	var errorstate="";
	//define request
	var url="http://www.spacesbattles.com/proxy_next.php?path=profile/feed.rss&subdomain="+p_subDomain+"&hostname="+p_hostName;
    var temp=decodeURIComponent(p_subDomain);
    temp=temp.substring(7,temp.indexOf(".spaces"));
 
    var xmlhttp=new AJAXInteraction(url, getSubFeedComplete, temp);
    xmlhttp.doGet();
	
	return errorstate;
}

//Post asynchronous request for profilename.spaces.live/profile/feed/rss profile feed via php proxy
function getPhotoFeed(p_subDomain,p_hostName,p_passthru,p_callback)
{
   //alert(p_subDomain+"\n"+p_hostName+"\n"+p_passthru.alias+"\n"+p_callback);
	var errorstate="";
	//define request
	var url="http://www.spacesbattles.com/proxy_next.php?path=photos/feed.rss&subdomain="+p_subDomain+"&hostname="+p_hostName;

	var xmlhttp=new AJAXInteraction(url, p_callback, p_passthru);
    xmlhttp.doGet();
	return errorstate;
}

//Callback called after xml fetch of PHOTO feed
function getPhotoFeedComplete(responseXml, p_passThru)
{
    var myXmlDoc=responseXml.documentElement;
	
	var resultArray=null;
	resultArray=parsePhotoFeedRSS(myXmlDoc,p_passThru.alias,p_passThru.title);
	var photoHtml="";
	for (i=0;i<resultArray.length;i++)
	{
	    photoHtml=photoHtml+resultArray[i].photoHtml;
	}
	replaceHtml(photoHtml,"photoFeed",{});
	callbackStatus="getSubFeed";
}

//Replace a div with a string
function replaceHtml(p_string,p_id, p_params) 
{
    for (i in p_params) 
    { 
        p_string=p_string.replace(eval("/"+i+"/g"),p_params[i]); 
    }
    p_string=parseIfs(p_string);
    document.getElementById(p_id).innerHTML=p_string;
}

//Replace a div with a string
function replaceString(p_string, p_params) 
{
    for (i in p_params) 
    { 
        p_string=p_string.replace(eval("/"+i+"/g"),p_params[i]); 
    }
    p_string=parseIfs(p_string);
    return p_string; 
}

function parseIfs(p_stringInput) 
{
    var failsafeCounter=0; 
    while (p_stringInput.indexOf("<if")!=-1 && failsafeCounter<10) 
    {
        var ifStart=p_stringInput.indexOf("<if"); 
        var ifEnd=p_stringInput.indexOf("</if>")+5; 
        var temp=p_stringInput.substring(ifStart,p_stringInput.length); 
        //works 
        var p1=temp.indexOf("(")+1; 
        var p2=temp.indexOf(")"); 
        var temp2=temp.substring(p2+1,temp.length); 
        var ifInnerStart=temp2.indexOf(">")+1; 
        var ifInnerEnd=temp2.indexOf("</if>"); 
        var innerString=temp2.substring(ifInnerStart,ifInnerEnd); 
        var clause=temp.substring(p1,p2); 
        try { 
            if (eval(clause)) 
            {
                //alert("Searching"+p_stringInput+"\n\n"+clause+ " evaluated true\n\nReplacing:"+innerString+" with "+p_stringInput.substring(ifStart,ifEnd)); 
                p_stringInput=p_stringInput.replace(p_stringInput.substring(ifStart,ifEnd),innerString);
            }
            else 
            {
                // alert("Searching"+p_string+"\n"+clause+ " evaluated false\nReplacing:"+innerString+" with blank");
                p_stringInput=p_stringInput.replace(p_stringInput.substring(ifStart,ifEnd),""); 
            }
        }
        catch (e) 
        {
            failsafeCounter=100;
        }
        failsafeCounter+=1;
    }
    return p_stringInput; 
}

//Is feed valid
function feedCheckLocation()
{
    var valid=true;
    if (!resultObj) {valid=false;return valid}
    if (typeof(resultObj.location)==undefined){ valid=false; return valid}
    if (resultObj.location==null){ valid=false; return valid}
    if (genderMode=="girls" && resultObj.gender=="Male"){ valid=false; return valid}
    if (genderMode=="boys" && resultObj.gender=="Female"){ valid=false; return valid}
    if (resultObj.image==undefined){ valid=false; return valid}
    if (resultObj.image=="no data"){ valid=false; return valid}
    if (resultObj.location=="no data"){ valid=false; return valid}
    return valid;
}
//Is feed valid
function feedCheck()
{
    var valid=true;
    if (!resultObj) {valid=false;return valid}
    if (genderMode=="girls" && resultObj.gender=="Male"){ valid=false; return valid}
    if (genderMode=="boys" && resultObj.gender=="Female"){ valid=false; return valid}
    if (resultObj.image==undefined){ valid=false; return valid}
    if (resultObj.image=="no data"){ valid=false; return valid}
    return valid;
}


//Go and get the Map
	function GetMap()
	{
		try
		{
			map = new VEMap('myMap');
			map.SetDashboardSize(VEDashboardSize.Small);
			map.LoadMap();
			
			var mode = map.GetMapMode();
			var view=null;
			var latLong=new VELatLong(51.500152,0);
			if (mode == VEMapMode.Mode3D)
			{
				//Set initial position of map
				view = new VEMapViewSpecification(latLong, 1, 1000000, -55, 0);
				map.SetMapView(view);
			}
			else
			{
				view = new VEMapViewSpecification(latLong, 2);
				map.SetMapView(view);
			}
			
			map.AttachEvent("onendpan", panComplete);
			map.AttachEvent('onmodenotavailable', No3DInstalled);
			var href=String(window.location);
			
		}
		catch (e)
		{
			errorHandler(e+"in function GetMAP");
		}
		
	}
function growImg(imgId)
{
    var h=document.getElementById(imgId).height;
    document.getElementById(imgId).height=64;
    pause=true;
}

function shrinkImg(imgId)
{
    var h=document.getElementById(imgId).height;
    document.getElementById(imgId).height=32;
    pause=false;
}

function showPhotos(p_alias,p_div,p_title)
{
    var photoHtml="<b>Loading data...</b><br/><img src=\"images/smallrotate.gif\" alt=\"busy\"/>";
  replaceHtml(photoHtml,"photoDiv",{});
    var p_alias="http://"+p_alias+".spaces.live.com/";
    var response=getPhotoFeed(p_alias,0,{"alias":p_alias,"div":p_div,"title":p_title},showPhotosComplete);
}

//Callback called after xml fetch of PHOTO feed
function showPhotosComplete(responseXml,p_passthru)
{
    var myXmlDoc=responseXml.documentElement;
	var resultArray=null;
	resultArray=parsePhotoFeedRSS(myXmlDoc,p_passthru.alias,p_passthru.title);

	var photoHtml="";
	for (i=0;i<resultArray.length;i++)
	{
	    photoHtml=photoHtml+resultArray[i].photoHtml;
	}

	replaceHtml(photoHtml,p_passthru.div,{});
	callbackStatus="getSubFeed";
}