// Global methods used on EVERY page
// Do not put anything here that is not global
// Popup methods open new browser windows
var globalPopup;
function openPopup(url) {
closeWin(globalPopup);
var globalPopup = window.open(url,"pop_win","height=400,width=500,location=no,menubars=no,scrollbars=yes,toolbars=no,resizable=yes");
}
// Close window if needed
function closeWin(winRef) { 
if (winRef)
if (!winRef.closed) winRef.close();
return null;
}
// Open window to given size
function openPopupToSize(url, width, height) {
closeWin(globalPopup);
globalPopup = window.open(url,"pop_win","height="+height+",width="+width+",location=no,menubars=no,scrollbars=yes,toolbars=no,resizable=yes");
}
// Open help window convenience method
function openHelpPopup(url) {
openPopupToSize(url, 790, 661);
}
// End popup methods 
// Use this (please!) whenever injecting user input into a page.
function escapeXml(s) {
return s.toString().replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#39;");
}
//function to replace all the line breaks with br tags -for user textarea input
function addLineBreaks(s) {
return s.toString().replace(/\n/g, "<br/>");
}
//function to truncate string to x number of chars and add .. to the end
function truncate(st, charNum, appendThis) {
if(st == null || st.length <= charNum)
return st;
appendThis = appendThis? appendThis : "...";
return st.substring(0, charNum) + appendThis;
}
//function to validate for extended characters 
function validateForExtendedCharacters(st) {
if(st == null)
return st;
var re = new RegExp("^[\-!#$%&()*+,./0-9:;<=>?@A-Z\\[\\\\\\]^_\'a-z{|}~\\s]*$");
return st.match(re)
}
////////////////////////////////////////////////////////////////////////////////
// The following two methods are now global, because
// they're used in the Getting to Know You module in the footer
function trim(s) {
// trim leading and trailing "whitespace"
var whitespace = " \t\n\r";
var i = 0;
while ((i < s.length) && (whitespace.indexOf(s.charAt(i)) != -1)) i++;
var j = s.length;
while ((j > i) && (whitespace.indexOf(s.charAt(j-1)) != -1)) j--;
return s.substr(i,j - i);
}
function isEmailValid(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) return false;
var user=matchArray[1]
var domain=matchArray[2]
if (user.match(userPat)==null) return false;
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
for (var i=1;i<=4;i++) {
if (IPArray[i]>255) return false;
}
return true;
}
var domainArray=domain.match(domainPat)
if (domainArray==null) 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>4) return false;
if (len<2) return false;
return true;
}
// display clickable image thumbnail in the specified div id.
function showPreview(url,imageURL,targetDiv, title){
document.getElementById(targetDiv).innerHTML="<a href='" + url + "' title='" + title + "'>" + "<img src='" + imageURL + "'/></a>";
}
