function placeFocus() {
	if (document.forms.length > 0) {
		var field = document.forms[0];
		for (i = 0; i < field.length; i++) {
			if (field.elements[i].type != null){
				if ((field.elements[i].type == "text") || 
					(field.elements[i].type == "textarea") || 
					(field.elements[i].type.toString().charAt(0) == "s")) {
						document.forms[0].elements[i].focus();
					break;
				}
			}
		}
	}
}

function getCookieVal(offset) {
 var endstr = document.cookie.indexOf(";", offset);
 if (endstr == -1) endstr = document.cookie.length;
 return unescape(document.cookie.substring(offset, endstr));
} 

function getCookie(name) {
 var cookie = document.cookie;
 var arg = name + "=";
 var alen = arg.length;
 var clen = cookie.length;
 var i = 0;
 while (i < clen) {
	 var j = i + alen;
 	if (cookie.substring(i, j) == arg)
 	return getCookieVal (j);

 	i = cookie.indexOf(" ", i) + 1;
 	if (i == 0) break; 
 }
 return null;
}


/**
* This is the basic calendar open function for a single calender page
* @param name				: Target object name
* @param calendarURL		: Calendar URL
*/
function openSingleCalendar(name, calendarURL) {

	var monthsToDisplay = 1;
	var defaultSize = 300;

	var width = defaultSize;
	var height = defaultSize;

	openMultiCalendar(name, calendarURL, monthsToDisplay, height, width);
}

/**
* This is the detail calendar open function 
* @param name				: Target object name
* @param calendarURL		: Calendar URL
* @param monthsToDisplay   	: Number of months to display
* @param height   			: Windows height
* @param width   			: Windows width
*/

function openMultiCalendar(name, calendarURL, monthsToDisplay, height, width) {

	var target = null;
	var ref = null;

	//check pop-up window size		
	var maxHeight = window.screen.availHeight;
	var maxWidth = window.screen.availWidth;
	
	if(height > maxHeight)
		height = maxHeight;

	if(width > maxWidth)
		width = maxWidth;
		
	
	//check if the specified element is valid
	var element = null;
	var elementName = name;
	var elementFound = false;
	
	
	var position = name.indexOf('[');
	if(position > 0) 
		elementName = name.substring(0,position);
		
	for(i=0;i<document.forms.length && elementFound == false;i++) {
	
		for(j=0;j<document.forms[i].elements.length;j++) {
		
			element = document.forms[i].elements[j];
		
			if(element.name == elementName) {
				
				elementFound = true;
				
				target = document.forms[i].name + "." + name;
				ref = eval("document." + target);
		
				if(ref != null) {
					// in case the passed calenderURL already contains some paramters attached to the
					// URL with an "?" -> change the delimeter to "&" and proceed with building of the 
					// URL
					var pos = calendarURL.indexOf('?');
					var delimeter = "?";
					if(pos > 0) {
						delimeter = "&";
					}
				
					var location = calendarURL + delimeter + 'field=' + target + '&monthsToDisplay=' + monthsToDisplay + '&specifiedDate=' + ref.value + '&dateNotSelectableBefore=0001-01-01&dateNotSelectableAfter=9999-12-31';
					var opener = window.open(location ,'Calendar','width=' + width +',height=' + height + ',resizable=yes,toolbar=no,location=no,menubar=no,alwaysRaised=yes');

					opener.focus();
				}
		
			 	break;
			}
		}
	}
}

// Loop through to convert + to %20 because the java.net.URLEncoder.encode converts spaces to + 
// and javascript does not recognise + (expects %20).  
// This incompatibility may be resolved in a later version of JavaScript

function tmsunescape (str)
{
	while (true){
		var i = str . indexOf ('+');
		if (i < 0)
			break;
		str = str . substring (0, i) + '%20' +
			str . substring (i + 1, str . length);
	}

	// now decode the string using the standard JavaScript function
	return unescape (str);
}


// Opens the code list page.
//
// @param formName the form name of the parent window where the element exists.
// @param fieldName the field name that will be use to place the selected result. 
// @param isFieldGroup 1 = true 0 = false.
// 		a flag used by the child window to determine if the selection will return a result to a
//      field that is part of a group i.e a form has been defined with more than one
//		field that has the same name. (eg Advanced search).
// @param fieldGroupIndex in the event that isFieldGroup=true this index will be used to 
//      find the field within the group to place the result in.
// @param codeListURL the URL of to call the code list page.
// @param app the application.
// @param key the key.
// @param codeStart the code start.
// @param codeLength the code length.      
// @param descStart the description start.
// @param descLength the description length.
//
function openCodeListWindow(formName, fieldName, isFieldGroup, fieldGroupIndex, codeListURL, app, key, codeStart, codeLength, descStart, descLength)
{
	// contruct the url
	var queryString = "formName=" + formName  + "&fieldName=" + fieldName + "&isFieldGroup=" + isFieldGroup + "&fieldGroupIndex=" + fieldGroupIndex + "&xactdApplication=" + app + "&xactdKey=" + key + "&codeStart=" + codeStart + "&codeLength=" + codeLength;
	queryString  = queryString + "&direction=1&descStart=" + descStart + "&descLength=" + descLength;

	var pos = codeListURL.indexOf('?');
	var delimeter = "?";
	// if the codeListURL already has ? in it, change the delimeter to &
	if(pos > 0) {
		delimeter = "&";
	}

	var url = codeListURL + delimeter + queryString;
	
	// call the child window
	
	// Note: we can not use the function showModalDialog or showModeless as any
	// links from the popup will always open in a new window not
	// in the same window!!!!!!
	
	window.open(url,'CodeList','width=600,height=500,scrollbars=1,menubar=0,toolbar=0,resizable=1');
	
}



// Function takes over the values for the delivery address from the billing address.
// After the values have been put in the delivery address the fields are disabled
function sameAsBillingFlagClicked(form, oCheck){

	//TODO JCH - this is very weak to hard code the names of the fields here in the method. Very error proun.
	if (oCheck.checked){ 
		// copy the info
		form.A$DCNM.value = form.C$REGN.value
		form.A$DAD1.value = form.C$PAD1.value
		form.A$DAD2.value = form.C$PAD2.value
		form.A$DAD3.value = form.C$PAD3.value
		//form.A$DAD4.value = form.C$PAD4.value
		form.city.value = form.city2.value
		form.state.value = form.state2.value
		form.A$DAD5.value = form.C$PAD5.value
		form.A$DAD6.value = form.C$PAD6.value

		// disable the fields		
		form.A$DCNM.disabled = true; 
		form.A$DAD1.disabled = true; 
		form.A$DAD2.disabled = true; 	
		form.A$DAD3.disabled = true; 
		//form.A$DAD4.disabled = true; 
		form.city.disabled = true;
		form.state.disabled = true;
		form.A$DAD5.disabled = true; 
		form.A$DAD6.disabled = true; 
	}else{
		// enable the fields
		form.A$DCNM.disabled = false; 
		form.A$DAD1.disabled = false; 
		form.A$DAD2.disabled = false; 	
		form.A$DAD3.disabled = false; 
		//form.A$DAD4.disabled = false;
		form.city.disabled = false;
		form.state.disabled = false; 
		form.A$DAD5.disabled = false; 
		form.A$DAD6.disabled = false; 
	}
}


var isIE=document.all?true:false; 
var isDOM=document.getElementById?true:false; 
var isNS4=document.layers?true:false; 

// Function toggles the control from disabled <-> enabled
// _w : the ID of the control to be toggled
// _h :  'h' or 's' = hide/show 
function toggleTT(_w,_h) { 
	if (isDOM) 
	{ 
		if (_h=='s') document.getElementById(_w).style.visibility='visible'; 
		if (_h=='h') document.getElementById(_w).style.visibility='hidden'; 
	} 
	else if (isIE) { 
		if (_h=='s') eval("document.all."+_w+".style.visibility='visible';"); 
		if (_h=='h') eval("document.all."+_w+".style.visibility='hidden';"); 
	} 
	else if(isNS4) 
	{ 
		if (_h=='s') eval("document.layers['"+_w+"'].visibility='show';"); 
		if (_h=='h') eval("document.layers['"+_w+"'].visibility='hide';"); 
	} 
} 

// Loads the Button Images.
function LoadButtonImage(im){
	var i=new Image();
	i.src='../theme/button'+im;
	return i;
}

// Changes the source of an image.
function ChangeImage(id){
	x=id.substring(0,id.length-1);
	document['button'+x].src=eval('button'+id+'.src');
	if(id.indexOf('e')!=-1)
		document['button'+x+'e'].src=eval('button'+id+'e.src');
}

// Changes the source of an input button.
function ChangeInputButtonImage(id){
	x=id.substring(0,id.length-1);
	document.getElementById('button'+x).src=eval('button'+id+'.src');
	if(id.indexOf('e')!=-1)
		document['button'+x+'e'].src=eval('button'+id+'e.src');
}
