// direcory category
function loadCategory(which) {
	var ids="";
	for(i=0;i<document.directoryCategorys.DirectoryCategoryIDs.length;i++) {
		if(document.directoryCategorys.DirectoryCategoryIDs[i].checked==true) {
			ids=ids+document.directoryCategorys.DirectoryCategoryIDs[i].value+",";
		}
	}
	$("#news").load("/ajax_directory.cfm?cids="+ids+"&letter="+which);
}

// directory company
function loadDirectoryCompany(which) {
	$("#dir"+which).load("/ajax_directory.cfm?id="+which);
}

// news ajax selector
function toggleSection(which) {
	$("#rc"+which).slideToggle("fast");
}

// select all section category ids
function selectAll(which) {
	for(i=0;i<document.getElementById("cs"+which).value;i++) {
		if(document.getElementById("rc"+which+"s"+parseInt(i+1)).checked==true) {
			document.getElementById("rc"+which+"s"+parseInt(i+1)).checked=false;
		} else {
			document.getElementById("rc"+which+"s"+parseInt(i+1)).checked=true;
		}
	}
}

// reload news articles
function updateNews(query) {
	var theIDs="";
	for(i=0;i<document.refineForm.SectionIDs.length;i++) {
		if(document.refineForm.SectionIDs[i].checked==true) {
			theIDs=theIDs+document.refineForm.SectionIDs[i].value+",";
		}
	}
	if(theIDs!="") {
		$("#nextprev").hide("fast");
		$("#loading").show("fast");
		$.get("/_news.cfm?q="+query+"&ids="+theIDs, function(data) {
			document.getElementById("news").innerHTML=data;
			$("#loading").hide("fast");
		});
	}
}

// logout
function confirmLogout() {
	if(confirm("Are you sure you want to logout?")) {
		document.location.href="/_login.cfm?logout";
	}
}
// view an image in a dynamic popup
function viewSpecifiedImage(which) {
	var x = window.open('','pictureViewer','width=800,height=800,resizable=1');
	if( !x ) { return true; }
	x.document.open();
	x.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html><head><title>Picture Viewer<\/title>');
	x.document.write('<sc'+'ript type="text/javascript">');
	x.document.writeln('var isNN,isIE;');
	x.document.writeln('function reSizeToImage(){');
	if (navigator.appName.indexOf("Microsoft")==-1)
	{
		x.document.writeln('window.innerWidth=document.images["ex_popimage"].width;');
		x.document.writeln('window.innerHeight=document.images["ex_popimage"].height;');
		x.document.writeln('document.title="Picture Viewer";');
	}else if(window.opera){
		x.document.writeln('window.resizeTo(100,100);');
		x.document.writeln('width=130-(document.body.clientWidth-document.images[0].width);');
		x.document.writeln('height=100-(document.body.clientHeight-document.images[0].height);');
		x.document.writeln('window.resizeTo(width,height);');
	}else{
		x.document.writeln('window.resizeTo(100,100);');
		x.document.writeln('width=100-(document.body.clientWidth-document.images[0].width);');
		x.document.writeln('height=100-(document.body.clientHeight-document.images[0].height);');
		x.document.writeln('window.resizeTo(width,height);');
	}
	x.document.writeln('}<\/sc'+'ript>');
	x.document.write('<\/head><body style="margin:0px;" id="ex_popbody" onload="reSizeToImage();self.focus();">');
	x.document.write('<img id="ex_popimage" name="ex_popimage" src="'+which+'" onclick="window.close();" alt="" \/><\/body><\/html>');
	x.document.close();
	if( x.focus ) { x.focus(); }
}

// Window opening
function winOpenCenter(url,name,statbar,scroll,locate,resize,xWidth,yWidth)	{
var adjustedleft = 8//optional
var adjustedheight = 30//adjust height because of windows taskbar
var screenwidthremainder = screen.availWidth%2//really not needed, but won't hurt
var screenheightremainder = screen.availHeight%2
var screenwidth = screen.availWidth - screenwidthremainder
var screenheight = screen.availHeight - screenheightremainder
var winheight = yWidth //set new window height properties
var winwidth = xWidth //set new window width properties
var winleft = parseInt(screenwidth/2) - (winwidth/2) - adjustedleft//optional
var wintop = parseInt(screenheight/2) - (winheight/2) - adjustedheight

var win = window.open(url,name,'width=' +winwidth+ ',height=' +winheight+',status=' +statbar+',resizable='+resize+',scrollbars='+scroll+',location='+locate+',top='+wintop+',left='+winleft);
return win;
}

// Email Checker
function ValidateEmailAddress(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) {
		var textObj =document.getElementById("Email"); 
		alert("Please enter a valid email address.")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
		var textObj =document.getElementById("Email"); 
	    alert("Email address username is not valid.")
	    return false
	}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
				var textObj =document.getElementById("Email"); 
		        alert("Email address destination IP is invalid.")
			return false
		    }
	    }
	    return true
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		var textObj =document.getElementById("Email"); 
		alert("Email address 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) {
		var textObj =document.getElementById("Email"); 
		alert("Email address must end in a three-letter domain, or two letter country.")
		return false
	}
	if (len<2) {
		var textObj=document.getElementById("Email"); 
		var errStr="Email address is missing a hostname."
		alert(errStr)
		return false
	}
	return true;
}
