// JavaScript Document

/*------------------------------------------------------------------------
	StripSpaces
	:: function to strip spaces from pre & post entry.
		input	 - str: string
		output - x: string
------------------------------------------------------------------------*/
function StripSpaces(str) {
	x = str;
	
	while (x.substring(0,1) == ' ') x = x.substring(1);
	while (x.substring(x.length-1,x.length) == ' ') x = x.substring(0,x.length-1);
	
	return x;
}
/*--StripSpaces---------------------------------------------------------*/

/*------------------------------------------------------------------------
	IsValidEmail
	:: function to validate the email address enterred.
		input	 - str: string
		output - 0 or 1
------------------------------------------------------------------------*/
function IsValidEmail(str) {

	//var reg = new RegExp(/(^[a-z0-9][\w-]{0,}([\.]?[a-z0-9]{1,}){1,}@[a-z0-9][\w-]{1,}([\.][a-z]{2,}){1,}$)/i);
	var reg = new RegExp(/(^[a-z0-9][\w-]{0,}([\.]?[a-z0-9]{1,}){0,}@[a-z0-9][\w-]{1,}([\.][a-z]{2,}){1,}$)/i);
	if (str.match(reg)) {
		return 0; //successful
	} else {
		return 1;
	}
}
/*--IsValidEmail--------------------------------------------------------*/

/*------------------------------------------------------------------------
	IsValidPhone
	:: function to validate the phone number enterred is using	valid characters
	:: allowing: a +(for international) 0-9 space - ()
		input	 - str: string
		output - dirty: 0 or 1
------------------------------------------------------------------------*/
function IsValidPhone(str){
	dirty = 0;	
	if(str != ""){
		if (checkPhone(str) == false){
			dirty = 1;
		}
	}
	return dirty;
}

var digits = "0123456789";
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 8;

function isInteger(s)
{
	var i;
	for (i = 0; i < s.length; i++)
	{   
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	return true;
}

function stripCharsInBag(s, bag)
{
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++)
	{   
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function checkPhone(str){
	s = stripCharsInBag(str,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function func_deleteItem(itemNo,site){
		location.href = "shopping-cart.php?s="+ site +"&a=deleteItem&iID=" + itemNo;
}
/*--IsValidPhone---------------------------------------------------------*/

function confirmDelete(isbn){
	var answer = confirm("Delete this item?", "Delete")
	if(answer){
		document.getElementById("DeleteItem_" + isbn).submit();
	}
}
