function checkWindow(myName) 
{
	try{if(eval("window."+myName+".document"))return true;}catch(e){}
	return false;
}

var win=null;

function NewWindow(mypage,myname,w,h,scroll,pos){
	if(pos=="random"){
		LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
	}
	if(pos=="center"){
		LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;
	}
	else if((pos!="center" && pos!="random") || pos==null){
		LeftPosition=0;
		TopPosition=20;
	}
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	win=window.open(mypage,myname,settings);
}

function popUp(theUrl, myName) 
{ 
	eval(myName+" = window.open(theUrl, myName)");
}


var currentPopup = false;

function showLayer(id) {
	if (currentPopup) hideLayer(currentPopup);
	var o = document.all.item(id);
	o.style.visibility = "visible";
	currentPopup = id;
}

function hideLayer(id) {
	var o = document.all.item(id);
	o.style.visibility = "hidden";
}

function getBrowserInfo() {
	if (checkIt('konqueror')) {
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser 	= "Safari"
	else if (checkIt('omniweb')) browser 	= "OmniWeb"
	else if (checkIt('opera')) browser 		= "Opera"
	else if (checkIt('webtv')) browser 		= "WebTV";
	else if (checkIt('icab')) browser 		= "iCab"
	else if (checkIt('msie')) browser 		= "Internet Explorer"
	else if (!checkIt('compatible')) {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS) {
		if (checkIt('linux')) OS 		= "Linux";
		else if (checkIt('x11')) OS 	= "Unix";
		else if (checkIt('mac')) OS 	= "Mac"
		else if (checkIt('win')) OS 	= "Windows"
		else OS 								= "an unknown operating system";
	}
}

function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

function randomNumber(UpTo){
	var ran_unrounded=Math.random()*UpTo;
	var ran_number=Math.floor(ran_unrounded);	
	
	return ran_number;
}

function checkArraySimple(needle, haystack){
	for(var i = 0; i < haystack.length; i++){
		if(haystack[i] == needle){
			return false;
		}
	}
	return true;
}

function checkArrayComplex(TheArray, TheFor, splitBy){
	if(TheArray != null && TheArray.length != 0){
		TheArray = TheArray.split(splitBy)
		for(var i = 0; i < TheArray.length; i++){
			if(TheArray[i] == TheFor){
				return false;
			}
		}
	}
	else{
		return false;
	}
	return true;
}

function compareArrayComplex(passed, taken, splitBy){
	passed = passed.split(splitBy);
	passed = passed.sort();
	
	taken = taken.split(splitBy);
	taken = taken.sort();
	
	if(passed.length==taken.length){
		return true;
	}
	else{
		return false;
	}
}

function createElement(){
	var returnElement = document.createElement(arguments[0]);
	
	for(var i = 1; i < arguments.length; i++){
		returnElement.setAttribute(arguments[i], arguments[i+1]);
		i++;
	}
	
	return returnElement;
}

function getById(){
	return document.getElementById(arguments[0]);	
}

function createTextNode(){
	return document.createTextNode(arguments[0]);
}

function appendChild(){
	arguments[0].appendChild(arguments[1]);
}

function createInput(name, value, type){
	if(type == "cb") type = "checkbox";
	if(type == "rb") type = "radio";
	var id = "idrad_" + name;
	if(browser == "Internet Explorer"){
		var objRadItem = document.createElement("<input type='"+type+"' value='"+value+"' value='"+value+"' id='"+id+"' name='"+name+"'>");
	}
	else{
		var objRadItem = document.createElement("input");
		objRadItem.type = type;
		objRadItem.name = name;
		objRadItem.id = id;
		objRadItem.value = value;
	}
	//alert(objRadItem);
	return objRadItem;
}

function changestate(element) {
	documentDirty=true;
	currval = eval(element).value;
	if(currval==1) {
		eval(element).value=0;
	} else {
		eval(element).value=1;
	}
}

function changeHidden(){
	document.forms[0].elements[document.forms[0].elements.length-1].value = arguments[0].value;
}
var masterwin;
function doPopups() {
  if (!document.getElementsByTagName) return false;
  var links=document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("popup")) {
      links[i].onclick=function() {
      // Next two lines should be on one line
        masterwin = window.open(this.href, "win",
          "top=40,left=40,width=785,height=500");
        return false;
      }
    }
  }
}

function emailCheck (emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	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];
	
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			//alert("Ths username contains invalid characters.");
			return false;
		}
	}
	
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			//alert("Ths domain name contains invalid characters.");
			return false;
		}
	}

	if (user.match(userPat)==null) {
		// user is not valid
		//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 atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			//alert("The domain name does not seem to be valid.");
			return false;
		}
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 && 
		domArr[domArr.length-1].search(knownDomsPat)==-1) {
		//alert("The address must end in a well-known domain or two letter " + "country.");
		return false;
	}

	if (len<2) {
		//alert("This address is missing a hostname!");
		return false;
	}

	// If we've gotten this far, everything's valid!
	return true;
}


function runCheckAndSet(arg){
	clearTimeout(theTimeOut[arg]);
	theTimeOut[arg] = setTimeout("checkandset('"+arg+"')", 1500);
}

function checkandset(arg){
	clearTimeout(theTimeOut);
	var message;
	var element = document.getElementById(arg);
	var div = document.getElementById(arg + "_");
	var input = element.value;
	var errorImg = "common/img/aff_cross.gif";
	var passImg = "common/img/aff_tick.gif";
	var img = document.createElement("img");
	var failImg = passImg;
	var fail = false;
	while (div.firstChild) {
  		div.removeChild(div.firstChild);
	}
	if(input.length == 0){
		//alert(element.getAttribute("title"));
		failImg = errorImg;
		fail = true;
	}
	else{
		fail = false;
	}
	
	if(arg == "email"){
		if(!emailCheck(input)){
			failImg = errorImg;
			fail = true;
		}
		else{
			fail = false;
		}
	}
	
	if(!fail){
		if(checkArraySimple(arg, formCorrect)){
			formCorrect.push(arg);
		}
	}
	img.setAttribute("src", failImg);
	div.appendChild(img);
	clearTimeout(theTimeOut[arg]);
	
	if(formCorrect.length == formElements){
		document.getElementById("submit").disabled  = false;
		document.getElementById("submit").value  = "Please Submit";
	}
	//alert(input);
}

var theTimeOut = Array();
var formElements;
var formCorrect = Array();
function isValidEmail(str) {
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}


var encN=1;
// DECODES AND UNESCAPES ALL TEXT.
function decodeTxt(s){
	var s1=unescape(s.substr(0,s.length-1));
	var t='';
	for(i=0;i<s1.length;i++)t+=String.fromCharCode(s1.charCodeAt(i)-s.substr(s.length-1,1));
	return unescape(t);
}

// ENCODES, IN UNICODE FORMAT, ALL TEXT AND THEN ESCAPES THE OUTPUT
function encodeTxt(s){
	s=escape(s);
	var ta=new Array();
	for(i=0;i<s.length;i++)ta[i]=s.charCodeAt(i)+encN;
	return ""+escape(eval("String.fromCharCode("+ta+")"))+encN;
}

function launch(title, url, w, h, overlay_click_close, closeC) {
        var greybox = new GreyBox();
		greybox.overlay_click_close = overlay_click_close
        greybox.setDimension(w, h);
        greybox.setCenterWindow(true);
		greybox.overlay_click_close = closeC;
        return greybox.show(title, url)
}
