/**
* Common Javascript functions
*
* Used by both Admin system and site
*
* @version 1.1 02/01/2010
* V1.1 Added getSelectedItem for Radio Buttons
*/

/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/

function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}

	textBox.value = trim(textBox.value);
	/*if (textBox.value.length == 0) {
		textBox.value = 0;
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);

	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	return _isEmpty;
}

/* Set one value in combo box as the selected value */

function setSelect(listElement, listValue) {
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}
}

/*	These functions added by NC from top.html to keep all JS in one place */

function showsearch(keywords){
	document.searchform.keywords.value=keywords;
	with (window.document.searchform) {
		submit();
	}
}

function getProductOptions(p)
{
	with (window.document.frmProductOptions) {
		window.location.href = 'index.php?p=' + p + '&o=' + sizeOption.options[sizeOption.selectedIndex].value;
	}
}

function urlencode(str) {
	return escape(str).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '%2F').replace('@', '%40');
}

function urldecode(str) {
return unescape(str.replace('+', ' '));
}

/* Get selected Radio Button value */
function getSelectedItem(f1, r1)
{
	chosen = "";
	len = document[f1][r1].length;

	for (i = 0; i <len; i++) {
		if (document[f1][r1][i].checked) {
			chosen = document[f1][r1][i].value;
		}
	}
	return chosen;
}

/**
* Submit a form from a hyperlink
*/
function submitForm(formName) {
	document[formName].submit();
}
function noPaste(formElement) {
	alert('Sorry, copy & paste not allowed.');
	formElement.value="";
	//document.customerReg.customerEmail2.value="";
	//document.customerReg.customerEmail2.focus();
	return false;
}

function confirmEmail()
{
	var email2 = prompt("Please re enter your email address.");
	if(email2 != null){
		email1 = document.customerReg.customerEmail.value;
		if(email1 != email2){
			alert("Email addresses match");
		} else {
			alert("Sorry, email addresses don't match, try again");
		}
		//window.location.href = 'processAssembly.php?action=renameOption&option=' + optionId + '&name=' + optionName;
	}
	return false;
}

/* Grateful thanks to Jeremy Keith for this function */
function showPic (whichpic) {
	if (document.getElementById) {
		document.getElementById('main_image').src = whichpic;
		return false;
	} else {
		return false;
	}
}

// Set up xmlhttp request
var xmlhttp = false;
try {
    xmlhttp = new XMLHttpRequest();
    } catch (trymicrosoft) {
    try {  
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (othermicrosoft) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (failed) {
                xmlhttp = false;
            }
        }
    }
if (!xmlhttp){
    alert("Sorry, your browser does not support this application! Please update your browser and make sure JavaScript is enabled");
}

// AJAX function for switching active flag
function activeSwitch(item_id, element_id)
{
   // alert('element_id= '+element_id+' item_id= '+item_id);
if (item_id=='')
  {
  alert('You must specify an item_id');
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById(element_id).innerHTML=xmlhttp.responseText;
    }
  }
// alert('order_date= '+order_date+' Transaction= '+transaction);
xmlhttp.open("GET","changeActive.php?item_id="+item_id+"&eid="+element_id,true);
xmlhttp.send();
}
