// Developed By Sofia Celic Dec 2004

//open new window with results
function results_window(ss_els,cs_els) {
imr=window.open("","imagemap_results","location=no,menubar=no,scrollbars=yes,resizable=1,left=20,top=20");
//catch popup blocker error
	try{
		imr.document.open();}
		catch(e) {
			if(e.message.toLowerCase().indexOf('object required')>=0) 
			{
				alert(errortxt1);
				return;
			}
			else if(e.message.toLowerCase().indexOf('access is denied')>=0) 
			{
				if(!imr.closed) imr.close();
				alert(errortxt4);
				return;
			}
	}	

	imr.document.writeln("<html><link rel=\"Stylesheet\" href=\"http://wat2.z6i.org/resources/WAT/scripts/css/generic.css\" type=\"text/css\"><body><a href=\"javascript:window.close();\">"+docp2+"</a>");
	imr.document.writeln("<h1>"+map2+"'"+self.document.title+"'</h1><hr />");
	
	// Server-side image map specs
	imr.document.writeln("<h2>"+map3+"</h2>");
	if(ss_els.length!=0)
	{
		for(var q=0;q<ss_els.length;q++)
		{
			imr.document.writeln("<p>"+ss_els[q]+"</p>");
		}
		imr.document.writeln("<hr>");
	}
	else
	{
		imr.document.writeln("<p>"+map4+"</p>");
	}
	
	// Client-side image map specs
	imr.document.writeln("<hr /><h2>"+map5+"</h2>");
	if(cs_els.length!=0)
	{						
		for(var l=0;l<cs_els.length;l++)
		{
			//first array item is the image		
			if(!cs_els[l][0].hasAttribute('alt'))
			{
				imr.document.writeln("<p>"+cs_els[l][0].outerHTML+"&nbsp;<br /><strong style=\"color:#FF0000\">"+map6+"</strong></p>");
			}
			else
			{
				imr.document.writeln("<p>"+cs_els[l][0].outerHTML+"</p><hr />");
			}
		
			//second array item is the map opening tag
			var capsule=cs_els[l][1].outerHTML;
			capsule=capsule.slice(0,(capsule.indexOf('>')+1));
			imr.document.writeln("<p>"+replace_brackets(capsule)+"</p>");
			
			//next array items except last are 'area' elements
			for(var m=2;m<cs_els[l].length-1;m++)
			{		
				if(!cs_els[l][m].hasAttribute('alt'))
				{
					imr.document.writeln("<p>"+replace_brackets(cs_els[l][m].outerHTML)+"<strong style=\"color:#FF0000\">"+map7+"</strong></p>");
				}
				else
				{
					imr.document.writeln("<p>"+replace_brackets(cs_els[l][m].outerHTML)+"</p>");
				}
			}
			imr.document.writeln("<p>"+cs_els[l][(cs_els[l].length-1)]+"</p>");
			
			//write in client-side map and area elements in code
			imr.document.write(capsule);
			for(var m=2;m<cs_els[l].length-1;m++)
			{
				imr.document.write(cs_els[l][m].outerHTML);
			}
			
			
			imr.document.writeln("<hr />");
		}
	}
	else
	{
		imr.document.writeln("<p>"+map8+"</p><hr />");
	}
	
	imr.document.writeln("</body></html>");
	imr.document.close();
}

function replace_brackets(str){
	rep1=/</gi;
	replaceIt=str.replace(rep1, "&lt;");
	rep2=/>/gi;
	replaceIt=replaceIt.replace(rep2, "&gt;");
	return replaceIt;	
}



	//array to store server-side image maps
	ss_maps=new Array();

	//each map element has its associated code (image, map, areas) stored in an array
	// this 2D array is used to collate all of the individual map arrays
	all_cs_map_arrays=new Array();
	
	var usemap_str;
	var img_els=document.getElementsByTagName('img');


function start()
{

//first check page to see if vraiable are loaded
if (typeof(testvar) == "undefined"){

 setTimeout ('start()', 500);
}

else{
	//first check page for frames
	var frm=document.getElementsByTagName('frame');
	if (frm.length>0)
	{
		alert(errortxt3);
		return;
	}
	serverside();
	clientside();
	if(ss_maps.length==0&&all_cs_map_arrays.length==0)
	{
		alert(map9);
	}
	else
	{
		results_window(ss_maps,all_cs_map_arrays);
	}
}
}
//check for server-side image maps
function serverside()
{
	if(img_els.length!=0)
	{
		for(var n=0;n<img_els.length;n++)
		{
			if(img_els[n].hasAttribute("isMap"))
			{
				ss_maps.push(img_els[n].outerHTML);
			}
		}
	}
}


//check for client-side image maps
function clientside() 
{
	//first look for 'map' element. if none present return negative
	//this means that even if an img element has a 'usemap' attribute it will return negative
	var map_els=document.getElementsByTagName('map');
	

	if(map_els.length!=0)
	{
		for(var j=0;j<img_els.length;j++)
		{
			if(img_els[j].hasAttribute("useMap"))
			{//alert(img_els[j].useMap);
				for(var i=0;i<map_els.length;i++)
				{
					usemap_str=img_els[j].getAttribute("useMap");
					//each usemap value starts with a # which needs to be removed
					usemap_str=usemap_str.slice(usemap_str.indexOf("#")+1);	
					//alert(usemap_str);		
					mapname_str=map_els[i].getAttribute("name");
					mapid_str=map_els[i].getAttribute("id");
					if((usemap_str==mapname_str)||(usemap_str==mapid_str))
					{
						//create array of map and child areas
						map_array = new Array();
						//get image properties
						var img_src=img_els[j].getAttribute("src");
						
						//add img node as first item of array
						map_array.push(img_els[j]);
					
						//add map element as second item of array
						map_array.push(map_els[i]);
						
						//get child 'area' elements of map
						if(map_els[i].hasChildNodes)
						{
							var children=map_els[i].childNodes;
							for (var k=0;k<children.length;k++)
							{
								if(children[k].nodeType==1)
								{
									//check for 'area' element & alt attribute
									if(children[k].nodeName.toLowerCase()=="area")
									{
										map_array.push(children[k]);
									}
								}
								
							}
							
						}
						map_array.push(replace_brackets("</map>"));
						all_cs_map_arrays.push(map_array);
					}
				}
			}
		}
	}
}

start();
