 	/*
		Build a dynamically generated 'smart' menu.
	
		When		Who		Why
		10-01-07	Arman	Created
		10-01-07	Arman	Added Array Sub menu's
		
	*/

    // base URL for the corp and tools sites (staging vs prod)
    //var corpURL = "http://www-local";
    //var corpURL = "http://www-stage.enterprisemobile.com";
    var corpURL = "http://www.enterprisemobile.com";
    //var toolsURL = "http://tools-local";
    //var toolsURL = "http://tools-stage.enterprisemobile.com";
    var toolsURL = "http://tools.enterprisemobile.com";
 
 	// Image filter
 	var isImage = false;
	
 	// use names
 	var name = 0;
	var url = 1;
 	var submenu = 2;
	
	// setup location and page parsing
	var loc = document.location.href;
	var host = location.protocol + "//" + location.hostname;

    function getArea(url)
    {
        var returnVal = url;
        // if url already ends with a / (but is not the root), chop it off, so we go one level up
        if ( (returnVal.substring(returnVal.length-1,returnVal.length) == "/") && (returnVal != host + "/") ) 
        {   
            returnVal = returnVal.substring(0, returnVal.length-1);
        }
        returnVal = returnVal.substring(0,returnVal.lastIndexOf("/") + 1);
        //alert("getArea(" + url + ") = " + returnVal);
        return returnVal;
    }	
    
    function trimBookmark(url)
    {
    	var trimmed = url;
    	if (url.indexOf("#") > 0)
    	{
    		trimmed = url.substring(0, url.indexOf("#"));
    	}
    	return trimmed;
    }
    
	
	//var area = loc.substr(host.length);
	var page = trimBookmark(loc);
	var area = getArea(page);
	

	// The menu/submenu Array
	var menuArray = Array (
		new Array('ABOUT US', corpURL + '/About-Us.aspx',
			new Array(
			)
		),
		new Array('PARTNERS', corpURL + '/Partners.aspx',
			new Array(
			)
		),
		new Array('IMPLEMENTATION', corpURL + '/Implementation-Services.aspx',
			new Array(
			)
		),
		new Array('OUTSOURCING', corpURL + '/Outsourcing.aspx',
			new Array(
			)
		),
		new Array('NEWS', corpURL + '/New--amp;-Events.aspx',
			new Array(
			)
		),
		new Array('RESOURCES', corpURL + '/Utility-Navigation/Resources.aspx',
			new Array(
			)
		),
		new Array('SOFTWARE', toolsURL + '/#',
		    new Array(
		        /*new Array('EMprovise', corpURL + '/software/emprovise.htm',0),*/
		        new Array('File Upload', corpURL + '/software/fileupload.htm',0),
		        new Array('Free Downloadable Utilities', toolsURL + '/',
			        new Array(
	                    /*new Array('Overview', toolsURL + '/',0),*/
			            new Array('iPhone Config Template', toolsURL + '/download/dl_form.aspx?tool=iphone-config-page', 0),
				        new Array('CAB Sign Tool', toolsURL + '/cabsign/',0),
				        new Array('IP Utility', toolsURL + '/iputil/',0)
				        /*,new Array('FAQs', toolsURL + '/faq/',0)*/
			        )
		        )
		    )
		),
		new Array('CAREERS', corpURL + '/About-Us/Careers.aspx',
			new Array(
			)  
		)
	);

			
			// Get the area the page comes from
			locArray = loc.split("/");
			p_area = locArray[(locArray.length)-2];
			p_file = locArray[(locArray.length)-1];
			
			/*
			alert(locArray.length);
			alert(locArray[(locArray.length)-2]);
			*/
			
	function showArea(){
 		var toAlert = "location [" + loc + "]";
 		toAlert += "\nhost [" + host + "]";
 		toAlert += "\narea [" + area + "]";
 		toAlert += "\npage [" + page + "]";
		alert(toAlert);
	
		//stringObject.substr(start,length)
	}
	
	
	// Check for existing menu image
	
	
	
	function checkImage(img){

		var imageObj = new Image();
    	
		imageObj.onload	 = function(){ //alert('good'); 
		isImage = true  }
		imageObj.onerror = function(){ //alert('bad'); 
		isImage = false }
    	
		imageObj.src = img;
	}
	
	
	function isArea(toCheck){

		//alert("area " + area + " = " + getArea(toCheck) ); 
		
		if(area == getArea(toCheck))
		    return true;
		else
		    return false;
	}
	
	function isPage(toCheck){

		//alert("page " + page + " = " + toCheck ); 
		
		if(page == toCheck) 
		    return true;
		else
		    return false;
	}
	
	
	// Needed to wrap this because of advert
	function doMenu(menu,lvl,areaOverride) {
	
	    if (areaOverride != null && areaOverride != ""){
	        area = areaOverride;
	    }
		
		generateMenu(menu,lvl);
		
		document.write('<img src="images/area.gif" width="200" alt="emobile"/>');
		
		document.write('<p>&nbsp;</p>');
		document.write('<img src="/images/mscert.gif" width="200" height="68" alt="mscert"/>');
	}
	
	function generateMenu(menu,lvl){
		
		//showArea();
		
		for(var i=0; i < menu.length; i++){

				if(isArea(menu[i][url]) || lvl > 0) {
				
					if(isPage(menu[i][url]))
							document.write('<ul class=menunode-' + lvl + '><li><p>' + menu[i][name] + '</p></li></ul>');
					else	document.write('<ul class=menunode-' + lvl + '><li><a href="' + menu[i][url] +'">' + menu[i][name] + '</a></li></ul>');
				
					if(typeof(menu[i][submenu]) == 'object')generateMenu(menu[i][submenu],lvl + 1);
				
				}
				else document.write('<ul class=menunode-' + lvl + '><li><a href="' + menu[i][url] +'">' + menu[i][name] + '</a></li></ul>');
		}

	}
	


	function he(name,domain,ext){
		document.write("<a href=\"mailto");
		document.write(":" + name + "@");
		document.write(domain + "." + ext + "\">" + name + "@" + domain + "." + ext + "<\/a>");
	}
