// list of top most menu names and links
var mainMenu = new Array ("Home", "Buying@Columbia", "Training", "Forms", "Policies", "For Vendors", "Newsletter", "FAQs", "About Us / Feedback");
var mainMenuLinks = new Array("/purchasing/index.html", "/purchasing/buying.html", "/purchasing/training.html", "/purchasing/forms.html", "/purchasing/policies.html", "/purchasing/vendors.html", "/purchasing/news.html", "/purchasing/faq.html", "/purchasing/about.html");


// array of top menu items and array of children (if any).  Way to link menu names with child menus.
var menuArray = new Array();
menuArray["Home"] = new Array(0);
menuArray["Buying@Columbia"] = new Array ("How to Buy", "Hiring a Service Provider", "Using Local & Small Businesses", "Preferred Vendor List", "Personal Purchases", "P-card", "Vendor Classifieds");
menuArray["Training"] = new Array ("Procurement Overview", "How Do I..", "Tutorials", "Change Orders", "P-Card", "Service Providers");
menuArray["Forms"] = new Array(0);
menuArray["Policies"] = new Array ("General Purchasing", "Vendor Insurance", "Medical Product & Equipment Recall", "Service Providers", "Purchase Orders & EZ-POs", "Purchasing Card");
menuArray["For Vendors"] = new Array ("Registration");
menuArray["Newsletter"] = new Array ("Archives");
menuArray["FAQs"] = new Array(0); 
menuArray["About Us / Feedback"] = new Array ("Staff Directory", "Org Chart", "Buyer Search");


//links associated with the child menus (if any)
var menuArrayLinks = new Array();
menuArrayLinks["Home"] = new Array(0);
menuArrayLinks["Buying@Columbia"] = new Array("/purchasing/howto.html", "/purchasing/hire_consultant.html", "/purchasing/uls_biz.html", "/purchasing/pv_list.html", "/purchasing/personal_p.html", "/purchasing/p-card.html", "/purchasing/classifieds/index.asp");
menuArrayLinks["Training"] = new Array ("/purchasing/required_training.html", "/purchasing/how_do_i.html", "/purchasing/tutorials/index.html", "/purchasing/changeorders.html", "/purchasing/pcardTraining.html", "/purchasing/serviceProvider.html");
menuArrayLinks["Forms"] = new Array(0);
menuArrayLinks["Policies"] = new Array ("http://www.columbia.edu/cu/administration/policylibrary/responsible_office/pchs.html", "/purchasing/pdf/insuranceRequirements.pdf", "/purchasing/pdf/productRecall.pdf", "/purchasing/consultant.html", "/purchasing/po_ez.html", "/purchasing/pcardPolicy.html");
menuArrayLinks["For Vendors"] = new Array ("/purchasing/qualification/index.html");
menuArrayLinks["Newsletter"] = new Array ("/purchasing/archives.html");
menuArrayLinks["FAQs"] = new Array ("/purchasing/faq.html")
menuArrayLinks["About Us / Feedback"] = new Array ("/purchasing/staff_dir.html", "http://finance.columbia.edu/org_charts/purchasing_office.html", "/purchasing/buyer_search.html");


//tertiary menu items and links for how to page
var howToArray = new Array("Office Solutions", "Computer & Related Supplies", "Travel Services", "Medical Supplies & Equipment", "Leasing Equipment", "Hiring a Service Provider", "Facilities");
var howToArrayLinks = new Array ("/purchasing/office_solutions.html", "/purchasing/computer_rs.html", "/purchasing/contracts/travelServices.asp", "/purchasing/medical.html", "/purchasing/leasing.html", "/purchasing/hire_consultant.html", "/purchasing/facilities.html");


var mainMenuTertiary = "Buying@Columbia";	// top menu item containing submenu linking to tertiary menu
var subMenuTertiary = "How to Buy";			// sub menu item containing actual tertiary menu


var newsMenu = mainMenu[6];
var newsMenuArchive = menuArray["Newsletter"][0];



//////////////// DO NOT EDIT BELOW THIS POINT.///////////////////////////////////////

// the filename (with extension) of the current page.

var pageName = location.pathname ;

function createTertiaryMenu(){
	
	//create menu
	
	tertiaryList = document.createElement('UL');
					
	for (j=0; j < howToArrayLinks.length; j++){
	   if ( pageName == howToArrayLinks[j] ) {
		tertiaryListItem = createListItem(howToArray[j], howToArrayLinks[j], 'active_parent_small_sub');
	   } else {
	    tertiaryListItem = createListItem(howToArray[j], howToArrayLinks[j], 'inactive_parent_small_sub');
	   }
		
		tertiaryList.appendChild(tertiaryListItem);
		document.getElementById('tertiary_menu').appendChild(tertiaryListItem);
		
		if ( pageName == howToArrayLinks[j]) {
			subMenu = document.createElement('UL');
	
			for ( i = 0; i < menuArray[mainMenuTertiary].length; i++ ) {

				childListItem = createListItem(menuArray[mainMenuTertiary][i], menuArrayLinks[mainMenuTertiary][i], 'inactive_parent_small');
				subMenu.appendChild(childListItem);
			}
	
			parentListItem = document.getElementById(mainMenuTertiary);
			parentListItem.appendChild(subMenu);

		    parentListItem.setAttribute('class', 'active');
			parentListItem.setAttribute('className', 'active');
         
         	childMenuWithTertiary = document.getElementById(subMenuTertiary).childNodes[0];

			childMenuWithTertiary.setAttribute('class', 'active_parent_small');
			childMenuWithTertiary.setAttribute('className', 'active_parent_small');
		
		} 
	}
}



function startMenu(){
		displayMenu( mainMenu, mainMenuLinks );
}



function displayMenu( menu, links ){
	
	/*
	  loop through each part of the main menu, and print it out. 
	  if the current page belongs to the group of either child links or parent links
	  print out the parent AND children links, and make the appropriate link "active"	
	*/
	
		for (i=0; i < menu.length; i++) {
			
			//create an <LI> and <a> for each member of the top most menu list.
			if ( pageName == links[i] ) {
			  listItem = createListItem(menu[i], links[i], 'active_parent'); 
			} else {
			  listItem = createListItem(menu[i], links[i], 'parent_is_active'); 
			}
			  /* if the top menu item has a child menu, is the current page:
			  	  1.  the link associated with current top menu item ?
				  2.  a child link which is part of the current top menu item?
			  */
			
			// if current page == the current menu link, make link active and if it has a child menu, display it.  
			if ( links[i] == pageName ) { 
			   listItem.setAttribute('className', 'active'); //set class AND className because IE uses className....
			   listItem.setAttribute('class', 'active');
			   listItem.childNodes[0].setAttribute('class', 'active_parent');
			   listItem.childNodes[0].setAttribute('className', 'active_parent');

			   if ( menuArray[menu[i]].length > 0 ) {
			   		unorderedList = createChildMenu( menuArray[menu[i]], menuArrayLinks[menu[i]] , listItem);
					listItem.appendChild(unorderedList);
			   }
			
			} else if ( menuArray[menu[i]].length > 0 ) {
				
				// if one of the child menu links is the current page, display submenu, make link "active" and parent "active"
				for ( a = 0; a < menuArray[menu[i]].length; a++ ){
					
					if ( pageName == menuArrayLinks[menu[i]][a] ) {
					   unorderedList = createChildMenu( menuArray[menu[i]], menuArrayLinks[menu[i]], listItem );
					   listItem.appendChild(unorderedList);
				    }
				}	
				    
			 }	
			 
				document.getElementById('site_nav').appendChild(listItem);
	      }	
}


//creates actual <li> and <a> and appends class if necessary.
function createListItem(menuName, linkName, className){

	var listItem = document.createElement('LI');
	var anchorLink = document.createElement('A');
	var anchorText = document.createTextNode(menuName);			

	anchorLink.setAttribute('href', linkName);
	anchorLink.appendChild(anchorText);			
	listItem.setAttribute('id', menuName);
	
	
	//if ( pageName  == linkName ){
		anchorLink.setAttribute('class', className);  //set class AND className because IE uses className....
		anchorLink.setAttribute('className', className);
	//} 
	
	listItem.appendChild(anchorLink);
	
	return listItem;
}


//creates the entire child menu and returns the <UL>
function createChildMenu (menuArraySub, menuArrayLinksSub, parentItem){

	unorderedList = document.createElement('UL');

	for (j=0; j < menuArraySub.length; j++){
	   if ( pageName == menuArrayLinksSub[j] ) {
	   	subListItem = createListItem(menuArraySub[j], menuArrayLinksSub[j], 'active_parent_small');
	   } else {
	    subListItem = createListItem(menuArraySub[j], menuArrayLinksSub[j], 'inactive_parent_small');
	   } 
		unorderedList.appendChild(subListItem);
						
		if (subListItem.childNodes[0].getAttribute('class') == 'active_parent_small') {					  
		  parentItem.setAttribute('class', 'active');  //set class AND className because IE uses className....
		  parentItem.setAttribute('className', 'active');
		}							
		
	}						
		
	return unorderedList; 		
}


function newsPage() {
		mainMenuNews = document.getElementById(newsMenu);
		subMenuNews = createChildMenu( menuArray[newsMenu], menuArrayLinks[newsMenu],  mainMenuNews);
		
		mainMenuNews.appendChild(subMenuNews);
		mainMenuNews.setAttribute('class', 'active');  //set class AND className because IE uses className....
		mainMenuNews.setAttribute('className', 'active');
		mainMenuNews.childNodes[0].setAttribute('class', 'active_parent');
		mainMenuNews.childNodes[0].setAttribute('className', 'active_parent');
}

function newsArchivesPage() {
		mainMenuNews = document.getElementById(newsMenu);
		subMenuNews = createChildMenu( menuArray[newsMenu], menuArrayLinks[newsMenu],  mainMenuNews);		
		mainMenuNews.appendChild(subMenuNews);
		
		mainMenuNews.setAttribute('class', 'active');  //set class AND className because IE uses className....
		mainMenuNews.setAttribute('className', 'active');
		
		subMenu = document.getElementById(newsMenuArchive).childNodes[0];
		subMenu.setAttribute('class', 'active_parent_small');  //set class AND className because IE uses className....
		subMenu.setAttribute('className', 'active_parent_small');
}


//START VENDOR FAIR POPUP SCRIPT 
//THIS SETS A COOKIE SO THAT THE POPUP ONLY APPEARS ONCE
var expDays = 120; // number of days the cookie should last

var page = "venFairPopup.html";
var windowprops = "width=300,height=200,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes";

function GetCookie (name) {  
var arg = name + "=";  
var alen = arg.length;  
var clen = document.cookie.length;  
var i = 0;  
while (i < clen) {    
var j = i + alen;    
if (document.cookie.substring(i, j) == arg)      
return getCookieVal (j);    
i = document.cookie.indexOf(" ", i) + 1;    
if (i == 0) break;   
}  
return null;
}
function SetCookie (name, value) {  
var argv = SetCookie.arguments;  
var argc = SetCookie.arguments.length;  
var expires = (argc > 2) ? argv[2] : null;  
var path = (argc > 3) ? argv[3] : null;  
var domain = (argc > 4) ? argv[4] : null;  
var secure = (argc > 5) ? argv[5] : false;  
document.cookie = name + "=" + escape (value) + 
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
((path == null) ? "" : ("; path=" + path)) +  
((domain == null) ? "" : ("; domain=" + domain)) +    
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {  
var exp = new Date();  
exp.setTime (exp.getTime() - 1);  
var cval = GetCookie (name);  
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function amt(){
var count = GetCookie('count')
if(count == null) {
SetCookie('count','1')
return 1
}
else {
var newcount = parseInt(count) + 1;
DeleteCookie('count')
SetCookie('count',newcount,exp)
return count
   }
}
function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function checkCount() {
var count = GetCookie('count');
if (count == null) {
count=1;
SetCookie('count', count, exp);

window.open(page, "", windowprops);

}
else {
count++;
SetCookie('count', count, exp);
   }
}
