/*
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);
	}*/
}


function isEmpty(formElement, message) {
 
  if(typeof(formElement) == "string")
    formElement= document.getElementById(formElement);

	formElement.value = trim(formElement.value);
	_isEmpty = false;
	if (formElement.value == null || typeof(formElement.value)=='undefined' || formElement.value == '' ) {
		_isEmpty = true;
		document.getElementById("errMessage").value = message;
				}
	return _isEmpty;
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmptyAlert(formElement, message) {
 
 if(typeof(formElement) == "string")
    formElement= document.getElementById(formElement);

	formElement.value = trim(formElement.value);
	_isEmpty = false;
	if (formElement.value==null || formElement.value=='' || typeof(formElement.value)=='undefined') {
		_isEmpty = true;
		alert(message);
		fldID = formElement.name;
		expr1 = "document.getElementById('" + fldID + "').focus();"
		setTimeout(expr1,1);
	}
	
	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;
		}
	}	
}

function focusOn( field ) {
  
    if ( field ) {
      field.focus();    // May throw "Permission denied" (uncatchable)
    }
 }
 
function changepic(img_name,img_src) {
    document[img_name].src = img_src;
}

function calcTotal()
{
	colID = document.getElementById('txtCurrentCol').value;
	if (colID!= '')	{
	   total =  Math.round(document.getElementById("clrp" + colID).title);
	   }
	else
	{total =  Math.round(document.getElementById('hidProdPrice').value);
	}
    for (i=1; i <= document.getElementById("txtNumOptions").value; i++) {
        odID = document.getElementById('option' + i).value;
		if (odID!= '')	{
			value = Math.round(document.getElementById('odp' + odID).title)
			if (value < 0 )
			{total = total * value *-1;}
			else
			{total = total + value;}
		}
	}		
    document.getElementById("txtTotal").value = "aud$" + total
}

function showshipping() {

  document.getElementById("cart").style.display = 'none';
   document.getElementById("shipping").style.display = 'inline';
}


function checkProductOptions(numOptions, pd_id) {
     
    options = Array();
        if (isEmpty(document.getElementById("txtCurrentCol"), 'Please select a colour'))
            {
            return false; 
            }
         else
            { 
            col_id = document.getElementById("txtCurrentCol").value;
            if (numOptions > 0) 
                {
                i = 1;
                for (i = 1; i <= numOptions; i=i+1) 
                    {
                    if (isEmpty(document.getElementById("option" + i), "Please select a " + document.getElementById("optimage" + i).alt.toLowerCase())) 
                       {
                        return false;
                       }
                    else 
                       {
                        //assign value from hidden option input field to options array
                        txtOption = "option"  + i;
                        options[i] = document.getElementById(txtOption).value;
                        }
                    }    
                }
        
            }
        
}

function isNumeric(vTestValue)
{
	// put the TEST value into a string object variable
	var sField = new String(trim(vTestValue));
	
	// check for a length of 0 - if so, return false
	if(sField.length==0) { return false; }
	else if(sField.length==1 && (sField.charAt(0) == '.' || sField.charAt(0) == ',' || (sField.charAt(0) == '-'))) { return false; }
	
	// loop through each character of the string
	for(var x=0; x < sField.length; x++) {
		// if the character is < 0 or > 9, return false (not a number)
		if((sField.charAt(x) >= '0' && sField.charAt(x) <= '9') || sField.charAt(x) == '.' || sField.charAt(x) == ',' || (sField.charAt(x) == '-' && x==0)) { /* do nothing */ }
		else { return false; }
	}
	
	// made it through the loop - we have a number
	return true;
}

function setGB() {
 var pathlength = window.location.pathname.lastIndexOf("/");
 var path = window.location.pathname.substr(0,pathlength);
 return "http://" + document.location.host + path + "/greybox/";
}