
//function to set a node value variable
function setNodeVariableParameter(variableName,value,targetId){
	//do not use % as the seperator
	//it may be interpreted by the browser as a different character
	//check the variable exists in the target - which will be a node
	var varIndex = 0;
	var ampIndex = 0;
	var paramIndex = 0;
	var searchVariable = new String("&" + variableName + "=");
	var valueVariable = new String(value);
	var targetNode;
	var targetValue;
	var firstPart;
	var secondPart;
	var thirdPart;
	var fourthPart;
	var fifthPart;
	
	targetNode = document.getElementById(targetId);
	targetValue = targetNode.getAttribute('value');
	
	if (targetValue != ""){
		
		//search for the variable
		varIndex = targetValue.indexOf(searchVariable);
		if(varIndex > -1){
			
			//ok we've found it, so set the value
			firstPart = targetValue.substring(0,varIndex);
			secondPart = targetValue.substring(varIndex + searchVariable.length);
			
			if(secondPart.length >0){
				ampIndex = secondPart.indexOf("&");
				
				if(ampIndex > -1){
					thirdPart = secondPart.substring(0, ampIndex);
					fourthPart = secondPart.substring(ampIndex);
					//thirdPart will contain the variable parameters
					//check if the parameter exists
					paramIndex = thirdPart.indexOf(valueVariable);
	
					if(paramIndex > -1){
						//parameter exists so remove it
						fifthPart = thirdPart.substring(0,paramIndex);
						sixthPart = thirdPart.substring(paramIndex + valueVariable.length);
						
						targetNode.setAttribute('value', firstPart + searchVariable + fifthPart + sixthPart + fourthPart);
					}
					else{
						
						//parameter does not exist so add it
						targetNode.setAttribute('value',firstPart + searchVariable + thirdPart + valueVariable + fourthPart);
					}
				}
				else{
					
					//no following variable
					paramIndex = secondPart.indexOf(valueVariable);
					if(paramIndex > -1){
						//parameter exists so remove it
						thirdPart = secondPart.substring(0,paramIndex);
						fourthPart = secondPart.substring(paramIndex + valueVariable.length);
						
						targetNode.setAttribute('value',firstPart + searchVariable + thirdPart + fourthPart);
					}
					else{
						//parameter does not exist so add it
						targetNode.setAttribute('value',firstPart + searchVariable + secondPart + valueVariable );
					}
				}
			}
			else{
				targetNode.setAttribute('value',firstPart + searchVariable + valueVariable);
			}
		}
	}
	
}

	
//function to add a parameter to a URL Link Variable
//it will add or remove it depending on whether it already
//exists or not
//**************************
function setLinkVariableParameter(variableName,target,separator,value){
	//NOTE:---DO NOT USE % AS A SEPERATOR
	//IT MAY BE INTERPRETED BY THE BROWSER AS A DIFFERENT CHARACTER
	//*************************************************************

	//check the variable exists in the target
	var varIndex = 0;
	var ampIndex = 0;
	var paramIndex = 0;
	var searchVariable = new String("&" + variableName + "=");
	var valueVariable = new String(separator + value);
	var linkString = new String(target);
	var firstPart = "";
	var secondPart = "";
	var thirdPart = "";
	var fourthPart = "";
	var fifthPart = "";
	var sixthPart = "";
	
	varIndex = target.indexOf(searchVariable);

	if(varIndex > -1){
		//variable found
		//we now need to get the value of the variable
		firstPart = target.substring(0,varIndex);
		secondPart = target.substring(varIndex + searchVariable.length);

		if(secondPart.length >0){
			ampIndex = secondPart.indexOf("&");

			if(ampIndex > -1){
				thirdPart = secondPart.substring(0, ampIndex);
				fourthPart = secondPart.substring(ampIndex);
				//thirdPart will contain the variable parameters
				//check if the parameter exists
				paramIndex = thirdPart.indexOf(valueVariable);

				if(paramIndex > -1){
					//parameter exists so remove it
					fifthPart = thirdPart.substring(0,paramIndex);
					sixthPart = thirdPart.substring(paramIndex + valueVariable.length);

					return firstPart + searchVariable + fifthPart + sixthPart + fourthPart;
				}
				else{
					//parameter does not exist so add it
					return firstPart + searchVariable + thirdPart + valueVariable + fourthPart;
				}
			}
			else{
				//no following variable
				paramIndex = secondPart.indexOf(valueVariable);
				if(paramIndex > -1){
					//parameter exists so remove it
					thirdPart = secondPart.substring(0,paramIndex);
					fourthPart = secondPart.substring(paramIndex + valueVariable.length);
					
					return firstPart + searchVariable + thirdPart + fourthPart;
				}
				else{
					//parameter does not exist so add it
					return firstPart + searchVariable + secondPart + valueVariable ;
				}
			}
		}
	}
	return linkString; 
}	


//function to get the value from an input field
function getInputValue(inputId){
	var inputNode = null;
	
	inputNode = document.getElementById(inputId);
	if (inputNode != null) {
		return inputNode.getAttribute('value');	
	}
}

//function to handle Enter key presses from dropdowns on search form
function searchFormKeyPressHandler(e){
	var characterCode;
	var target;
	
	if(e && e.which) {
		//NN4
		e=e;
		characterCode = e.which;
	}
	else{
		//IE
		e = event;
		characterCode = e.keyCode;
	}
	
	if (characterCode == 13){
		//we need to get the target URL to jump to
		target = document.getElementById('hiddenSearchLocation');
		if (target != null){
			document.location.href = target.getAttribute('value');
		}
	}
}

function addLinkVariable(variable,target){
	target += variable;
}

//function to set a variable in an href
function setHrefVariable(id,variableName,value){
	target = null;
	
	//get the target object
	for(i=0;i<document.links.length;i++){
		if(document.links[i].id == id){
			target = document.links[i];
			break;
		}
	}
	
	if(target != null){
		targethref = target.href;
		//set the value for the product options
		target.href = setLinkVariable(variableName, value, targethref);
	}
}

	
//function to replace the value of a URL Link variable
function setLinkVariable(variableName, newValue, target){
	//check the variable name exists in the target
	var varIndex = 0;
	var ampIndex = 0;
	var linkString = new String(target);
	var searchVariable = new String("&" + variableName + "=");
	var ampString = new String("&");
	var firstPart = "";
	var secondPart = "";
	var thirdPart = "";
	var fourthPart = "";
	
	varIndex = linkString.indexOf(searchVariable);
	
	if(varIndex > -1){
		//found the variable
		firstPart = linkString.substring(0, varIndex);
		secondPart = linkString.substring(varIndex+searchVariable.length);

		//if there is already a value, we need to replace it
		if(secondPart.length > 0){
			ampIndex = secondPart.indexOf(ampString);
			if(ampIndex > -1){
				thirdPart = secondPart.substring(0, ampIndex);
				fourthPart = secondPart.substring(ampIndex);
				
				return firstPart + searchVariable + newValue + fourthPart;
			}
			//else{
			//	return firstPart + searchVariable + newValue + secondPart;
			//}
		}
	
		return firstPart + searchVariable + newValue;
	}
}

//function to set the id's from dropdowns
function setDropdownId(source,target) {
	var index;
	index = source.selectedIndex;
	target.value = source.options[index].id;
}			
	
//function to set the selected value based on the id
function setDropdownSelected(source,target){
	var index;
	var i;
	index = source.value;
	
	if(index >0){
		for(i=0;i<target.options.length;i++){
			if(target.options[i].id == index){
				target.options[i].selected = "selected";
				break;
			}
		}
	}
}


//function to set the selected value based on the id
function setDropdownFromId(id,target){
		for(i=0;i<target.options.length;i++){
			if(target.options[i].id == id){
				target.options[i].selected = "selected";
				break;
			}
		}
	
}

//function to display a message
function displayMsg(msg){
	//msgString = new String(warningmsg);
	
	if(msg != ""){
		alert(msg);
	}
}

//function to display a new window with specified parameters
//and page link
function openWindow(url,name,params){
	winRef = window.open(url,name,params);
	return false;
}

/*
* function to set a field value
*/
function setFieldValue(value, target){
	target.value = value;
}

//only allow numeric characters - decimals can be allowed
function OnlyNumerics(keypress, decimals) {
	var key;
	var keychar;

	if (window.event)
		key = window.event.keyCode;
	else if (keypress)
		key = keypress.which;
	else
		return true;
		keychar = String.fromCharCode(key);


	//Control Keys
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27))
		return true;

	//Numbers
	else if ((("0123456789").indexOf(keychar) > -1))
		return true;

	else{
		if (decimals && key==46){
			return true;
		}
		else{
			return false;	
		}
	}

}

function refreshOpener(){
	if(window.opener){
		window.opener.location.href = window.opener.location.href;
	}
}



