
//
//	Group of functions for moving list items
//	based on its index
//
function moveListItemUp( listName, itemIndex )
{
	swapListItems( listName, itemIndex, (itemIndex - 1));
}

function moveListItemDown( listName, itemIndex )
{
	swapListItems( listName, itemIndex, (itemIndex + 1));
}

function swapListItems( listName, srcIndex, trgIndex )
{
	if( (srcIndex >= 0) && (srcIndex < (listName.length)) &&
		(trgIndex >= 0) && (trgIndex < (listName.length)) &&
		(srcIndex != trgIndex) )
	{
		var optValue = listName.options[srcIndex].value;
		var optText  = listName.options[srcIndex].text;
		var optDef	 = listName.options[srcIndex].defaultselected;
		var optSel	 = listName.options[srcIndex].selected
		listName.options[srcIndex].text = listName.options[trgIndex].text;
		listName.options[srcIndex].value = listName.options[trgIndex].value;
		listName.options[srcIndex].defaultSelected = listName.options[trgIndex].defaultSelected;
		listName.options[srcIndex].selected = listName.options[trgIndex].selected;
		listName.options[trgIndex].text = optText;
		listName.options[trgIndex].value = optValue;
		listName.options[trgIndex].defaultSelected = optDef;
		listName.options[trgIndex].selected = optSel;
	}
}



//
//	Finds a list item based on its value,
//	can be full word or variable expression.
//	Returns -1 if item not found
//
function getListItemIndex( listName, itemValue, fullWord )
{
	if( fullWord == true )
	{
		for( var i = 0;  i < listName.length;  i++ )
			if( listName.options[i].value == itemValue )
				return i;
	}
	else
	{
		for( var i = 0;  i < listName.length;  i++ )
			if( listName.options[i].value.indexOf(itemValue) != -1 )
				return i;
	}
	return -1;
}


//
//	tests if the list has an valid option value 
//	currently selected
//
function hasValidListValue( listName )
{
	return (trim(getCurrentListValue(listName)) == "") ? false : true;
}



//
//	validates the list and optionally displaying a message
//	and setting the focus
//
function validateList( listName, errorMsg )
{
	if( !hasValidListValue(listName) )
	{
		if( errorMsg != null )
		{
			alert( errorMsg );
			listName.focus();
		}
		return false;
	}
	return true;
}



//
//	get the current option value in a list
//	obs.:
//		returns empty string if no option is selected
//
function getCurrentListValue( listName )
{
	var selIndex = listName.selectedIndex;
	if( selIndex != -1 )
		return listName.options[selIndex].value;
	else
		return "";
}

//
//	get the current option text in a list
//	obs.:
//		returns empty string if no option is selected
//
function getCurrentListText( listName )
{
	var selIndex = listName.selectedIndex;
	if( selIndex != -1 )
		return listName.options[selIndex].text;
	else
		return "";
}


//
//	Selects a list item by its value
//	use:
//		selectListItem( form1.myComboBox, optionValue, true )
//
function selectListItem( listName, itemValue, deselectOthers )
{
	for( var i=0;  i < listName.options.length;  i++ )
		if( listName.options[i].value == itemValue )
		{
			listName.selectedIndex = i;
			listName.options[i].selected = true;
			if( deselectOthers == false ) break;
		}
		else
		{
			if( deselectOthers == true )
				listName.options[i].selected = false;
		}
}


//
//	Indicates if a string is already an option of a list item
//	
//	use: isListItem( listName, searchString, isText )
//			isText = true if searching option text
//			isText = false if searching option value (default)
//
//	Example: isListItem( form1.city, "Los Angeles", true )
//
function isListItem( listName, searchString, isText )
{
	((isText) ? isText : false)
	var returnVal = false;
	for( var i=0;  i < listName.options.length;  i++ ){
		if( isText ){
			if( listName.options[i].text == searchString ){
				returnVal = true;
				break;
			}
		} else {
			if( listName.options[i].value == searchString ){
				returnVal = true;
				break;
			}
		}
	}
	return returnVal;
}



//
//	String trimming
//  use:
//		trimmedString = trim( originalString );
//	obs.:
//		Trims both leading and trailing spaces;
//		Returns an empty string if originalString contains 
//		only spaces or is null
//		
function trim( strValue )
{
	if( strValue != null )
		for( var start = 0;  start < strValue.length;  start++ )
			if( strValue.charAt(start) != ' ' )
				for( var end = strValue.length - 1;  end >= start;  end-- )
					if( strValue.charAt(end) != ' ' )
						return strValue.substr( start, end - start + 1);
	return "";
}



//
//	Date validation 
//	from: http://javascript.internet.com/forms/
//
//	use: isValidDate( value, format )
//	Example: isValidDate( f.datecreate.value, DATE_MMDDYYYY )
//
//	Formats:
var DATE_MMDDYY		= 0;
var DATE_MMDDYYYY	= 1;

function isValidDate( dateStr, fmtIndex ) 
{
	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	// Also separates date into month, day, and year variables
	
	//var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	
	// To require a 4 digit year entry, use this line instead:
	//var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
	
	var dateFmt = new Array();
	dateFmt[ DATE_MMDDYY ]		= /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	dateFmt[ DATE_MMDDYYYY ]	= /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
	
	var datePat = dateFmt[ fmtIndex ];
	
	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null)
	{
		alert("Date is not in a valid format.")
		return false;
	}
	
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];
	
	if (month < 1 || month > 12)  // check month range
	{
		alert("Month must be between 1 and 12.");
		return false;
	}
	
	if (day < 1 || day > 31) 
	{
		alert("Day must be between 1 and 31.");
		return false;
	}
	
	if ((month==4 || month==6 || month==9 || month==11) && day==31) 
	{
		alert("Month "+month+" doesn't have 31 days!")
		return false
	}
	
	if (month == 2)  // check for february 29th
	{
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) 
		{
			alert("February " + year + " doesn't have " + day + " days!");
			return false;
		}
	}
	
	return true;  // date is valid
}



//
//	Limit Text Area
//
//	use: checkchars( textAreaObject, maximumCharactersAllowed )
//
//	Example: checkchars(this.form.textAreaName, 2000)
//
function checkchars(field, maxlimit)
{
	if (field.value.length >= maxlimit)
	{
		alert("Please do not enter any more information within this field.  " +
				"All further input will be ignored during save process.");
		field.blur();
	}
}



//
//	Ignore all spaces within a string
//
//	use: ingnoreSpaces( oringinalString )
//
//	Example: ignoreSpaces(this.value)
//
function ignoreSpaces(string)
{
	var temp = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
		temp += splitstring[i];
	return temp;
}



//
//	Converts all single quotes(') to two single quotes('') for use in SQL statement
//
//	use: catchSQuote( oringinalString )
//
//	Example: catchSQuote(this.value)
//
function catchSQuote(string)
{
	return replacePhrase(string, "'", "\''");
}



//
//	Checks all text input of a specified form and
//		converts single quotes(') to double single quotes('')
//		alerts invalid use of characters " and \
//
//	use: catchInvalidChars( formObject )
//
//	Example: catchInvalidChars(document.form1)
//
function catchInvalidChars(form)
{
	var thisValue;
	var thisType;
	for( var i=0; i < form.elements.length; i++)
	{
		thisValue = form.elements[i].value;
		thisType = form.elements[i].type;
		if( thisType=="text" )
		{
			if( catchPhrase(thisValue, '"') ||
				catchPhrase(thisValue, '\\') )
			{
				alert( 'Invalid character found.  Do not use " or \\' +
					'\r\n(' + form.elements[i].name + ')\r\n');
				return false;
			}
		}
	}
	for( var i=0; i < form.elements.length; i++)
	{
		form.elements[i].value = catchSQuote(form.elements[i].value);
	}
	return true;
}



//
//	Converts all instances of a phrase with another phrase
//
//	use: replacePhrase( originalString, originalPhrase, replacementPhrase  )
//
//	Example: replacePhrase(this.value, "'", "\''")
//
function replacePhrase(string, phrase1, phrase2)
{
	string = '' + string;
	splitstring = string.split(phrase1);
	var temp = splitstring[0];
	for(i = 1; i < splitstring.length; i++)
		temp += phrase2 + splitstring[i];
	return temp;
}



//
//	Returns true when specified string is found else returns false.
//
//	use: catchPhrase( oringinalString, catchString)
//
//	Example: catchPhrase(this.value, "help me")
//
function catchPhrase(string, catchString)
{
	string = '' + string;
	splitstring = string.split(catchString);
	if(splitstring.length > 1) return true;
	else return false;
}



//
//	The following 2 functions (URLReturnBack & startURLWin) are needed
//		for use of the dirselector.jsp which allows for browsing of 
//		directories and selection of files.
//

	//  declaration of shared variables
	var urlpopwin = "";
	var urlField = "";

	//	this function is used to call dirselector.jsp and open it from
	//	within a child window
	function startURLWin( field, filter, basePath, booleanReturnFolderName )
	{
		urlField = field;
		if( booleanReturnFolderName){
			urlpopwin=open("http://nttest.virtualeducation.org:8080/jagermeister/jsp/dirmakerdo.jsp?" +
			"filter=" + escape(filter) + "&Path=" + escape(basePath) + "&ShowFullPath=0",
			"urlspage","scrollbars=yes,width=450,height=400");
		}
		else {
			urlpopwin=open("http://nttest.virtualeducation.org:8080/jagermeister/jsp/dirselector.jsp?" +
			"filter=" + escape(filter) + "&Path=" + escape(basePath) + "&ShowFullPath=0",
			"urlspage","scrollbars=yes,width=450,height=400");
		}
	}

	//	this function is called by dirselector.jsp to return the
	//	selected value and close the child window opened by startURLWin
	function URLReturnBack( data )
	{
		urlField.value = data;
		urlpopwin.close();
	}
	
	//The next three functions move all selected items from one list to another
	sortitems = 0;  // Automatically sort items within lists? (1 or 0)
	//fbox = list options are from 
	//tbox = list options are going to
	function move(fbox,tbox) {
		for(var i=0; i<fbox.options.length; i++) {
			if(fbox.options[i].selected && fbox.options[i].value != "") {
				var no = new Option();
					no.value = fbox.options[i].value;
					no.text = fbox.options[i].text;
					tbox.options[tbox.options.length] = no;
					fbox.options[i].value = "";
					fbox.options[i].text = "";
		   }
		}
		BumpUp(fbox);
	if (sortitems) SortD(tbox);
	}
	function BumpUp(box)  {
		for(var i=0; i<box.options.length; i++) {
			if(box.options[i].value == "")  {
				for(var j=i; j<box.options.length-1; j++)  {
					box.options[j].value = box.options[j+1].value;
					box.options[j].text = box.options[j+1].text;
				}
				var ln = i;
				break;
		   }
		}
		if(ln < box.options.length)  {
			box.options.length -= 1;
			BumpUp(box);
   		}	
	}
	function SortD(box)  {
		var temp_opts = new Array();
		var temp = new Object();
		for(var i=0; i<box.options.length; i++)  {
			temp_opts[i] = box.options[i];
		}
		for(var x=0; x<temp_opts.length-1; x++)  {
			for(var y=(x+1); y<temp_opts.length; y++)  {
				if(temp_opts[x].text > temp_opts[y].text)  {
					temp = temp_opts[x].text;
					temp_opts[x].text = temp_opts[y].text;
					temp_opts[y].text = temp;
			    }
			}
		}
		for(var i=0; i<box.options.length; i++)  {
			box.options[i].value = temp_opts[i].value;
			box.options[i].text = temp_opts[i].text;
		}
	}



function submitIt(AAASubscriptionForm) {
	//CC or Cash
		payemntOption = -1
			for (i=0; i<AAASubscriptionForm.radioPayment.length; i++) {
				if (AAASubscriptionForm.radioPayment[i].checked) {
					payemntOption = i
				}
			}
			if (payemntOption == -1) {
				alert("Please check method of payment.")
				return false
			}
			
	//country check
	countryChoice = AAASubscriptionForm.countrymenu.selectedIndex
			if (AAASubscriptionForm.countrymenu.options[countryChoice].value == "") {
				alert("Please select a country")
				return false
			}
	//other country check 
	
	if((AAASubscriptionForm.countrymenu.options[countryChoice].value == "Other" )&&(AAASubscriptionForm.boxOther.value == "" )){
		alert("Please specify country")
				return false
	
		}
	
	// If we made it to here, every	thing's valid, so return true	
	return true
}	

function launch() { 

        gallery=window.open
        ("s_h.html","gallery",
        'toolbar=no,menubar=yes,scrolling=no,scrollbars=yes,resizable=no,width=230,height=320')

        window.gallery.focus()

}



