/*

To Determine numbr of pages in pagination

*/

function getNumberOfPages(theListCtrlID)
{
//alert("getNumberOfPages("+theListCtrlID+")");
	var myNumberOfPages = 0;
	var myListCtrl = document.getElementById(theListCtrlID);
	if (myListCtrl != null)
	{
		var myItemList = myListCtrl.getElementsByTagName("li");
//		if (myItemList != null && myItemList.length > 0)
		if (myItemList != null )
		{
//			myNumberOfPages = myItemList.length;
			var myItem = null;
			for (var  i=0; i < myItemList.length; i++ )
			{
				myItem = myItemList[i];
				if ( myItem != null )
				{
//alert("myItem.id = " + myItem.id );
					if (  myItem.id != null && myItem.id.indexOf("PAGE_") > -1  )
					{
						myNumberOfPages++;
//alert("myNumberOfPages="+myNumberOfPages);
					}
				}
			}
/*			
			var myItem = null;
			myItem = myItemList[myItemList.length-1];
			myNumberOfPages = myItem.id;
*/
		}
	}
	return myNumberOfPages;
}

function getDisplayedPageIndex(theListCtrlID)
{
//alert("getDisplayedPageIndex("+theListCtrlID+")");	
	var myDisplayedPageIndex = 0;
	var myListCtrl = document.getElementById(theListCtrlID);
	if (myListCtrl != null)
	{
		var myItemList = myListCtrl.getElementsByTagName("li");
		if (myItemList != null)
		{
			var myItem = null;
			for (var i = 0; i < myItemList.length; i++)
			{
				myItem = myItemList[i];
				if (  	myItem != null && 
						myItem.id != null && 
						myItem.id.indexOf("PAGE_") > -1 && 
						myItem.style.display != "none" 
					)
				{
					var myStartIndex = myItem.id.indexOf("_");
					var myEndIndex = myItem.id.length;
					myDisplayedPageIndex = myItem.id.substring( myStartIndex+1 );;
//alert("myStartIndex= "+myStartIndex +". myEndIndex="+myEndIndex + ", myDisplayedPageIndex ="+myDisplayedPageIndex );
					break;
				}
			}
		}
	}
	return myDisplayedPageIndex ;
}

function Paginate(thePageIndex, theListCtrlID, thePaginationCtrlID)
{

//alert("Paginate("+thePageIndex+", "+theListCtrlID+", "+thePaginationCtrlID+")");

	var myPageIndex = thePageIndex;
	var myListCtrl = document.getElementById(theListCtrlID);
	if (myListCtrl != null)
	{
		var myNumberOfPages = getNumberOfPages( theListCtrlID );
//alert("myNumberOfPages ="+myNumberOfPages );			

		var myItemList = myListCtrl.getElementsByTagName("li");
		if (myItemList != null)
		{
			var displayedPageIndex = getDisplayedPageIndex(theListCtrlID);
//alert("displayedPageIndex ="+displayedPageIndex );			
//			if ( displayedPageIndex == -1 )
//				displayedPageIndex = 1;
			if (myPageIndex == 0)
			{
				myPageIndex = displayedPageIndex-1;
			}
			
			if (myPageIndex-1 == myNumberOfPages )
			{
				myPageIndex= displayedPageIndex;
				myPageIndex++;
			}

//			if ( myPageIndex != displayedPageIndex )
			{
				var myItem = null;
				for (var i = 0; i < myItemList.length; i++)
				{
					myItem = myItemList[i];
				if (myItem != null && myItem.id != "")
				{
//var myPageID = "PAGE_" + myPageIndex;
//alert("myItem.id="+myItem.id+", myPageIndex="+myPageIndex+", myPageID="+myPageID +", myItem.name="+myItem.name);
					if ( myItem.id == "PAGE_" + myPageIndex)
					{
//alert("MATCH");
						myItem.style.display = "block";
					}
					else
					{
//alert("NO MATCH");
						myItem.style.display = "none";
					}
				}
				}

//alert("myPageIndex="+myPageIndex);			
				
				var myPaginationCtrl = document.getElementById(thePaginationCtrlID);
				if (myPaginationCtrl != null)
				{
					var myPageList = myPaginationCtrl.getElementsByTagName("li");
					if (myPageList != null)
					{
						var myPage = null;
						var myPrevPage = myPageList[0];
						var myNextPage = myPageList[myPageList.length-1];
						for (var j = 0; j < myPageList.length; j++)
						{
							myPage = myPageList[j];
							if ( myPage.id == myPageIndex )
							{
								myPage.className="selected";
			//					myPage.setAttribute("classname", "selected");
							}
							else
							{
								myPage.className="";
			//					myPage.setAttribute("classname", "");
							}
						}
						if (myPageIndex <= 1)
						{
							myPrevPage.style.display = "none";
						}
						else
						{
							myPrevPage.style.display = "block";
						}
						if (myPageIndex >= myPageList.length-2)
						{
							myNextPage.style.display = "none";
						}
						else
						{
							myNextPage.style.display = "block";
						}
					}
				}
				
			}
		}
	}
}

function FilterList_ItemCtrl(theListCtrlID, theItemID)
{
	var myListCtrl = document.getElementById(theListCtrlID);
	if (myListCtrl != null)
	{
		var myItemList = myListCtrl.getElementsByTagName("li");
		if (myItemList != null)
		{
			var myItem = null;
			for (var i = 0; i < myItemList.length; i++)
			{
				myItem = myItemList[i];
				if (myItem.id == theItemID || theItemID == '')
				{
					myItem.style.display = "block";
				}
				else
				{
					myItem.style.display = "none";
				}
			}
		}
	}
}

function FilterList_SelectCtrl(theListCtrlID, theSelectCtrlID)
{
	var mySelectCtrl = document.getElementById(theSelectCtrlID);
	if (mySelectCtrl != null)
	{
		var mySelectedItem = mySelectCtrl.options[mySelectCtrl.selectedIndex];
		if (mySelectedItem != null)
		{
			FilterList_ItemCtrl(theListCtrlID, mySelectedItem.value);
		}
	}
}

function showCtrl(theID, status)
{
	var myCtrl = document.getElementById(theCtrlID);
	if (myCtrl != null)
	{
		if (status)
		{
			myCtrl.style.display = "block";
		}
		else
		{
			myCtrl.style.display = "none";
		}
	}
}

function showCtrl(theCtrlID)
{
	var myCtrl = document.getElementById(theCtrlID);
	if (myCtrl != null)
	{
		if (myCtrl.style.display == "none")
			myCtrl.style.display = "block";
		else
			myCtrl.style.display = "none";

	}
}

function openEmail(path)
{
	var url = window.location;
	path += url	
	window.location.href = (path);
}


// This function determines if strInput is empty:
//        Example usage: if (isEmpty(form.login.value)) { ...
//
function isEmpty(strInput)
{
	if (strInput == "" || strInput == null)
	{
		return true;
	}
	return false;
}


// This function will attempt to validate an e-mail address, if it is given
// It assumes that the email field on the form is called "email".
// It will take 2 parameters: the email address object and true/false if
//  the field is required.
//        Example usage:  if (!isValidEmailAddress(form.email, true)) { ...
//
function isValidEmailAddress(email, blnRequired)
{
   var allValid = true;
   var strInput = email.value;
	
   // Check if the email address exists and if it is required:
	if (isEmpty(strInput))
	{
	     if (!blnRequired) { return true; }
	     else {
	      alert("Please enter your e-mail address.");
	      email.focus();
	      return false;
	     } // else
	    } //if
	
	   var strLen = strInput.length;
	
	   // CHECK IF EMAIL ADDRESS HAS A '@' CHARACTER:
	     if (strInput.indexOf("@") == -1) {
	      alert("The e-mail address you entered was not valid because it does not have a '@'.  Please re-enter your \"e-mail address\".");
	      email.focus();
	      email.select();
	      return false;
	     } //if
	
	   // CHECK IF EMAIL ADDRESS HAS A '.' CHARACTER:
	     	if (strInput.indexOf(".") == -1) {
	       		alert("The e-mail address you entered was not valid because it does not have a '.'.  Please re-enter your \"e-mail address\".");
	          email.focus();
	          email.select();
	          return false;
	     	} //if
	
	   // CHECK IF EMAIL ADDRESS STARTS WITH A '.':
	     	if (strInput.indexOf('.') == 0) {
	       		alert("The e-mail address you entered was not valid because it starts with '.'  Please re-enter your \"e-mail address\".");
	         email.focus();
	         email.select();
	       	 return false;
	     	} //if
	
	   // CHECK IF EMAIL ADDRESS STARTS WITH A '@':
	     	if (strInput.indexOf('@') == 0) {
	       		alert("The e-mail address you entered was not valid because it starts with '@'  Please re-enter your \"e-mail address\".");
	         email.focus();
	         email.select();
	       	 return false;
	     	} //if
	
	   // Check for any invalid characters in the address:
	     	for (var i = 0; i < strLen; i++) {
	         	ch = strInput.charAt(i)
	         	if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z")
	                 	|| (ch == "@") || (ch == ".") || (ch == "_")
	               	  	|| (ch == "-") || (ch >= "0" && ch <= "9")
				|| (ch == ",")) { ; }
	         	else { allValid = false; }
	     	} //for
	
	     	if (!allValid) {
	       		alert("The e-mail address you entered was not valid because it contains an illegal character.  Valid characters are letters, numbers, dashes or underscores.  Please re-enter your \"e-mail address\".");
	         email.focus();
	         email.select();
	       	 return false;
	     	} //if
	
	   //It has a . adjacent to the @:
	         var atpos=strInput.indexOf('@')
	     	if (strInput.charAt(atpos-1)=='.'){
	       		alert("The e-mail address you entered was not valid because it contains a '.' adjacent to the '@'  Please re-enter your \"e-mail address\".");
	         email.focus();
	         email.select();
	       	 return false;
	     	} // if
	
	         var copy=strInput.substring(atpos+1,strInput.length);
	   //It contains more than one @:
	  	/*if (copy.indexOf('@')!=-1){
	       		alert("The e-mail address you entered was not valid because it contains more than one '@'.  Please re-enter your \"e-mail address\".");
	         email.focus();
	         email.select();
	       	 return false;
	     	}*/
	
	   // It has a . adjacent to the @:
	  	if (copy.indexOf(".")<1){
	       		alert("The e-mail address you entered was not valid because it contains a '.' adjacent to the '@'.  Please re-enter your \"e-mail address\".");
	         email.focus();
	         email.select();
	       	 return false;
	     	}
	
	   // It ends with a . or an @:
	  	if (copy.lastIndexOf(".")+1==copy.length){
	       		alert("The e-mail address you entered was not valid because it ends with '.' or an '@'.  Please re-enter your \"e-mail address\".");
	         email.focus();
	         email.select();
	       	 return false;
	     	}
	
	   // Invalid top level domain length:
	  	copy = strInput.substring(strInput.lastIndexOf('.')+1,strLen);
	  	var len = copy.length;
	   	if (len == 2 || len == 3 || len == 4) { ; }
	   	else {
	       	 alert("The e-mail address you entered was not valid since the top-level domain (after the last .) should only be 2 or 3 characters in length.  Please re-enter your \"e-mail address\".");
	          email.focus();
	          email.select();
	       	 return false;
	 	} //else
	
	   // It contains two adjacent .:
	  	copy = strInput;
	  	while (copy.indexOf('.') != -1) {
	         copy = copy.substring(copy.indexOf('.') + 1, copy.length);
	         if (copy.indexOf('.') == 0) {
	       	  alert("The e-mail address you entered was not valid because it contains two adjacent periods.  Please re-enter your \"e-mail address\".");
	          email.focus();
	          email.select();
	       	  return false;
	     	 } //if
	  	} //while
	
	     	return true;
	
	  } //isValidEmailAddress

/*	  
	 function ValidateForm(form) 
	 {
	
	   	// The friend's name is a required field:
	   if (isEmpty(form.toname.value)) {
	   		alert("Please enter your Friend's name.");
	   		form.toname.focus();
	   		return false;
	   }
	   	// The friend's email address is required and must be valid:
	   if (!isValidEmailAddress(form.toemail, true)) {
	       return false;
	   }
	   	// The sender's name is a required field:
	   if (isEmpty(form.fromname.value)) {
	   		alert("Please enter your name.");
	   		form.fromname.focus();
	   		return false;
	   }
	    // The sender's email address is required and must be valid:
	   if (!isValidEmailAddress(form.fromemail, true)) {
	       return false;
	   }
	}
*/
	function PreProcessForm(theForm)
	{
		var isValid = ValidateForm( theForm );
		if ( isValid == false )
			return isValid;

		var isSenderEmailValid = new Boolean(false);
		var isRecipientEmailValid = new Boolean(false);
		var myShowFormElement = null;

		if (theForm != null)
		{
			var myFormElement = null;
			var i=0;
			while ( i < theForm.elements.length && isValid )
			{
				myFormElement = theForm.elements[i];
				if (myFormElement != null)
				{
					myFormElement.name = unescape(myFormElement.name);
					if ( (myFormElement.type == "text") && !isEmpty( myFormElement.value) )
					{
						myFormElement.value = unescape( myFormElement.value );
					}
					else if ( (myFormElement.type == "textarea") && !isEmpty( myFormElement.value) )
					{
						myFormElement.value = unescape( myFormElement.value );
					}
					else if ( (myFormElement.type == "select-one") && (myFormElement.selectedIndex >= 0) )
					{
						myFormElement.value = unescape( myFormElement.options[myFormElement.selectedIndex].value );
					}
					else if ( (myFormElement.type == "select-multiple") && (myFormElement.selectedIndex >= 0) )
					{
						for (var j = 0; j < myFormElement.options.length; j++)
							if ( myFormElement.options[ j ].selected )
								myFormElement.value = unescape( myFormElement.options[ j ].value );
					}
					else if ( (myFormElement.type == "radio") && (myFormElement.selectedIndex >= 0) )
					{
						myFormElement.value = unescape( myFormElement.options[myFormElement.selectedIndex].value );
					}


					if ( myFormElement.name == "senderEmail" && isEmpty( myFormElement.value) )
					{
						isSenderEmailValid = true;

					}
					
					if ( myFormElement.name == "recipientEmail" && isEmpty( myFormElement.value) )
					{
						isRecipientEmailValid = true;
					}
					if ( myFormElement.name == "showForm")
					{
						myShowFormElement = myFormElement;
					}

					
				}
				i++;
			}
			if ( !isSenderEmailValid )
			{
				alert( "Sender Email is missing.");
				isValid = false;
			}
			if ( !isRecipientEmailValid )
			{
				alert( "Recipient Email is missing.");
				isValid = false;
			}

		}

		if ( myShowFormElement != null )
		{
			if ( isValid )
			{
				myShowFormElement.value = "false";
			}
			else
			{
				myShowFormElement.value = "true";
			}
		}
		
		return isValid;
	}


	function ValidateForm(theForm)
	{
		var isValid = new Boolean(true);
		if (theForm != null)
		{
			var myFormElement = null;
			var i=0;
			while ( i < theForm.elements.length && isValid )
			{
				myFormElement = theForm.elements[i];
				if (myFormElement != null)
				{
					if ( !isEmpty(myFormElement.id) && (myFormElement.id.toUpperCase().indexOf("_REQUIRED") >= 0) )
					{
						if ( (myFormElement.type == "text") && isEmpty( myFormElement.value) )
						{
							isValid = false;
						}
						else if ( (myFormElement.type == "textarea") && isEmpty( myFormElement.value) )
						{
							isValid = false;
						}
						else if ( (myFormElement.type == "select-one") && (myFormElement.selectedIndex <= 0) )
						{
							isValid = false;
						}
						else if ( (myFormElement.type == "select-multiple") && (myFormElement.selectedIndex <= 0) )
						{
							isValid = false;
						}
						else if ( (myFormElement.type == "radio") && (myFormElement.selectedIndex <= 0) )
						{
							isValid = false;
						}
					}
					else
					{
					}
					
					if (!isValid)
					{
						var myLabel = myFormElement.name;
						if ( myLabel != null )
						{
							var index = myLabel.indexOf("_");
							if ( index > -1 )
							{
								index = myLabel.indexOf( "_", index+1 );
								if ( index > -1 )
								{
									myLabel = myLabel.substring(index+1);
									if ( myLabel != null )
									{
										myLabel = myLabel.replace("_", " ");
									}
								}
							}
						}
						
						
						alert( myLabel + " is required");
						myFormElement.focus();
					}
				}
				i++;
			}
		}
		
		return isValid;
	}



	var addthis_config =
	{
	   data_track_linkback: true
	}
	
	
	var more_toggle_flag = 0;
	function toggle_more(){
		if(more_toggle_flag == 0){
			document.getElementById("more-menu").style.display = "inline";
			document.getElementById("social-nav").style.height = "70px";
			document.getElementById("more-link").className = "less";
			more_toggle_flag = 1;
		}
		else{
			document.getElementById("more-menu").style.display = "none";
			document.getElementById("social-nav").style.height = "35px";
			document.getElementById("more-link").className = "more";
			more_toggle_flag = 0;
		}
	}






/*

show and hide by date/type.

*/

function showByDate()
{ document.getElementById("infoBlockDate").style.display = "block"
  document.getElementById("infoBlockType").style.display = "none"
  document.getElementById("byDate").style.display = "block"
  document.getElementById("byType").style.display = "none"
}

function showByType()
{ document.getElementById("infoBlockDate").style.display = "none"
  document.getElementById("infoBlockType").style.display = "block"
  document.getElementById("byDate").style.display = "none"
  document.getElementById("byType").style.display = "block"
}



// Clear Cache
function clearPageCache (id, channelId, siteId, server)
{
	var serverArray = parseString(server, ',');
	//for each array element
	for (var i in serverArray)
	{
		url = "'http://" + serverArray[i] + "/vgn-ext-templating/imshealth/jsp/clearpagecache.jsp?vgnContentInstanceId=" + id + "&channelId=" + channelId + "&vgnSiteId=" + siteId + "'";
		if (serverArray[i] == undefined || serverArray[i] == '')
		{
			url = "'/vgn-ext-templating/imshealth/jsp/clearpagecache.jsp?vgnContentInstanceId=" + id + "&channelId=" + channelId + "&vgnSiteId=" + siteId + "'";
		}
		winUrl = eval(url);
		vcmIdStr = eval("'" + id + "'");
		SimplePopwin(winUrl, vcmIdStr, '', '800', '600');
	}
}

function parseString(inputString, delimiter)
{
	var splitArray = inputString.split(delimiter);
	return splitArray;
}


