// JavaScript Document

/*  This function shows or hide (toggles the display) project specific standards when
 *  the user clicks on the project heading. 
 *  If 'noToggle' is true, hide all the project specific standards and display only the selected project
 *  @textId - id of the <div> which has the project specific standards text - <div id="PRJ-AG1-TEXT"
 *  @imgId  - id of the <img> which has got the right/down arrow for the same project - <img id="PRJ-AG1-IMG"
 *  @[noToggle] - [optional] boolean value decides if the function call is a toggle event or not
 *
 */
 
function showHide(textId,imgId,noToggle)
{
	if(arguments.length > 2)	
	{
		var allDivs = document.getElementsByTagName('div');
		var allImgs = document.getElementsByTagName('img');
		var reText = new RegExp("^PRJ-.*-TEXT$");
		var reImg = new RegExp("^PRJ-.*-IMG$");
		
		for(var i=0; i<allDivs.length; i++)
		{
			if(reText.test(allDivs[i].id))
			{
				document.getElementById(allDivs[i].id).style.display = 'none'
			}
		}
		
		for(var i=0; i<allImgs.length; i++)
		{
			if(reImg.test(allImgs[i].id))
			{
				document.getElementById(allImgs[i].id).src = '/standards/images/arrowRIGHT.gif'
			}
		}
		
		if(document.getElementById(textId))
		{// the element exist on the page
		
			document.getElementById(textId).style.display = ''
			document.getElementById(imgId).src = '/standards/images/arrowDOWN.gif'
		}
	}
	else
	{
		if(document.getElementById(textId))
		{// toggle the display only if the element exist on the page
		
			if(document.getElementById(textId).style.display =='none')
			{
				document.getElementById(textId).style.display = ''
				document.getElementById(imgId).src = '/standards/images/arrowDOWN.gif'
			}
			else
			{
				document.getElementById(textId).style.display = 'none'
				document.getElementById(imgId).src = '/standards/images/arrowRIGHT.gif'
			}
		}
		else
		{
			alert("Element does not exist")
		}
	}
}


/*
 *  This function sets a new cookie or updates an existing cookie based on the 
 *  option selected in the dropdown. 
 *   - domain : specify a domain name if there is one.e.g., microsoft.com or google.com 
 *  The cookie expires in one year (365 days)
 *  365 days in a year
 *  24 hours in a day
 *  60 minutes in an hour
 *  60 seconds in a minute
 *  1000 milliseconds in a second
 *  **If you want to modify the expiry time for the cookie, change the days accordingly
 */
 
function setProjSpecStdsForUser(selectedProjID)
{	
	var now = new Date();
	now.setTime(now.getTime() + 365*24*60*60*1000);
	
	var path = "/";	
	var domain = "";  // Specify a domain name
	
	setCookie("UXUserProject", selectedProjID, now, path,domain)
		
	showProjSpecStdsForUser();
}

/*
 *  This function checks the cookie for the project previously selected and 
 *   - shows the project specific standards
 *   - selects the project in the dropdown list
 *  This function must be called on onLoad of <body> of all the page where project specific standards
 *  need to be displayed. - <body onLoad="javascript:showProjSpecStdsForUser();">
 */

function showProjSpecStdsForUser()
{
	var curUserProj = getCookie("UXUserProject");
	if(curUserProj)
	{	
		showHide("PRJ-"+curUserProj+"-TEXT","PRJ-"+curUserProj+"-IMG",true);
		selectPrjInDropdownList(curUserProj);
	}
}

/*
 *  This function sets the project option in the application dropdown list based on the cookie value
 *  **Assumption: the dropdown list select id must be 'UXAppDropdownSelect'**
 *  @optVal - option value to select in the dropdown
 */

function selectPrjInDropdownList(optVal)
{
	var appDropdownList = document.getElementById('UXAppDropdownSelect');
	if(appDropdownList)
	{
		for(var i=0; i<appDropdownList.options.length; i++)
		{
			if(appDropdownList.options[i].value == optVal)
			{
				// Item found. Set its selected property and exit the loop
				appDropdownList.options[i].selected = true;
				break;
			}
		}
	}
}

/*  
 *  This generic function sets a new cookie
 *  @name      - name of the cookie
 *  @value     - value of the cookie
 *  @[expires] - [optional] expiration date of the cookie (defaults to end of current session)
 *  @[path]    - [optional] path for which the cookie is valid (defaults to path of calling document)
 *  @[domain]  - [optional] domain for which the cookie is valid (defaults to domain of calling document)
 *  @[secure]  - [optional] Boolean value indicating if the cookie transmission requires a secure transmission
 *  * an argument defaults when it is assigned null as a placeholder
 *  * a null placeholder is not required for trailing omitted arguments
 */

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


/*
 *  This generic function gets an existing cookie
 *  @name - name of the desired cookie
 *  return string containing value of specified cookie or null if cookie does not exist
 */

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


/*
 *  This generic function deletes an existing cookie
 *  @name     - name of the cookie
 *  @[path]   - [optional] path of the cookie (must be same as path used to create cookie)
 *  @[domain] - [optional] domain of the cookie (must be same as domain used to create cookie)
 *  path and domain default if assigned null or omitted if no explicit argument proceeds
 */

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

