
//check browser
	var browserName=navigator.appName; // Get the Browser Name
	
	
	// insert the image into user supplied element using inputted id		
	if(browserName=="Microsoft Internet Explorer") // For IE
	{
	    
	    //window.onload = show_image;
	    show_image();
	}
	else // for non IE
	{
	
		if (document.addEventListener)
		{
			document.addEventListener(
				"DOMContentLoaded", 
				show_image(), 
				false
			);
		}
	}	


// base function
function show_image(){
    
	//create ajax request
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	//send request to xml file which holds all the image paths
	xmlhttp.open("GET","test/images.xml",false);
	xmlhttp.send(null);
	xmlDoc=xmlhttp.responseXML;		
	
	total_tags = xmlDoc.getElementsByTagName("image").length
	 
	var previous_value	=	 readCookie('previous'); // capture previous random value from cookie
	var rand_no				=	generateRandom(total_tags); // generate a new random number
	
	// if previous & present random number are equal
	// loop until a different random number is generated
	while(rand_no == previous_value) {
		rand_no	=	generateRandom(total_tags);
	}
	
	
	//save the newly generated random number to cookie
	createCookie('previous', rand_no, 1);


	var scriptParent = getSCRIPTParent("test/random-images.js");
	
	scriptParent.innerHTML += xmlDoc.getElementsByTagName('image_' + rand_no)[0].childNodes[0].nodeValue ;
	
}

// function to generate random number
function generateRandom(total_tags)
{
	var rand_no = Math.random();
	rand_no = rand_no * total_tags; 
	rand_no = Math.ceil(rand_no);
	
	return rand_no;
}

//function to create cookie
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

//function to read cookie
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}	

function getSCRIPTParent(src) {
	getSCRIPTParent.isSupported = document.getElementsByTagName ? true : false;
	var parent = null;
	var scripts;
	var i = 0;
	var end = 0;
	if (getSCRIPTParent.isSupported) {
		scripts = document.getElementsByTagName('script');
		for (i, end = scripts.length; i < end; i++) {
			if (scripts[i].src.indexOf(src) > -1) {
				parent = scripts[i].parentNode;
			}
		}
	}
	return parent;
}

