/***************************************************************************
	client javascript functions for ASHM
****************************************************************************/

/***************************************************************************
	toggle a div
****************************************************************************/
function ToggleDiv(divID, ShowHide)
{

	if (document.getElementById(divID))
	{
		if (ShowHide == 0)
		{
			document.getElementById(divID).style.display="none";
		}
		else
		{
			document.getElementById(divID).style.display="block";
		}	
	}
}

/*****************************************************************************
* function to open a new window
******************************************************************************/
function openPopupWindow(windowURL, windowName, xLeft, yTop, xWidth, xHeight)
{
	var win=window.open(windowURL, windowName, "left="+xLeft+",top="+yTop+",width="+xWidth+",height="+xHeight+",status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
	win.focus();  
}

/**************************************************************************
* function to submit the resource order
**************************************************************************/
function SubmitOrder()
{
	document.getElementById("ResourceList").submit();
}

/**************************************************************************
* show/hide sub areas of interest div
**************************************************************************/
function ShowSubDiv(DivID)
{
	//alert('inside function')
	if (document.getElementById(DivID))
	{
		//alert('inside div')
		if (document.getElementById(DivID).style.display=="none")
		{
			//alert('none')
			document.getElementById(DivID).style.display="";
			//alert('show')
		}
		else
		{		
			document.getElementById(DivID).style.display="none";
		}
	}
}
/************************************************************************
* function for mouse over and out on course list
************************************************************************/
function RowOver(RowID)
{
	if (document.getElementById(RowID))
	{
		document.getElementById(RowID).className="CourseRowOver";
	}
}

function RowOut(RowID, DivID)
{
	if (document.getElementById(RowID))
	{
		if (document.getElementById(DivID))
		{
			if(document.getElementById(DivID).style.display=="none")
			{
				document.getElementById(RowID).className="CourseRow";
			}
			else
			{
				document.getElementById(RowID).className="CourseRowOver";
			}
		}
	}
}


function ClickRow(DivID, RowID)
{
	if (document.getElementById(DivID))
	{
		if(document.getElementById(DivID).style.display=="none")
		{
			document.getElementById(DivID).style.display = "";
			if (document.getElementById(RowID))
			{
				document.getElementById(RowID).className="CourseRowOver";
			}
		}
		else
		{
			document.getElementById(DivID).style.display = "none";
			if (document.getElementById(RowID))
			{
				document.getElementById(RowID).className="CourseRow";
			}
		}
	}
}

/************************************************************************
* function for mouse over and out on area of interest
************************************************************************/
function GroupOver(GroupID)
{
	if (document.getElementById(GroupID))
	{
		document.getElementById(GroupID).className="MailinglistTitleOver";
	}
}

function GroupOut(GroupID, DivID)
{
	if (document.getElementById(GroupID))
	{
		if (document.getElementById(DivID))
		{
			if(document.getElementById(DivID).style.display=="none")
			{
				document.getElementById(GroupID).className="MailinglistTitle";
			}
			else
			{
				document.getElementById(GroupID).className="MailinglistTitleOver";
			}
		}
	}
}


function ClickGroup(DivID, GroupID)
{
	if (document.getElementById(DivID))
	{
		if(document.getElementById(DivID).style.display=="none")
		{
			document.getElementById(DivID).style.display = "";
			if (document.getElementById(GroupID))
			{
				document.getElementById(GroupID).className="MailinglistTitleOver";
			}
		}
		else
		{
			document.getElementById(DivID).style.display = "none";
			if (document.getElementById(GroupID))
			{
				document.getElementById(GroupID).className="MailinglistTitle";
			}
		}
	}
}

/************************************************************************
* function for mouse over and out on order list
************************************************************************/
function OrderRowOver(RowID, BackOrderMessageID)
{
	if (document.getElementById(RowID))
	{
		document.getElementById(RowID).className="ResourcesListRowOver";
		/*if (document.getElementById(BackOrderMessageID))
		{
			document.getElementById(BackOrderMessageID).style.display="";
		}*/
	}
}

function OrderRowOut(RowID, BackOrderMessageID)
{
	if (document.getElementById(RowID))
	{		
		if (document.getElementById(BackOrderMessageID))
		{	
			if (document.getElementById(BackOrderMessageID).style.display=="none")
			{
				document.getElementById(RowID).className="ResourcesListRow";
			}
			else
			{
				document.getElementById(RowID).className="ResourcesListRowOver";
			}
		}
		else
		{
			document.getElementById(RowID).className="ResourcesListRow";
		}
	}
}

function ClickOrderRow(RowID, BackOrderMessageID)
{
	if (document.getElementById(BackOrderMessageID))
	{
		if (document.getElementById(BackOrderMessageID).style.display == "none")
		{
			document.getElementById(BackOrderMessageID).style.display="";
			document.getElementById(RowID).className="ResourcesListRowOver";
		}
		else
		{
			document.getElementById(BackOrderMessageID).style.display="none";
			document.getElementById(RowID).className="ResourcesListRow";
		}
	}
}

/***********************************************
* Enable input box when checked
***********************************************/
function EnableInput(TheCheckBox, TheInputBoxID)
{
	if (TheCheckBox.checked==true)
	{
		document.getElementById(TheInputBoxID).readOnly = false;
	}
	else
	{
		document.getElementById(TheInputBoxID).readOnly = true;
	}
}

/************************************************
* function to validate donation form
***********************************************/
function ValidateDonation(FormID)
{
	var ErrorCount = 0;

	/* check if invalid international donation amount */
	if (document.getElementById('RegisterCourseInterCheck').checked == true)
	{
		if(CheckInputBox(true, 'RegisterCourseInterAmount', 'Invalid Donation Amount')==false)
		{
			ErrorCount = ErrorCount + 1;
		}
	}
	
	/* check if invalid domestic donation amount */
	if (document.getElementById('RegisterCourseDomesCheck').checked == true)
	{
		if(CheckInputBox(true, 'RegisterCourseDomesAmount', 'Invalid Donation Amount')==false)
		{
			ErrorCount = ErrorCount + 1;
		}
	}

	/* check if no donation checked */
	if (document.getElementById('RegisterCourseInterCheck').checked == false && document.getElementById('RegisterCourseDomesCheck').checked == false)
	{
		ErrorCount = ErrorCount + 1;
		alert('At lease one donation needs to be selected');
	}
	
	/* check first name */
	if(CheckInputBox(false, 'FirstName', 'Invalid First Name')==false)
	{
		ErrorCount = ErrorCount + 1;	
	}
	
	/* check last name */
	if(CheckInputBox(false, 'LastName', 'Invalid Last Name')==false)
	{
		ErrorCount = ErrorCount + 1;
	}
	
	/* check email */
	if(CheckEmail('Email')==false)
	{
		ErrorCount = ErrorCount + 1;
	}

	//alert(ErrorCount);

	/* no error */
	if (ErrorCount == 0)
	{
		document.getElementById(FormID).submit();
	}
}

/************************************************************
 validate input box
 NumericValue is boolean to check numeric or not
*************************************************************/
function CheckInputBox(NumericValue, InputBoxID, ErrorMessage)
{
	if (document.getElementById(InputBoxID))
	{
		if (document.getElementById(InputBoxID).value == "")
		{
			alert(ErrorMessage);
			return false;
		}
		else
		{
			if (NumericValue)
			{
				if (isNaN(document.getElementById(InputBoxID).value))
				{
					alert(ErrorMessage);
					return false;
				}
				else
				{
					if (parseFloat(document.getElementById(InputBoxID).value) <=0)
					{
						alert(ErrorMessage);
						return false;
					}
					else
					{
						return true;
					}
				}
			}
			else
			{
				return true;
			}
		}
	}
}

/************************************************************
 validate email
*************************************************************/
function CheckEmail(EmailID)
{
	if (document.getElementById(EmailID))
	{
		if (document.getElementById(EmailID).value == "")
		{
			alert("Invalid Email");
			return false;
		}
		else
		{
			var emailfmt= /^\w+([.-]\w+)*@\w+([.-]\w+)*\.\w{2,8}$/;
			if(!emailfmt.test(document.getElementById(EmailID).value))
			{ 
					alert("Invalid Email");
					return false;
			}
			else
			{
				return true;
			}
		}
	}
}


/*************************************************************************************************
	Remember me functions
**************************************************************************************************/
function newCookie(name,value,days) {
 var days = 10000;   // the number at the left reflects the number of days for the cookie to last
                 // modify it according to your needs
 if (days) {
   var date = new Date();
   date.setTime(date.getTime()+(days*24*60*60*1000));
   var expires = "; expires="+date.toGMTString(); }
   else var expires = "";
   document.cookie = name+"="+value+expires+"; path=/"; }

function readCookie(name) 
{
   var nameSG = name + "=";
   var nuller = '';
   var CookieValue
   if (document.cookie.indexOf(nameSG) == -1)
   return nuller;

	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) 
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameSG) == 0) 
		{
			CookieValue = c.substring(nameSG.length,c.length);
			CookieValue = CookieValue.replace(/%40/g, '@');
			CookieValue = CookieValue.replace(/%2E/g, '.');
			CookieValue = CookieValue.replace(/%5F/g, '_');
			
			return CookieValue; 
		}
	}
		return null; 
}

function eraseCookie(name) {
  newCookie(name,"",1); }

function toMem(a) {
    newCookie('UsernameCookie', document.getElementById('Login').value);     // add a new cookie as shown at left for every
    newCookie('PasswordCookie', document.getElementById('Password').value);   // field you wish to have the script remember
}

function delMem(a) {
  eraseCookie('UsernameCookie');   // make sure to add the eraseCookie function for every field
  eraseCookie('PasswordCookie');

   document.getElementById('Login').value = '';   // add a line for every field
   document.getElementById('Password').value = ''; }

function LoadLoginDetails()
{	
	if (document.getElementById('Login'))
	{
		document.getElementById('Login').value = readCookie("UsernameCookie"); 
	}

	if (document.getElementById('Password'))
	{
		 // Change the names of the fields at right to match the ones in your form.
		document.getElementById('Password').value = readCookie("PasswordCookie");
	}
}
