
var SERVLET_STRING = '/!redirect/';
var debug=false;
						  

	/* Only update the link if we're supposed tof
		1. It starts with http
		2. The CURRENT hostname is not dev.accu-chek.*
		3. The hostname does not contain *.accu-chek.*
	*/
function isThisAnUpdateCandidate( url ) {
	
	url = url.toLowerCase();
	if ( 
			(
				url.substring(0, 4) == 'http'
					&&
				location.href.toLowerCase().indexOf('dev.accu-chek.com') == -1
					&&
				url.indexOf('.accu-chek.') == -1 
			)
			||
			debug
		)
		{
			return true;
		} 
		
		return false;
}

function processLink(tag)
{
	var queryString = tag.getAttribute('href');

	if (!queryString || queryString=="" || queryString.length==0) return;

	if ( isThisAnUpdateCandidate(queryString) )
		{
			// Replace the HREF's :// with just a /, and stick on the redirect servlet
			tag.setAttribute('href', SERVLET_STRING + tag.getAttribute('href').replace("://", "/"));
			if (debug) alert('HREF->'+ tag.getAttribute('href'));

		} 
		//else { alert(queryString.toLowerCase().substring(0, 3)); }
}



/* 
	1. Iterate through every link AND form action on the page.
	2. If their HREF or ACTION does not contain "_pageLabel", add "_pageLabel=$CURRENT_PAGE"
*/
function updateLinks() {

 if (debug) alert("Starting updateLinks function");

  // Find any link tags and grab their content
  tgs = document.getElementsByTagName('a');
  if (!tgs) return;
  for (var i=0;i<tgs.length;i++) {
	processLink( tgs[i] );
  }	
// Finally, if this is on development, update all of the image tags
if (location.href.toLowerCase().indexOf('dev.accu-chek.com') != -1) {
  tgs = document.getElementsByTagName('img');
  for (var i=0;i<tgs.length;i++) {
        if (tgs[i].src.indexOf('acs')==-1 ) {
                tgs[i].src = ('http://wip.accu-chek.com'+tgs[i].src.substring(24, tgs[i].src.length) );
        }
  }
}

  
  if (debug) alert("Finished updateLinks function");
}



