//Description	: Shared javascript validation
//Last Modified	: S.Wijaya [02/21/2001]
//Main Changes	: Comments


function AddBookMark(url, pagename){
	if (window.external) { 
		window.external.AddFavorite(url,pagename) 
	} else { 
		alert("Sorry! Your browser doesn't support this function."); 
	}
}

function showParticipantDetail(iParticipantID)
{	
	document.mainform.show.value += "|" + iParticipantID + "|";
	document.mainform.mode.value = "show";
	document.mainform.submit();
}

function hideParticipantDetail(iParticipantID)
{
	var startval, valtakeout, beg, end;
		
	startval = document.mainform.show.value;
	valtakeout = "|" + iParticipantID + "|";
		
	if (startval.indexOf(valtakeout) != -1) 
	{
		beg = startval.substring(0, startval.indexOf(valtakeout));
		end = startval.substring(startval.indexOf(valtakeout) + valtakeout.length, startval.length);
		document.mainform.show.value = beg + end;
	}
	document.mainform.mode.value = "hide";
	document.mainform.submit();
}

function IsNumeric(val) {

	var re = /[A-Za-z]+/
	var stringFound = val.match(re);
		
	if (stringFound) 
	{
		return false;
	}
	return true;
}

function IsWholeNumber(val) {
	var re = /[0-9]+/
	var stringFound = val.match(re);
	
	if (val == stringFound) 
	{
		return true;
	}
	return false;
}

function getYear(d) { 
  return (d < 1000) ? d + 1900 : d;
  }


function IsDate(val) {
	var year,month,day;
	
	sDate = val.split("/")
	if (sDate.length != 3) {
		return false;
	}
	
	month = parseInt(sDate[0], 10)
	day = parseInt(sDate[1], 10)
	year = parseInt(sDate[2], 10)
	
	// month argument must be in the range 1 - 12
	month = month - 1;  // javascript month range : 0- 11
	var tempDate = new Date(year,month,day);
	if ( (getYear(tempDate.getYear()) == year) &&
		(month == tempDate.getMonth()) &&
		(day == tempDate.getDate()) )
		return true;
	else
		 return false
}

function IsDateNoYear(val) {
	var year,month,day;
	
	sDate = val.split("/")
	if (sDate.length < 2) {
		return false;
	}
	
	month = parseInt(sDate[0], 10)
	day = parseInt(sDate[1], 10)
	if (sDate.length > 2) {
		year = parseInt(sDate[2], 10)
	} else {
		year = new Date().getYear();
	}
	// month argument must be in the range 1 - 12
	month = month - 1;  // javascript month range : 0- 11
	var tempDate = new Date(year,month,day);
	if ( (getYear(tempDate.getYear()) == year) &&
		(month == tempDate.getMonth()) &&
		(day == tempDate.getDate()) )
		return true;
	else
		 return false
}

function IsZipCodeValid(sZipcode)
{
	var re = new RegExp("^[A-Z a-z-0-9]*$","i");
	var stringFound = sZipcode.match(re);

	//Display error message if the pattern was not found
	if (!stringFound) 
	{
		return false;
	}
	return true;
}

function IsStateValid(sState)
{
	var re = new RegExp("^[A-Z a-z-0-9]*$","i");
	var stringFound = sState.match(re);

	//Display error message if the pattern was not found
	if (!stringFound) 
	{
		return false;
	}
	return true;
}

function IsPhoneValid(sPhone)
{
	var counttemp = sPhone.replace(/[^0-9]+/g,'');
	
	var re = new RegExp("^[0-9( )-]*$","i");
	var stringFound = sPhone.match(re);

	//Display error message if the pattern was not found
	if (!stringFound || (counttemp.length < 10))
	{
		return false;
	}
	return true;
}

function IsEmailValid(sEmail)
{
	var EmailOk  = true;
	var str = new String(sEmail)
	
	if (str.substr(0,1) == '[' && str.substr(str.length-1,1) == ']') {
		return true;
	}
	//var re = new RegExp("^[a-zA-Z0-9]+[a-zA-Z_0-9.]*[@]{1}[a-zA-Z_0-9]+[.]{1}[a-zA-Z_0-9]+[a-zA-Z_0-9.]*[a-zA-Z0-9]{1}$");
	var re = new RegExp("^[a-zA-Z0-9']+([a-zA-Z_0-9.']|(-))*[@]{1}([a-zA-Z_0-9]|(-))+[.]{1}[a-zA-Z_0-9]+([a-zA-Z_0-9.]|(-))*[a-zA-Z0-9]{1}$");
	var stringFound = sEmail.match(re);

	//Display error message if the pattern was not found
	if (!stringFound) {
		EmailOk = false;
	}
	return EmailOk
}

function IsPasswordValid(sPassword)
{
	var bRepeated = true;
	var i;
	var sPrevChar="";
	
	for(i=0;i<sPassword.length;i++)
	{
		if((sPrevChar != "") && (sPrevChar != sPassword.substr(i,1)))
		{
			bRepeated = false;
			break;
		}
		sPrevChar = sPassword.substr(i,1);
	}
	
	if(sPassword.length < 6 || sPassword.length > 20 || bRepeated)
	{
		return false;
	}
	
	var re = new RegExp("^[0-9a-zA-Z]*$","i");
	var stringFound = sPassword.match(re);

	//Display error message if the pattern was not found
	if (!stringFound) 
	{
		return false;
	}

	return true;
}

function cat(ID, Desc, parent) 
{
	this.ID = ID
	this.Desc = Desc
	this.children = new Array()
	this.parent = parent
		
}

function IsValidURL(val)
{
	var re = /http:\/\/|https:\/\//
	var stringFound = val.match(re);
			
	if (!stringFound)
	{
		return false;
	}
					
	return true;
}

function IsValidURL(val)
{
	var re = /http:\/\/|https:\/\//
	var stringFound = val.match(re);
			
	if (!stringFound)
	{
		return false;
	}
					
	return true;
}

function prependDefaultProtocol(val)
{
	var re = /http:\/\/|https:\/\//
	var stringFound = val.match(re);
			
	if (!stringFound)
	{
		return 'http://'+val;
	}
					
	return val;
}

function checkTime(hour1, min1, half1, hour2, min2, half2)
{
	hour1 = parseInt(hour1)
	hour2 = parseInt(hour2)
	if (hour1 == 12) {
		hour1 = 0;
	}
	if (hour2 == 12) {
		hour2 = 0;
	}
	
	//if midnight then OK
	//alert(hour2 + "*" + min2 + "*" + half2);
	if (hour2 == 0 && min2 == ":00" && half2 == " AM") {
		return true;
	}
	if (half2 == " AM" && half1 == " PM") {
		return false;
	} else if (half1 == " AM" && half2 == " PM") {
		return true;
	}
	if (hour2 < hour1) {
		return false;
	} else {
		return true;
	}
	return true;		
}

// whitespace characters
var whitespace = " \t\n\r";

/****************************************************************/

// Check whether string s is empty.
function isEmpty(s)
{ return ((s == null) || (s.length == 0)) }

/****************************************************************/

function isWhitespace (s)
{
     var i;

     // Is s empty?
     if (isEmpty(s)) return true;

     // Search through string's characters one by one
     // until we find a non-whitespace character.
     // When we do, return false; if we don't, return true.

     for (i = 0; i < s.length; i++)
     {
          // Check that current character isn't whitespace.
          var c = s.charAt(i);

          if (whitespace.indexOf(c) == -1) return false;
     }

     // All characters are whitespace.
     return true;
}

function validateFileExtension(fileName,extensions,allowBookmarks)
{
	var extarray;
	var fileext;
	var validext;
	var periodloc;
	var poundloc;
	validext = false;
	extensions = extensions.toUpperCase();
	extarray=extensions.split(",");
	if(fileName != "")
	{
		for (var i = 0;i < extarray.length; i++)
		{
			extlength = extarray[i].length;
			fileext = fileName.toUpperCase();
			periodloc = fileName.lastIndexOf('.');
			poundloc = fileName.lastIndexOf('#');
			if(!allowBookmarks && poundloc > periodloc)
			{
				return false;
			}
			
			if(poundloc==-1 || poundloc < periodloc){
				fileext = fileext.substr(periodloc,fileext.length - periodloc + 1);
			}else{
				fileext = fileext.substr(periodloc,poundloc - periodloc);
			}
			//alert(fileext);
			if(fileext == extarray[i])
			{
				return true;
			}
		}
		validext = false;
	}
	else
	{
		validext = true;
	}
	return validext;
}

function leftTrimOld(passString)
{
	var sString = new String(passString);
	if (sString.length > 0)
	{
		while (sString.substring(0,1) == ' ')
		{
			sString = sString.substring(1, sString.length);
		}
	}
	return sString;
}

function rightTrimOld(passString)
{
	var sString = new String(passString);
	if (sString.length > 0)
	{
		while (sString.substring(sString.length-1, sString.length) == ' ')
		{
			sString = sString.substring(0,sString.length-1);
		}
	}
	return sString;
}

function trimAllOld(passString)
{
	var sString = new String(passString);
	if (sString.length > 0)
	{
		while (sString.substring(0,1) == ' ')
		{
			sString = sString.substring(1, sString.length);
		}
		while (sString.substring(sString.length-1, sString.length) == ' ')
		{
			sString = sString.substring(0,sString.length-1);
		}
	}
	return sString;
}

function trimAll(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function leftTrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rightTrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function ShowPopup(popupname)
{
	var obj = document.all[popupname];
    /* get the mouse left position */
    /* x = event.clientX + document.body.scrollLeft; */
    /* get the mouse top position  */
    /* y = event.clientY + document.body.scrollTop + 35; */
    /* display the pop-up */
    obj.style.display="block";
    /* set the pop-up's left */
    /* obj.style.left = 285; */
    /* set the pop-up's top */
    /* obj.style.top = y; */
}

/* this function hides the pop-up when user moves the mouse out of the link */
function HidePopup(popupname)
{
	var obj = document.all[popupname];
    /* hide the pop-up */
    obj.style.display="none";
}

/* this function edits a value for a valid date and then reformats it in mm/dd/yyyy format */
function FormatDate_MMDDYYYY(val)
{
	var year,month,day;
	var newval;
	
	sDate = trimAll(val);
	sDate = sDate.split("/");
	if (sDate.length != 3) {
		val="";
		return val;
	}
	
	month = parseInt(sDate[0], 10)
	day = parseInt(sDate[1], 10)
	year = parseInt(sDate[2], 10)
	
	//alert(month);
	if(month < 10)
		newval = "0" + month + "/";
	else
		newval = month + "/";
	//alert(day);
	if(day < 10)
		newval = newval + "0" + day + "/";
	else
		newval = newval + day + "/";
	//alert(year);
	if(sDate[2].length == 2)
		newval = newval + "20" + sDate[2];
	if(sDate[2].length == 4)
		newval = newval + sDate[2];
		
	if (!IsDate(newval))
	{
		val = "";
		return val;
	}
		
	val = newval;
	return val;
}

function IsValidTime(timeFld)
{
	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds and AM/PM are optional.

	var timePat = /^(\d{1,2}):(\d{2}):((\d{2}))?(\s?(AM|am|PM|pm))?$/;
	var timeStr = document.mainform[timeFld].value;
			
	var startidx = timeStr.indexOf(':');
	if(startidx != -1) 
	{
		if(timeStr.indexOf(':',startidx + 1) == -1)
		{
			// add default of 00 for seconds
			var temptime = timeStr.substr(0,startidx + 3) + ":00" + timeStr.substr(startidx + 3);
			document.mainform[timeFld].value = temptime;
			timeStr = temptime;
		}
	}
			
	var matchArray = timeStr.match(timePat);
	//alert(matchArray);
	if (matchArray == null)
	{
		alert("Time is not in a valid format.  Valid standard time formats are: (hh:mm AM/PM, hh:mm:ss AM/PM).  Valid military time formats are: (hh:mm, hh:mm:ss)");
		return false;
	}
	
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[3];
	ampm = matchArray[6];

	if (second=="") { second = null; }
	if (ampm=="") { ampm = null; }

	if (hour < 0  || hour > 23)
	{
		alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
		return false;
	}	
	
	if (hour <= 12 && ampm == null)
	{
		if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time"))
		{
		alert("You must specify AM or PM.");
		return false;
		}
	}
	
	if  (hour > 12 && ampm != null)
	{
		alert("You can't specify AM or PM for military time.");
		return false;
	}
	
	if (minute < 0 || minute > 59)
	{
		alert ("Minute must be between 0 and 59.");
		return false;
	}
	
	if (second != null && (second < 0 || second > 59))
	{
		alert ("Second must be between 0 and 59.");
		return false;
	}
			
	return true;
}

//Dummy function called to set extra style properties, if needed
SetShowSettings=function(myItem)
{ };

function showHideItems(myItem, myButton)
{
	//function to display or hide a given element

	//this is the ID of the hidden item
	var myItem = document.getElementById(myItem);

	//this is the ID of the plus/minus button image
	var myButton = document.getElementById(myButton);

	if (myItem.style.display != "none")
	{
		//items are currently displayed, so hide them
		myItem.style.display = "none";
		swapImage(myButton,"plus");
	}
	else
	{
		//items are currently hidden, so display them
		myItem.style.display = "block";
		setShowSettings(myItem);
		swapImage(myButton,"minus");
	}
}

function swapImage(myImage, state)
{
	//function to swap an image based on its current state

	if (state == "minus")
	{
		myImage.src = "../images/minus.gif";
	}
	else
	{
		myImage.src = "../images/plus.gif";
	}
}





