var READY_STATE_UNINITIALIZED = 0;
var READY_STATE_LOADING = 1;
var READY_STATE_LOADED = 2;
var READY_STATE_INTERACTIVE = 3;
var READY_STATE_COMPLETE = 4;	
function isInteger(s){
var i;
for (i = 0; i < s.length; i++){ 
// Check that current character is number.
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
// All characters are numbers.
return true;
}
function removeleft0(s){
	while(s.charAt(0)=="0"){
		s = s.substr(1);
	}
	return s;
}
function openPopup(Id){
	sform = document.frmView;
	sform.PId.value = Id;
	open('',sform.name,'scrollbars=no,resizeable=no,locationbar=no,width=310,height=450,left='.concat((screen.width-301)/2).concat(',top=').concat((screen.height-500)/2));
	sform.action = 'ProductPopup.asp';
	sform.submit();
} 
function UpdateCart(){
	document.ShoppingCart.submit();
}

function CheckOut(){
	document.ShoppingCart.needupdate.value = 1;
	document.ShoppingCart.submit();
}
function Trim(iStr)
{
	while (iStr.charCodeAt(0) <= 32)
	{
		iStr=iStr.substr(1);
	}

	while (iStr.charCodeAt(iStr.length - 1) <= 32)
	{
		iStr=iStr.substr(0, iStr.length - 1);
	}

	return iStr;
}

function CheckEmailAddress(Email)
{
	Email = Trim(Email);

	while (Email != '')
	{
		c = Email.charAt(0);	
		if (c==' ' || c=='<' || c==39 || c==':' || c=='.')
		{
			Email = Email.substr(1);
		}
		else
		{
			break;
		}
	}

	i = Email.indexOf('>');
	if (i==-1)
	{
		while (Email != '')
		{
			c = Email.charAt(Email.length - 1);
			if (c==' ' || c==39 || c=='.')
			{
				Email = Email.substr(0, Email.length - 1);
			}
			else
			{
				break;
			}
		}
	}
	else
	{
		Email = Email.substr(0, i);
	}

	if (Email.length > 96)
		return '';

	i = Email.lastIndexOf('@');
	j = Email.lastIndexOf('.');
	if (i < j)
		i = j;

	switch (Email.length - i - 1)
	{
	case 2:
		break;
	case 3:
		switch (Email.substr(i))
		{
		case '.com':
		case '.net':
		case '.org':
		case '.edu':
		case '.mil':
		case '.gov':
		case '.biz':
		case '.pro':
		case '.int':
			break;
		default:
			return '';
		}
		break;
	default:
		switch (Email.substr(i))
		{
		case '.name':
		case '.info':
			break;
		default:
			return '';
		}
		break;
	}

	Email = Email.toLowerCase();

	if (Email == '')
		return '';

	if (Email.indexOf(' ') != -1)
		return '';

	if (Email.indexOf('..') != -1)
		return '';

	if (Email.indexOf('.@') != -1)
		return '';

	if (Email.indexOf('@.') != -1)
		return '';

	if (Email.indexOf(':') != -1)
		return '';

	for (i=0; i < Email.length; i++)
	{
		c = Email.charAt(i);

		if (c >= '0' && c <= '9')
			continue;
		
		if (c >= 'a' && c <= 'z')
			continue;
		
		if ('`~!#$%^&*-_+=?/\\|@.'.indexOf(c) != -1)
			continue;

		return '';
	}

	if ((i=Email.indexOf('@'))==-1)
		return '';

	if (Email.substr(i + 1).indexOf('@')!=-1)
		return '';

	if (Email.charAt(0)=='.' || Email.charAt(Email.length - 1)=='.')
		return '';

	return Email;
}
function GetProductInfor(Id)
{
	if(!Id){
		return;
	}
	sendRequest('LoadImage.asp','Id='+Id,'POST');
}


function getXMLHTTPRequest(){
	var xRequest = null;
	if(window.XMLHttpRequest){
		xRequest = new XMLHttpRequest();//Mozila/Safari
	}else if (typeof ActiveXObject != "undefined"){
		xRequest = new ActiveXObject("Microsoft.XMLHTTP")
	}
	return xRequest;
}


function sendRequest(url, params, HttpMethod){
	if(!HttpMethod){
		HttpMethod = "GET";
	}
	req = getXMLHTTPRequest();
	if (req){
		req.onreadystatechange=onReadyStateChange;
		req.open(HttpMethod, url, true);
		req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		req.send(params);
	}
}
function onReadyStateChange(){
	var ready=req.readyState;
	var data = null;
	if (ready == READY_STATE_COMPLETE){
		data=req.responseXML;
		if(data.childNodes.length!=0){
			PCode = data.childNodes[data.childNodes.length-1].childNodes[0].firstChild.nodeValue;
			PImage = data.childNodes[data.childNodes.length-1].childNodes[1].firstChild.nodeValue;
			PId = data.childNodes[data.childNodes.length-1].childNodes[2].firstChild.nodeValue;
			Height = parseInt(data.childNodes[data.childNodes.length-1].childNodes[3].firstChild.nodeValue);
			Width = parseInt(data.childNodes[data.childNodes.length-1].childNodes[4].firstChild.nodeValue);
			var obj = document.getElementById("Product_Code");
			var obj1 = document.getElementById("Product_Image");
			var str = ""
			str = '<table border="0" cellspacing="0" cellpadding="0" width="100%">';
			str = str + '<tr align="center"><td style = "margin-left:60px; magin-top:10;" align="center">';
			str = str + '	<font face="VNITime" color="#000099" style="font-size: 9pt">';
			str = str + '	<p style="margin-top: 0; margin-bottom: 0">'+ PCode +'</td></tr>';
			str = str + '</font>';
			str = str +  '</table>';
			obj.innerHTML = str;
			if(PImage!=null){
				strImage = '<IMG SRC="http://hoangia.hieulq.net/Upload/' + PImage + '.jpg" width="' + Width + '" height="' + Height + '" style="border-width: 0px">';
				obj1.innerHTML = strImage;
			}
				document.frmtmp.ProductId.value = PId;
		}
	}else{
		return false;
	}
}
