// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject()
{
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// this should work for all browsers except IE6 and older
	try
	{
		// try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		// assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
		"MSXML2.XMLHTTP.5.0",
		"MSXML2.XMLHTTP.4.0",
		"MSXML2.XMLHTTP.3.0",
		"MSXML2.XMLHTTP",
		"Microsoft.XMLHTTP");
		// try every prog id until one works
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
		{
			try
			{
				// try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch (e) {}
		}
	}
	// return the created object or display an error message
	if (!xmlHttp)
		alert("Error creating the XMLHttpRequest object.");
	else
		return xmlHttp;
}

// read a file from the server
function ups_process()
{
	// only continue if xmlHttp isn't void
	if (!xmlHttp) return;
	// don't try to make server requests if the XMLHttpObject is busy
	if (xmlHttp.readyState != 0 && xmlHttp.readyState != 4)
		alert("Can't connect to server, please try again later.");
	else
	{
		// try to connect to the server
		try
		{
			// get postal code values entered by the user
			var pc = document.getElementById("upspc").value;
			var weight = document.getElementById("shipweight").value;
			var price = document.getElementById("shipprice").value;
			// create the params string
			var params = "pc=" + pc + "&weight=" + weight + "&price=" + price;
			// initiate the asynchronous HTTP request
			xmlHttp.open("GET", "http://www.jumbocomputers.ca/index.php?do=Cart&cmd=sc&" + params, true);
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send(null);
		}
		// display the error in case of failure
		catch (e)
		{
			alert("Can't connect to server:\n" + e.toString());
		}
	}
}

// function called when the state of the HTTP request changes
function handleRequestStateChange()
{
	// when readyState is 4, we are ready to read the server response
	if (xmlHttp.readyState == 4)
	{
		// continue only if HTTP status is "OK"
		if (xmlHttp.status == 200)
		{
			try
			{
				// do something with the response from the server
				handleServerResponse();
			}
			catch(e)
			{
				// display error message
				alert("Error reading the response: " + e.toString());
			}
		}
		else
		{
			// display status message
			alert("There was a problem retrieving the data:\n" +
			xmlHttp.statusText);
		}
	}
}

// handles the response received from the server
function handleServerResponse()
{
	// retrieve the server's response
	var response = xmlHttp.responseText;

	if (response == "Error")
		throw("Problem to estimate shipping cost, please  input valid postal code");

	// display shipping charge
	upsDiv = document.getElementById("upsDivElement");
	upsDiv.innerHTML = response;
}

function openWindow( pageToLoad, winName, width, height, center)
{
    xposition=0; yposition=0;
    if ((parseInt(navigator.appVersion) >= 4 ) && (center)){
	xposition = (screen.width - width) / 2;
	yposition = (screen.height - height) / 2;
    }
    args = "width=" + width + ","
    + "height=" + height + ","
    + "location=0,"
    + "menubar=0,"
    + "resizable=No,"
    + "scrollbars=1,"
    + "status=0,"
    + "titlebar=0,"
    + "toolbar=0,"
    + "hotkeys=0,"
    + "screenx=" + xposition + ","  //NN Only
    + "screeny=" + yposition + ","  //NN Only
    + "left=" + xposition + ","     //IE Only
    + "top=" + yposition;           //IE Only

    window.open( pageToLoad,winName,args );
}

function CheckRegisterFields()
{
	var account = document.account;

	if (account.username.value == "")
	{
		alert("Please enter your 'User Name'.");
		return false;
	}
	else if (account.password.value == "")
	{
		alert("Please enter a 'Password'.");
		return false;
	}
	else if (account.password.value != account.confirmpassword.value)
	{
		alert("Your 'Password' and 'Confirm Password' do not match.");
		return false;
	}
//	else if (account.email.value == "")
//	{
//		alert("Please enter your 'Email Address'.");
//		return false;
//	}
	else if (account.name.value == "")
	{
		alert("Please enter your 'Name'.");
		return false;
	}

	return CheckUserFields();
//	return true;
}

function CheckPwdFields()
{
	var chgpwd = document.ChangePassword;

	if (chgpwd.oldpwd.value == "")
	{
		alert("Please enter a 'Old Password'.");
		return false;
	}
	else if (chgpwd.newpwd.value == "")
	{
		alert("Please enter a 'New Password'.");
		return false;
	}
	else if (chgpwd.newpwd.value != chgpwd.confirmpassword.value)
	{
		alert("Your 'New Password' and 'Confirm Password' do not match.");
		return false;
	}

	return true;
}

function CheckUserFields()
{
	var addr = document.account;

	if (addr.name.value == "")
	{
		alert("Please enter your 'Name'.");
		return false;
	}
	else if (addr.address1.value == "")
	{
		alert("Please enter a 'Address'.");
		return false;
	}
	else if (addr.city.value == "")
	{
		alert("Please enter your 'City'.");
		return false;
	}
	else if (addr.prov.value == "")
	{
		alert("Please select your 'Province'.");
		return false;
	}
	else if (addr.country.value == "")
	{
		alert("Please select your 'Country'.");
		return false;
	}
	else if (addr.email.value == "")
	{
		alert("Please enter your 'Email Address'.");
		return false;
	}
	else if (addr.postalcode.value == "")
	{
		alert("Please enter your 'Postal Code'.");
		return false;
	}
	if (addr.country.value == "CA")
	{

		var pc = addr.postalcode.value;
		if ( ! validatepc(pc))
		{
			alert("Please enter a valid postal code.");
			return false;
		}
	}

	return true;
}


function CheckSearchKW()
{
	var search = document.searchform;

	if (search.kw.value == "") {
		alert("Please enter keyword to search.");
		return false;
	}

	return true;
}
function confirmPlaceOrder()
{
var agree=confirm("Are you sure you want to place this order?");
if (agree)
	return true ;
else
	return false ;
}
function validate(string) {
    if (!string) return false;
    var Chars = "0123456789";

    for (var i = 0; i < string.length; i++) {
       if (Chars.indexOf(string.charAt(i)) == -1)
          return false;
    }
    return true;
}
function removeCharacters( strValue, strMatchPattern )
{
	var objRegExp =  new RegExp( strMatchPattern, 'gi' );

	//replace passed pattern matches with blanks
	return strValue.replace(objRegExp,'');
}

function validateValue( strValue, strMatchPattern )
{
	var objRegExp = new RegExp( strMatchPattern);

	//check if string matches pattern
	return objRegExp.test(strValue);
}

function formatpc()
{
	if (document.account.country.value == "CA" ||
		document.account.country.value == "")
	{
		pc = document.account.postalcode.value;
		pc = pc.toUpperCase();    // in case of lowercase characters
		document.account.postalcode.value = pc;
		pc = removeCharacters(pc, '[ ]');
		fpc = pc;
		if (pc.length >= 3 && pc.indexOf("-")<0)
		{
			fpc = pc.substring(0,3) + '-' + pc.substring(3,6);
		}
		document.account.postalcode.value = fpc;
	}
	setTimeout(formatpc,100);
}

function validatepc(pc)
{
	var ok = true;

	if (pc == null)
	{
		ok = false;
	}
	if (pc.length < 7)
	{
		ok = false;
	}
	for (i=0; i<pc.length; i++)
	{
		if (i==0)
		{
			if ('ABCEGHJKLMNPRSTVXY'.indexOf(pc.charAt(i))<0)
			{
				ok = false;
			}
		}

		if (i==2 || i==5)
		{
			if ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'.indexOf(pc.charAt(i))<0)
			{
				ok = false;
			}
		}

		if (i==1 || i==4 || i==6)
		{
			if ('0123456789'.indexOf(pc.charAt(i))<0)
			{
				ok = false;
			}
		}
	}
	return ok;
}

function ValidatePhone()
{
	var p;

	p = document.account.phone.value;

	if (p.length==3)
	{
		pp = p;
		d4 = p.indexOf('(');
		d5 = p.indexOf(')');
		if (d4==-1)
		{
			pp="("+pp;
		}
		if (d5==-1)
		{
			pp=pp+")";
		}
		document.account.phone.value = "";
		document.account.phone.value = pp;
	}
	if (p.length>3)
	{
		d1 = p.indexOf('(')
		d2 = p.indexOf(')')
		if (d2==-1)
		{
			l30 = p.length;
			p30 = p.substring(0,4);
			p30 = p30+")";
			p31 = p.substring(4,l30);
			pp  = p30+p31;
			document.account.phone.value = "";
			document.account.phone.value = pp;
		}
	}
	if(p.length>5)
	{
		p11=p.substring(d1+1,d2);
		if (p11.length>3)
		{
			p12 = p11;
			l12 = p12.length;
			l15 = p.length;
			p13 = p11.substring(0,3);
			p14 = p11.substring(3,l12);
			p15 = p.substring(d2+1,l15);
			document.account.phone.value = "";
			pp  = "("+p13+")"+p14+p15;
			document.account.phone.value = pp;
		}
		l16 = p.length;
		p16 = p.substring(d2+1,l16);
		l17 = p16.length;
		if(l17>3 && p16.indexOf('-')==-1)
		{
			p17 = p.substring(d2+1,d2+4);
			p18 = p.substring(d2+4,l16);
			p19 = p.substring(0,d2+1);
			pp  = p19+p17+"-"+p18;
			document.account.phone.value = "";
			document.account.phone.value = pp;
		}
	}
	setTimeout(ValidatePhone,100);
}

function show(obj1, obj2, img1, img2)
{
	var layerObj = document.getElementById(obj1);
	if (layerObj != null)
	{
		document.getElementById('pdtab1').style.display = 'none';
		document.getElementById('pdtab2').style.display = 'none';
		document.getElementById('pdtab3').style.display = 'none';
		document.getElementById('pdtab4').style.display = 'none';
//		document.getElementById('pdtab5').style.display = 'none';

		document.getElementById(obj1).style.display = '';
//		MM_swapImgRestore();
//		MM_swapImage(img1,'',img2,1);
	}
}