// modifyURL.js - functions to modify production (www).southwindsmagazine.com

 URLs to dev URLs where appropriate.
// Doing this in Javascript means we can keep static HTML pages.  It doesn't matter if it fails in production, 'cos the correct action is to do nothing.

// This is the list of target dev hosts.  If our current URL is one of these we will search for the respective prod hosts and replace them with the dev host.
var devHosts = new Array('server','67.192.24.122');
// This 2D array holds lists of hosts to search for in the anchors for the respective dev hosts above.  
// e.g. if Our current URL matches the first entry in the Dev hosts list, then we will use that host to replace URLs that match any item 
//      in the first array in the prodHosts list
var prodHosts = new Array(new Array('www.southwindsmagazine.com


', 'southwindsmagazine.com

'), new Array('server.southwindsmagazine.com

'));

var booRemoveHTTPS = false;
var strResult = '';

function changeAnchors(objRoot, prodHosts, devHost)
// prodHosts is an array of strings to match against hosts in anchor URLs
// Modify any anchors found from prod to dev and return the modified array of matching anchor objects
{
	var objAnchors = new Array();
	var intAnchorCount = 0;
	var objNodes;
	var i, j;

	if (document && document.childNodes && document.childNodes.length > 0)
	{
		if (objRoot == null)
		{
			objRoot = document;
		}
		objNodes = objRoot.childNodes;
		if (objNodes)
		{
			for (i=0; i < objNodes.length; i++)
			{
			
				if (objNodes[i].tagName && (objNodes[i].tagName.toLowerCase() == 'a') && (objNodes[i].href != '') && (objNodes[i].protocol).substring(0, 4) == 'http')
				{
					for (j = 0; j < prodHosts.length; j++)
					{
						if (objNodes[i].hostname.toLowerCase() == prodHosts[j])
						{
							objAnchors = objAnchors.concat(objNodes[i]);
							objNodes[i].hostname = devHost;
							if (booRemoveHTTPS && objNodes[i].protocol == 'https:')
							{
								objNodes[i].protocol = 'http:';
								objNodes[i].port = '80';
							}
							break;
						}
					}
				}
				else if (objNodes[i].childNodes && objNodes[i].childNodes.length > 0)
				{
					var objTmp = changeAnchors(objNodes[i], prodHosts, devHost);
					if (objTmp.length > 0)
					{
						objAnchors = objAnchors.concat(objTmp);
					}
				}
			}
		}
	}

	return objAnchors;
}

function changeForms(objRoot, prodHosts, devHost)
// prodHosts is an array of strings to match against hosts in anchor URLs
// Modify any anchors found from prod to dev and return the modified array of matching anchor objects
{
	var objAnchors = new Array();
	var intAnchorCount = 0;
	var objNodes;
	var i, j;

	if (document && document.childNodes && document.childNodes.length > 0)
	{
		if (objRoot == null)
		{
			objRoot = document;
		}
		objNodes = objRoot.childNodes;
		if (objNodes)
		{
			for (i=0; i < objNodes.length; i++)
			{
				if (objNodes[i].tagName && (objNodes[i].tagName.toLowerCase() == 'form') && (objNodes[i].action).substring(0, 4) == 'http')
				{
					for (j = 0; j < prodHosts.length; j++)
					{
						if (getHostName(objNodes[i].action).toLowerCase() == prodHosts[j])
						{
							objAnchors = objAnchors.concat(objNodes[i]);
							objNodes[i].action = replaceHost(objNodes[i].action, devHost);
							break;
						}
					}
				}
				else if (objNodes[i].childNodes && objNodes[i].childNodes.length > 0)
				{
					var objTmp = changeForms(objNodes[i], prodHosts, devHost);
					if (objTmp.length > 0)
					{
						objAnchors = objAnchors.concat(objTmp);
					}
				}
			}
		}
	}

	return objAnchors;
}

function getHostName(pstrURL)
{
	// returns the hostname from a given URL
	var strReturn;
	var strpos;
	if(pstrURL.substring(0,5) == 'https')
	{
		strpos = 8;
	}
	else if (pstrURL.substring(0,4) == 'http')
	{
		strpos = 7;
	}
	strReturn = pstrURL.substring(strpos, pstrURL.length);
	strReturn = strReturn.substring(0, strReturn.indexOf('/'));
	return strReturn;
}

function replaceHost(pstrURL, pstrNewHost)
{
	//replaces the host of pstrURL and returns the value
	var strProtocol;
	var strFile; 
	strFile = pstrURL.substring(8, pstrURL.length);
	strFile = strFile.substring(strFile.indexOf('/'), strFile.length);
	if (booRemoveHTTPS)
	{
		strProtocol = 'http';
	}
	else
	{
		strProtocol = pstrURL.substring(0, pstrURL.indexOf('://'));
	}
	return strProtocol + '://' + pstrNewHost + strFile 
}

function modifyURLs()
{
	if (location && location.hostname)
	{
		// DOM seems to be working OK, let's go
		var i;
		var objAnchors;
		var objForms;
	
		for (i=0; i < devHosts.length; i++)
		{
			//alert ('Dev Host ' + i.toString() + ' - ' + devHosts[i]);
			if (location.hostname.toLowerCase() == devHosts[i])
			{
				//alert ('Dev Host ' + i.toString() + ' - ' + devHosts[i] + ' matches current host.');
				objAnchors = changeAnchors(null, prodHosts[i], devHosts[i]);
				objForms = changeForms(null, prodHosts[i], devHosts[i]);
				//alert('Anchors: ' + objAnchors.toString());
			}
		}
	}
}


