
function xtrim(field) {
field.value=field.value.replace(/^\s*|\s*$/g,'')
/*while(''+field.value.charAt(0)==' ')field.value=field.value.substring(1,field.value.length);
while(''+field.value.charAt(field.value.length-1)==' ')field.value=field.value.substring(0,field.value.length-1);
field.value=field.value.replace(/^\n/g,""); */ 
}

function xtrimnum(field) {
xtrim(field)
var valid = "0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Numeric Value Only");
field.style.backgroundColor=bnkColor;
field.select();
   }
}

function xtrimphone(field) {
xtrim(field)
var valid = "0123456789+-."
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Numeric Values and + - . Only");
field.style.backgroundColor=bnkColor;
field.select();
   }
}

function xtrimcost(field) {
xtrim(field)
var valid = "0123456789$.,"
var msg="Valid Dollar Amount Only ($23.95)";
var ok = "yes";
var temp;
var comma=",";
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "yes" && field.value.indexOf(",") != "-1") {
var x = field.value.replace(/\','|\,/g,''); 
msg = "Do Not use a comma! ("+x+")"; 
ok = "no";
}
if (ok == "no") {
alert(msg);
field.style.backgroundColor=bnkColor;
field.value=field.value.replace(/\','|\,/g,'');
field.select();
   }
}

function validatesp(field) {
xtrim(field)
if(field.value.length > 0) {
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
	var ok = "yes";
	var temp;
	for (var i=0; i<field.value.length; i++) {
	temp = "" + field.value.substring(i, i+1);
	if (valid.indexOf(temp) == "-1") ok = "no"; 
	}
	var msg = "";
	var cnt=0;
	if (ok == "no") {
		cnt=cnt+1;
		msg=msg + ""+cnt+") Variable Names can only contain letters, numbers, and the underscore character.\n";
	   }
	
	if (!isNaN(field.value.substring(0,1)) || field.value.substring(0,1)=='_') {
		cnt=cnt+1;
		msg=msg + ""+cnt+") Each Variable Name must begin with a letter.";
	   }
	if(cnt>0) {
		alert(msg);
		field.style.backgroundColor=bnkColor;
		field.focus();
	}
}
}

function clic(field1,field2) {
if (field2.value=="") {
field2.focus()
}
if (field1.value=="") {
field1.focus()
}
if (field2.value!="" && field1.value!="") {
window.focus()
}
}

function ckAll(field1,field2) {
	if(field1.checked == true) {
		ck = true;
		cn = 'selected';
		field1.title='Uncheck All Messages';
	}
	else {
		ck = false;
		cn = 'altrow';
		field1.title='Check All Messages';
	}
	for (i = 0; i < field2.length; i++) {
		field2[i].checked = ck;
		document.getElementById('TR'+i).className=cn+(i % 2);
		}
}

function ckThis(f1,f2) {
	if(f1.checked==true) {
		cn = 'selected';
	}
	else {
		cn = 'altrow';
	}
	document.getElementById('TR'+f2).className=cn+(f2 % 2);
}

function trashTitle(field1,field2) {
	document.getElementById(field2).title=field1[field1.selectedIndex].text + '\n[Selected Messages]';	
}

var seleOpts="";
var allSelected=false;
function storeOld(field1,field2){
     seleOpts="";
     for(var i=0;i<field1.options.length;i++){
          if(field1.options[i].selected) 
		  	seleOpts+=i+",";
     }
     seleOpts=seleOpts.substring(0,seleOpts.length-1);
     if(field1.options.length==seleOpts.split(",").length){
          field2.checked=true;
          allSelected=true;
     }
else{
          allSelected=false;
          field2.checked=false;
     }
}

function selAll(field1,field2,SE,se,DE,de,field3) {
var se = se.split(',');	
var de = de.split(',');
var optOld=seleOpts.split(',');	
for (var i = 0; i < field1.options.length; i++)
	if(field2.checked == true) {
		if(field1.options[i].value) {
			field1.options[i].selected = true;
		}
		if(DE) {			
			for (var j = 0; j < de.length; j++)
			field1.options[de[j]].selected = false;		
		}
	}
	else {	
	field1.options[i].selected = false;
		if(SE) {			
			for (var j = 0; j < se.length; j++)
			field1.options[se[j]].selected = true;		
		}
	}
	
			if(!allSelected){
                    for(var i=0;i<field1.options.length;i++) {
                         for(j=0;j<optOld.length;j++) {
                              if(parseInt(optOld[j])==i) field1.options[i].selected=true;
                         }
                    }
               }
	if(field3 != null) {
	return selSame(field1,field3,0)
	}
	
}

function selSame(field1,field2,o) {
var temp="";
var optOld=seleOpts.split(',');	
	if(field2.checked == true) {
		for (var i = 0; i < field1.options.length; i++) {	
			if(field1.options[i].selected == true) {
				temp = temp + ',' + field1.options[i].text;
			}
		}

		if(temp.length) {
			
			temp = temp.split(',');
			for (var j = 0; j < temp.length; j++) {
				for (var i = 0; i < field1.options.length; i++) {
					if(field1.options[i].text == temp[j]) {
						field1.options[i].selected = true;
					}			
				}
			}					
		}
	}
	else if(o==1) {
		for(var i=0;i<field1.options.length;i++) {
			field1.options[i].selected=false;
			for(j=0;j<optOld.length;j++) {
				if(parseInt(optOld[j])==i) field1.options[i].selected=true;
				// select inverse by using !=
			}
		}
	}
}

function checkSearch(field1,field2,tf) {
var checkok = true;
var msg = "";
var f1 = false;
var f2 = false;
var cnt = 1;
	for (var i = 0; i < field1.options.length; i++)	{
		if(field1.options[i].selected == true) {
			f1 = true;			
		}
	}
	
	if(!f1) {
	checkok = false;
	field1[0].style.backgroundColor=bnkColor;
	msg = cnt + ") You must select an area to search.\n";
	cnt = cnt + 1;
	}
	if(tf == 1) {
		if(field2.value.length > 3) {
			f2 = true;		
		}
	msg = msg + cnt + ") Search Text must be 4 or more characters to perform a text search.";
	}
	else {	
		if(field2.value.length == 0 || field2.value.length > 3) {
			f2 = true;		
		}
	msg = msg + cnt + ") Search Text can be blank - which will then display listings based on other selections\n  or it must be 4 or more characters to perform a text search.";
	}
	
	if(!f2) {
	checkok = false;
	field2.style.backgroundColor=bnkColor;
	field2.focus();
	}
	else {
	field2.style.backgroundColor=reqColor;
	}
		
	if(!checkok) {
	alert(msg);
	return false;
	}	
}

function si(f) {
for (i=0; i<f.options.length; i++) {
	if(!f[i].value) {
	f[i].selected=false;	
	}
}
}

function colorc(field1,field2) {
	for(i = 0; i < field1.options.length; i++) {
		field1[i].style.backgroundColor='White';
		}		
	tcolor=field2[field2.selectedIndex].value;		
		if(field1[field1.selectedIndex].value == field2[field2.selectedIndex].value) {
			if(field1[field1.selectedIndex].value == 'White') {
				tcolor='Black';
				field2.value='Black';
			}
			else {
				tcolor='White';
				field2.value='White';
			}
		}
		field1[field1.selectedIndex].style.backgroundColor=tcolor;	
		window.focus()
}

function gettDate(field1,field2,field3) {
	var dlist = field1.value + ' ' + field2.value + ' ' + field3.value
	return dlist
}

function gdim(field1,field2,field3,field4){
     year = parseInt(field3.options[field3.selectedIndex].value);
     month = field2.selectedIndex;
     day = field1.selectedIndex;
     field1.options.length = 0;
     var days = new Array(31, ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
     for(i = 0; i < days[month]; i++)
     {
       field1.options.length = field1.options.length + 1;
       field1.options[i].value = i + 1;
       field1.options[i].text = i + 1;
     }	  
  if(field4 && field4.value==111) {
  field1.selectedIndex = field1.options.length-1 ;
  }
  else {
  field1.selectedIndex = (day < field1.options.length) ? day : field1.options.length - 1;
  }	
}  

//date,document.Form
function dateset(field1,field2,f3) {
list = field1.split("/");	
	field2.m.value=list[1];
	field2.y.value=list[2];
	gdim(field2.d,field2.m,field2.y);
	field2.d.value=list[0];
	gdim(f3,field2.m,field2.y);
	window.focus()	
}
//tempTime"h:mm tt", document.Form, document.Form.schedCat, 'cat'
function timeset(field1,field2,field3,field4) {
if(field1.length) {
	list = field1.split(":");	
	//alert(list)
	field2.hh.value=list[0];
	field2.mm.value=list[1];
}

if(field4.length) {
field3.value=field4;
}
	window.focus()
}

function blank(field1,field2,field3,field4,field5,field6,field7,field8,field9) {
var ok = true;
for (var i=1; i <= 9; i++) {
field = eval('field'+i);
	if(field!=null) {
		if(field.value.length==0) {
			ok = false;
			field.style.backgroundColor=bnkColor;
			field.focus();
			return false;
		}
		else {
			field.style.backgroundColor=reqColor;
		}
	}
}
if(!ok) {
return false
}
}
	
function delThis(val,msg) {
var agree=confirm(msg);
if (agree) {
	if(delok) {
		location.replace(''+delpage+'?action=delete&field='+val+'')	
	}
	else {
		alert("Sorry - you cannot delete in this demo");	
		window.focus()
		return false	
	}
}
else {
	window.focus()
	return false
	}
}

//cID,cDate,view
function delCal(val1,val2,val3,val4,val5) {
var agree=confirm("Permanently Delete?");
if (agree) {
	if(delok) {
		location.href="caldb.cfm?action=Delete&cID="+val1+"&cDate="+val2+"&view="+val3+"&calPage="+val4+"&row="+val5+""	
	}
	else {
		alert("Sorry - you cannot delete in this demo");	
		window.focus()
		return false	
	}
}
else {
	window.focus()
	return false
	}
}

function delCat(val,msg,page) {
var agree=confirm(msg);
if (agree)
location.replace(''+page+'?action=Delete&field='+val+'')	
else return false
}

function sblank(field) {
if (field.value=="") {
field.style.backgroundColor=bnkColor;
field.focus()
return false
}
else {
/*xwin=window.open(field,win3,"height=400,scrollbars=yes,width=800,status=no,toolbar=no,menubar=no,location=no");
xwin.focus();*/
document.sForm.submit();
}
}


//d,recur
function getr(field1,field2) {
	if(field2.value==111) {
	field1.selectedIndex=field1.options.length-1;
	}
	window.focus()
	}
	
function blanksearch(field1,box) {
var checkit=false;
	if(field1.value.length < 3) {
		checkit=true;
		if(box != null && box.checked==true) {
			checkit=false;
			}
		}
	if(checkit) {
	alert("Enter Search Term (3 char min)");
	field1.style.backgroundColor=bnkColor;
	field1.focus();
	return false;
	}	
}

function getPreviewAll(c,img,elem) {	
var d = 'inline';
var e = '[Collapse All]';
var j = collapseimg;
var v = 1;
if(document.getElementById('ExAll').innerHTML==e) {
var d = 'none';
var e = '[Expand All]';
var j = expandimg;
var v = 0;
}
	for (var i = 1; i <= c; i++) {
		document.getElementById(elem+i).style.display=d;	
	}	
	for (var i = 1; i <= c; i++) {		
		document.getElementById(img+i).src=j;
	}	
	
	document.getElementById('ExAll').innerHTML=e;
	document.getElementById('ExAll').value=v;
}

var preq='';
function getPreview(c,d,img,elem) {	
if(document.getElementById(elem+c).style.display=='inline') {
preq = c;
}
	if(d=='none') {
	i=expandimg;
	}
	else {
	i=collapseimg;
	}
if(c!=preq) {
	document.getElementById(elem+c).style.display='inline';
	document.getElementById(img+c).src=i;
	if(preq.length) {
		document.getElementById(elem+preq).style.display='none';
		document.getElementById(img+preq).src=expandimg;	
	}
	preq=c;
}
else if(c==preq) {
	document.getElementById(elem+preq).style.display='none';
	document.getElementById(img+preq).src=expandimg;	
	preq='';
}		
}	

function schedPreview(thisDate) {	
xwin=window.open(iToolsPath+'/isched/home.cfm?showHeaders=false&thisDate='+thisDate+'#main',win3,"height=400,scrollbars=yes,width=800,status=no,toolbar=no,menubar=no,location=no");
xwin.focus();

}

function iReflow(pattern, list){
  if (!list.bak){   
    list.bak = new Array();
    for (n=0;n<list.length;n++){
      list.bak[list.bak.length] = new Array(list[n].value, list[n].text);
    }
  }
  match = new Array();
  nomatch = new Array();
  for (n=0;n<list.bak.length;n++){
    if(list.bak[n][1].toLowerCase().indexOf(pattern.toLowerCase())==0){ 
      match[match.length] = new Array(list.bak[n][0], list.bak[n][1]);
    }else{
      nomatch[nomatch.length] = new Array(list.bak[n][0], list.bak[n][1]);
    }
  }
  for (n=0;n<match.length;n++){
    list[n].value = match[n][0];
    list[n].text = match[n][1];
  }
  for (n=0;n<nomatch.length;n++){
    list[n+match.length].value = nomatch[n][0];
    list[n+match.length].text = nomatch[n][1];
  } 
  list.selectedIndex=0;
}

function addFav(img,id,favname,favpage,favtext) {
if(document.getElementById(img).src.indexOf(expandimg)>=0) {
	var msg = 'Remove from ' + favname;
	document.getElementById(img).value=1;
	var j = collapseimg;
	var a = 'none';
}	
else {
	var msg = 'Add to ' + favname;
	document.getElementById(img).value=0;
	var j = expandimg;
	var a = 'delete';
}	
	document.getElementById(img).src=j;		
	document.getElementById(id).innerHTML=msg;
	hide.location.replace(iToolsPath+'/shared/addfavorites.cfm?favpage='+favpage+'&favtext='+favtext+'&action='+a+'');
} 

function getHelp(elem) {	
	var obj = document.getElementById(elem);
	if(obj != null) {
	var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
	hide.location.replace(iToolsPath+'/ihelp/ihelp.cfm?curleft='+curleft+'&curtop='+curtop+'&pathname='+window.location.pathname+'');
	}
}

function emailCheck(field,msg) {
xtrim(field)
var emailStr = field.value;
if(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(msg)
	field.style.backgroundColor=bnkColor 
	field.focus()
	return false
}
var user=matchArray[1]
var domain=matchArray[2]
if (user.match(userPat)==null) {
    alert(msg)
	field.style.backgroundColor=bnkColor
	field.focus()
    return false
}
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert(msg)
			field.style.backgroundColor=bnkColor
			field.focus()
		return false
	    }
    }
    return true
}
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert(msg)
	field.style.backgroundColor=bnkColor
    field.focus()
    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(msg)
   field.style.backgroundColor=bnkColor
   field.focus()
   return false
}
if (len<2) {  
   alert(msg)
   field.style.backgroundColor=bnkColor
   field.focus()
   return false
}
// If we've gotten this far, everything's valid!
return true;
}
}

function fileType(Frm,doblank) {
var ok = true;
var f = "";
var cnt = 0;
	for (var i=1; i <= uploadnum; i++) {
	field = eval(Frm + '.AttachFile' + i)	
		if(field!=null && field.value.length) { 
			dots = field.value.split(".")
			thisType = "." + dots[dots.length-1]
			thisType = thisType.toLowerCase()		
			if(Extensions.toLowerCase().indexOf(thisType) == "-1") {
			ok = false;
			f = f + " & " + field.name;
			field.style.backgroundColor=bnkColor;	
			cnt = cnt+1;		
				}
		}
	}
	
	var ext = Extensions.split(',');	
	if(ext.length > 1) {
	var t = "";
		for (var i=0; i < ext.length; i++) {
			t = t + ext[i];
			if(i < ext.length-1) {
			t = t +  ' & ' ;
			}
		}
	ext = t;
	}	
	
	f = f.replace(/ & /,'');	
	if(cnt == 1) {
	var msg="File Type in " + f + " is Not Allowed.\nUse " + ext + " Only!";
	}
	if(cnt > 1) {
	var msg="File Types in " + f + " are Not Allowed.\nUse " + ext + " Only!";
	}
	
	if(!ok) {
	alert(msg)
	return false
	}	
	
	if(doblank!=null && doblank.length) {
		return blank(eval(doblank))
	}
}

function focusFCK(caller, tabTargetId, callEvent)
{
	if(callEvent.keyCode == 9 && !callEvent.shiftKey)
	document.getElementById(tabTargetId + '___Frame').contentWindow.focus();
}

function FCKeditor_OnComplete( editorInstance ) {
if(isEditing) {
	try{		
		document.getElementById(editorInstance.Name + '___Frame').contentWindow.focus();
		}	
	catch(e){}
	}
}

if(document.getElementById('wait') != null) {
	document.getElementById('wait').style.visibility = 'hidden';
}
