/*
Copyright (C) 2004 Jadiel Flores <jadiel@jadiel.com> <http://www.jadiel.com>
Cyberbox.com Site Administrator 1.0 by Jadiel Flores

This file is part of Cyberbox.com Site Administrator

Cyberbox.com Site Administrator is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

Cyberbox.com Site Administrator is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Cyberbox.com Site Administrator, if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

<xmp>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE fusedoc SYSTEM "http://fusebox.org/fd4.dtd">
<fusedoc fuse="scripts.js">
	<responsibilities>
		Javascript functions
	</responsibilities>	
	<properties>
		<history author="Jadiel Flores" date="{ts '2004-07-14'}" email="jadiel@jadiel.com" role="Architect" type="Create">Coded today.</history>
	</properties>
</fusedoc>
</xmp>
*/

	function isArray(obj){
		return(typeof(obj.length)=="undefined")?false:true;
	}

	function checkForm(poForm){
		for(i=0; i<poForm.elements.length; i++){
			if(poForm.elements[i].required == "true"){
//				alert(poForm.elements[i].type);
				switch (poForm.elements[i].type) {
					case "text":
					case "password":
					case "textarea":
					case "file":
						if(poForm.elements[i].value == ""){
							alert(poForm.elements[i].friendly);
							poForm.elements[i].focus();
							return false;
						}
						break;
					case "radio":
						elradio = eval("poForm."+poForm.elements[i].name);
						checado = false;
						if(isArray(elradio)){
							for(j=0; j<elradio.length; j++){
								if(elradio[j].checked)
									checado = true;
							}
						} else{
							if(elradio.checked)
								checado = true;
						}
						if(!checado){
							if(isArray(elradio)){
								alert(elradio[0].friendly);
								elradio[0].focus();
							} else{
								alert(elradio.friendly);
								elradio.focus();
							}
							return false;
						}
						break;
					case "select-one":
						if(poForm.elements[i].selectedIndex < 1){
							alert(poForm.elements[i].friendly);
							poForm.elements[i].focus();
							return false;
						}
						break;
					case "select-multiple":
						if(poForm.elements[i].selectedIndex < 1){
							alert(poForm.elements[i].friendly);
							poForm.elements[i].focus();
							return false;
						}
						break;
				}
			}
		}
		return true;
	}

	function emailCheck(emailStr) {
		var emailPat=/^(.+)@(.+)$/
		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
		var validChars="\[^\\s" + specialChars + "\]"
		var quotedUser="(\"[^\"]*\")"
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
		var atom=validChars + '+'
		var word="(" + atom + "|" + quotedUser + ")"
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
		var matchArray=emailStr.match(emailPat)
		if (matchArray==null) {
//			alert("Email address seems incorrect (check @ and .'s)")
			return false
		}
		var user=matchArray[1]
		var domain=matchArray[2]
		if (user.match(userPat)==null) {
//		    alert("The username doesn't seem to be valid.")
		    return false
		}
		var IPArray=domain.match(ipDomainPat)
		if (IPArray!=null) {
			for (var i=1;i<=4;i++) {
				if (IPArray[i]>255) {
//					alert("Destination IP address is invalid!")
					return false
				}
			}
			return true
		}
		var domainArray=domain.match(domainPat)
		if (domainArray==null) {
//			alert("The domain name doesn't seem to be valid.")
			return false
		}
		var atomPat=new RegExp(atom,"g")
		var domArr=domain.match(atomPat)
		var len=domArr.length
		if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
//			alert("The address must end in a three-letter domain, or two letter country.")
			return false
		}
		if (len<2) {
//			var errStr="This address is missing a hostname!"
			alert(errStr)
			return false
		}
		return true;
	}

	function openWin(Addr, Name, Options){
		var tmp = window.open(Addr, Name, Options);
	}
	
	function viewPic(prodID, picID, indice){
		openWin("index.php?fuseaction=shop.gallery&prodID="+prodID+"&picID="+picID+"&indice="+indice, "gallery", "width=500,height=400");
	}
	
	function checknumber(poField){
		var x=poField;
		var anum=/(^\d+$)|(^\d+\.\d+$)/;
		if (anum.test(x))
			testresult=true;
		else{
			testresult=false;
		}
		return (testresult);
	}
