var paleRed="#FFC8C8" // pale red

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function textCounter(field, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
}

function ispassvalid(newp,confp,compare)
{
	aExcl=new Array();
	aExcl[0]='"';
	aExcl[1]="'";
	aExcl[2]="&";
	aExcl[3]="!";
	aExcl[4]="`";
	aExcl[5]=" ";
	exclStr="!  '"+'  "'+"  `  &";
	var ps=newp.value.trim();
	var cntd=0, cntl=0;
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	if(newp.value.trim()=="" || newp.value.trim().length<5)
	{
		alert("Password must be at least 5 characters in length");
		newp.focus();
		return false;
	}
	else
	{
		if (newp.value!=confp.value && compare)
		{
			alert("New password is not identical with retyped password");
			newp.focus();
			return false;
		}
		else
		{
			if (sPage=="resetpass.php")
			{
				var tmp=newp.value.substring(0,7);
				if (tmp=="leapers")
				{
					alert("You can not reset your password to leapersN (N: any number) because leapersN is temporary password.");
					newp.focus();
					return false;
				}
			}
			var invalid=false;
			check: 
			{
				for (i=0;i<ps.length;i++)
				{
					if (ps.substr(i,1).charCodeAt()>=48 && ps.substr(i,1).charCodeAt()<=57)
						cntd++;
					if ((ps.substr(i,1).charCodeAt()>=65 && ps.substr(i,1).charCodeAt()<=90) || (ps.substr(i,1).charCodeAt()>=97 && ps.substr(i,1).charCodeAt()<=122))
						cntl++;
					for(j=0;j<aExcl.length;j++)
					{
						if (ps.substr(i,1)==aExcl[j])
						{
							invalid=true;
							break check;
						}
					}
				}
			}
			if (invalid)
			{
				alert("Password cannot contain space and characters: "+exclStr);
				newp.focus()
				return false;
			}
			else if (cntd==0 && cntl==0)
			{
				alert("Password must contain at least one number and letter.");
				newp.focus()
				return false;
			}
			else if (cntd==0)
			{
				alert("Password must contain at least one number.");
				newp.focus()
				return false;
			}
			else if (cntl==0)
			{
				alert("Password must contain at least one letter.");
				newp.focus()
				return false;
			}
			else
			{
				return true;
			}
		}
	}
	return true;
}

function isNum(n)
{
	var sText=n.value;
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	if (sText.length==1 && (sText=='0' || sText=='.'))
	{
		n.style.backgroundColor=paleRed
		alert("Please enter a nonzero quantity.");
		isNumber=false;
		return false;
	}
	else if (sText!="")
	{

		for (i = 0; i < sText.length && IsNumber == true; i++) 
		{ 
			Char = sText.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) 
			{
				IsNumber = false;
			}
		}
		if (IsNumber)
		{
			n.style.backgroundColor="FFFFFF"
			return true;
		}
		else
		{
			n.style.backgroundColor=paleRed
			alert("Please enter a quantity.");
			isNumber=false;
			return false;
		}
	}
	else
	{
		n.style.backgroundColor=paleRed
		alert("Please enter a quantity.");
		isNumber=false;
		return false;
	}
//	return IsNumber;
}

function emptycell(c,compval)
{
	if (c.value==compval)// "QTY" || c.value=="Keyword or Item #" || c.value=="KEYWORD OR ITEM #" || c.value=="Enter keyword")
	{
		c.value="";
	}
}

function instru(id,instr)
{
	if (id.value.trim()=="")
	{
		id.value=instr;
	}
}

// start ajaxrequest.js


// Following is a javascript function that makes a httprequest - AJAX. This is the AJAX bit and all that is needed in that manner.
// Only in this one we won't be using XML in our response, we will accept and handle
// pure text and html and display this response directly to the user within the
// desired <div id> tags. It can even be used to include pure html files as a substitute
// solution to the "old" frames method where as no php or other scripting language is nessesary on the server.
// but use it with care - it is not a search engine supported method and indexing will fail. Workaround for this is not included here

function MyAjaxRequest(target_div,file,check_div)
{
var MyHttpRequest = false;
var MyHttpLoading = '<p>Loading...</p>'; // or use an animated gif instead: var MyHttpLoading = '<img src="loading.gif" border="0" alt="running" />';
var ErrorMSG = 'Sorry - No XMLHTTP support in your browser, buy a newspaper instead';

if(check_div)
{
var check_value = document.getElementById(check_div).value;
}
else
{
var check_value = '';
}



if(window.XMLHttpRequest) // client use Firefox, Opera etc - Non Microsoft product
{
try
{
MyHttpRequest = new XMLHttpRequest();
}
catch(e)
{
MyHttpRequest = false;
}
}
else if(window.ActiveXObject) // client use Internet Explorer
{
try
{
MyHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
MyHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
MyHttpRequest = false;
}
}
}
else
{
MyHttpRequest = false;
}



if(MyHttpRequest) // browser supports httprequest
{
var random = Math.random() * Date.parse(new Date()); // make a random string to prevent caching

var file_array = file.split('.'); // prepare to check if we have a query string or a html/htm file
if(file_array[1] == 'php') // no query string, just calling a php file
{
  var query_string = '?rand=' + random;
}
else if(file_array[1] == 'htm' || file_array[1] == 'html') // calling a htm or html file
{
  var query_string = '';
}
else // we have presumable a php file with a query string attached
{
  var query_string = check_value + '&rand=' + random;
}


MyHttpRequest.open("get", url_encode(file + query_string), true); // <-- run the httprequest using GET


// handle the httprequest
MyHttpRequest.onreadystatechange = function ()
{
if(MyHttpRequest.readyState == 4) // done and responded
{
document.getElementById(target_div).innerHTML = MyHttpRequest.responseText; // display result
}
else
{
document.getElementById(target_div).innerHTML = MyHttpLoading; // still working
}
}
MyHttpRequest.send(null);
}
else 
{
document.getElementById(target_div).innerHTML = ErrorMSG; // the browser was unable to create a httprequest
}
}
// end of "AJAX" function


// Here follows a function to urlencode the string we run through our httprequest, it has nothing to do with AJAX itself
// If you look carefully in the above httprequest you se that we use this url_encode function around the file and query_string
// This is very handy since we are using GET in our httprequest and for instance
// any occurrance of the char # (from textboxes etc) will brake the string we are sending to our file - we don't want that to brake!
// It will also convert spaces to +

function url_encode(string)
{
var string;
var safechars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/-_.&?=";
var hex = "0123456789ABCDEF";
var encoded_string = "";
for(var i = 0; i < string.length; i++)
{
var character = string.charAt(i);
if(character == " ")
{
encoded_string += "+";
}
else if(safechars.indexOf(character) != -1)
{
encoded_string += character;
}
else
{
var hexchar = character.charCodeAt(0);
if(hexchar > 255)
{
encoded_string += "+";
}
else
{
encoded_string += "%";
encoded_string += hex.charAt((hexchar >> 4) & 0xF);
encoded_string += hex.charAt(hexchar & 0xF);
}
}
}
return encoded_string;
}

function fillshipinfo()
{
MyAjaxRequest('main', 'updateCat.php?action=fillship&ponumber='+document.getElementById("ponumber").value+'&placedby='+document.getElementById("placedby").value+'&shipmethod='+document.getElementById("shipmethod").value+'&selship='+document.getElementById("selship").value);
}	

function addCat(catval)
{
	MyAjaxRequest('main','updateCat.php?catcontent='+catval+"&action=add");
}

function removeCat(id)
{
	MyAjaxRequest('main','updateCat.php?id='+id+'&action=remove');
}
function removefromfavorite(id)
{
	alert("Item "+id+" has been removed from your favorite list.");
	MyAjaxRequest('main','updateCat.php?id='+id+'&action=removefavorite');
}
function addtofavorite(id)
{
	alert("Item "+id+" has been been added into your favorite list.");
	MyAjaxRequest('main','updateCat.php?id='+id+'&action=addfavorite');
}
function changeCat(id,unit,quantity)
{
	MyAjaxRequest('main','updateCat.php?id='+id+'&action=change&unit='+unit+"&quantity="+quantity);
}

function logcatalog(catname)
{
	MyAjaxRequest('main','updateCat.php?id='+catname+'&action=logcat');
}

/*function emptyCat()
{
}*/

function launchBrowser(url, wname, width, height) {
	var x = 0;
	var y = 0;
	if(navigator.appVersion.length > 0 && navigator.appVersion.charAt(0) > '3' && navigator.appVersion.charAt(0) <= '9') {
		if(width > screen.availWidth - 12) {
			width = screen.availWidth - 12;
		}
		if(height > screen.availHeight - 48) {
			height = screen.availHeight - 48;
		}
		x = (screen.availWidth - 12 - width) / 2;
		y = (screen.availHeight - 48 - height) / 2;
	}
	var params =  "resizable=yes,scrollbars=yes,location=no,status=yes,toolbar=no,menubar=no,directories=no,screenX=" + x + ",screenY=" + y + ",width=" + width + ",height=" + height + ")";
	var windowvar = window.open(url, wname, params);
	windowvar.creator=self;
	windowvar.focus();
}

function loadpam(pg,pnm)
{
	if (pnm=="listproduct.php")
	{
		document.location=pg+"&svcurrent="+document.getElementById("svcurrent").value;
	}
	else
	{
		document.location=pg;
	}
}

function airsoftwarning(mitem, previtem)
{
	if (mitem=="airso" && previtem!="airso") 
		launchBrowser("airsoftwarnning.php", "warnning", 300, 270);
}

function emailtofriend(mitem, itemno)
{
	var pam="emailtofriend.php?mitem="+mitem+"&itemno="+itemno;
	launchBrowser(pam,'emailtofriend',550,250);
}

function zPrint(oTgt)
{
	oTgt.focus();
	oTgt.print();
}


function actaddress(id)
{
//	alert(id.length);
//	alert(id.selectedIndex);
	for (i=0;i<id.length ;i++ )
	{
		if (id.options[i].value!="")
		{
			if (i==id.selectedIndex)
			{
				document.getElementById(id.options[i].value).style.display="";
			}
			else
			{
				document.getElementById(id.options[i].value).style.display="none";
			}
		}
	}
}

function checkemail(id) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = id.value;
   if(reg.test(address) == false) {
      alert('Please enter an valid email address.');
	  id.style.backgroundColor=paleRed;
      return false;
   }
   else
	{
	   id.style.backgroundColor='white';
	   return true;
	}
}

