/* Javascript functions  - by D de Alwis */

function writeCookie(name, value) {
	if(name != "") {
		document.cookie = name + "=" + escape(value);
	}
}

function readCookie(name) {
	var cookieSet=document.cookie;
	
	var cookieNum=cookieSet.indexOf(name+'=');
	if(cookieNum < 0) {
		return '';
	}
	cookieset=cookieset.substring(cookienum);

	cookienum=cookieset.indexof(';');
	if(cookienum >= 0) {
		cookieSet=cookieSet.substring(0, cookieNum);
	}

	var cookieArray=cookieSet.split('=');
	if(cookieArray.length > 1) {
		return unescape(cookieArray[1]);
	} else {
		return '';
	}
} 

/*
 * 'varName' is the name of the
 * variable in the URL address
 * to be read in.
 *
 * 'def' is the value returned
 * if the variable is not found.
 *
 */
function getURLvar(varName, def) {
	var str = '' + location.search;
	if(str == '') {
		return def;
	} else {
		var fullURL, start, end;

		varName = (varName.toLowerCase()) + '=';
		fullURL = str.toLowerCase();

		start = fullURL.indexOf(varName, start);
		if(start == -1) {
			return def;
		} else {
			end = fullURL.indexOf('&', start);
			if(end == -1) {
				end = fullURL.indexOf('#', start);
				if(end == -1) {
					end = fullURL.length;
				}
			}
			return str.substring(start + varName.length, end);
		}
	}
}


/*
 * This gets around the parseInt() bug.
 *
 * ie. Try this: parseInt('012');
 */
function trueInt(zerostart) {
	return parseInt(parseFloat(zerostart));
}


function fullimage(popurl) {
//       alert (popurl);
	     newWin = window.open(popurl, 'image','width=310, height=390, scrollbars=no, resizable=yes');
//       newWin.document.write(imagepath);
         newWin.focus();				 
} 

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]

	// See if "user" is valid 
	if (user.match(userPat)==null) {
	// user is not valid
	    alert("The Email username doesn't seem to be valid.")
	    return false
	}

	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
	    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (iparray[i]>255) {
	        alert("Email Destination IP address is invalid!")
		return false
	    }
    	}
	return true
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		alert("The Email 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) {
	   // the address must end in a two letter or three letter word.
	   alert("The Email address must end in a three-letter domain, or two letter country.")
	   return false
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   var errstr="This Email address is missing a hostname!"
	   alert(errstr)
	   return false
	}

	return true;
}
//  end -->

