// JavaScript Document
// 0068 Durham Constabulary Intranet

// generic multi use pop-up with pre-programmed 'type'
newWin = null;
function openNewWindow(strURL, strType, strWidth, strHeight){
	if (newWin != null && !newWin.closed) newWin.close();
	var left = (screen.width/2) - strWidth/2;
  var top = (screen.height/2) - strHeight/2;
  if(strWidth>1024 || strHeight>736) {
    strWidth = 1024;
    strHeight = 736;
    left = 0;
    top = 0;
    strType = "max-img";
  }
	var strOptions="";
	if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
	if (strType=="console-scroll") strOptions="resizable,scrollbars,height="+strHeight+",width="+strWidth;
	if (strType=="max-img") strOptions="resizable,scrollbars,height="+strHeight+",width="+strWidth;
  if (strType=="fixed") strOptions="height="+strHeight+",width="+strWidth;
	if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,"+"resizable,location,height="+strHeight+",width="+strWidth;
	strOptions+=',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	newWin = window.open(strURL, 'newWin', strOptions);
	newWin.focus();	
	return true;
}

function openFileBrowser(doc_id)
{
	openNewWindow('/popups/filebrowse.php?id='+doc_id, 'console', 770, 258);
}

function doRefreshPage(a,b){

	window.location.href=a+b;
}

// pop-up for opening news and project images
var popwin = null;
function popImage(imagesrc,winwidth,winheight,alt){
	var left = (screen.width/2) - winwidth/2;
 	var top = (screen.height/2) - winheight/2;
 	var look='width='+winwidth+',height='+winheight+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	
 	if(winwidth>1024 || winheight>736) {
		winwidth2 = 1024;
		winheight2 = 736;
		left = 0;
		top = 0;
		look="resizable,scrollbars,height="+winheight2+",width="+winwidth2+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	  }
 
	if (popwin != null && !popwin.closed) popwin.close();
	popwin=window.open("","imgWin",look);
	popwin.document.open();
	popwin.document.write('<html>\n<head>\n');
	popwin.document.write('<title>'+alt+'</title>\n');
	popwin.document.write('<style type="text/css">\n');
	popwin.document.write('body { margin:0px; background-color:#e7e7e7; background-image:url(../images/loading.gif);	background-repeat:no-repeat; background-position:center center; }\n');
	popwin.document.write('</style>\n');
	popwin.document.write('</head>\n<body marginwidth="0" marginheight="0">\n');
	popwin.document.write('<a href="javascript:;" onClick="window.close()"><img src="'+imagesrc+'" alt="'+alt+'"  width="'+winwidth+'" height="'+winheight+'" border="0"></a>\n');
	popwin.document.write('</body>\n</html>');
	popwin.document.close();
}

// floating add post window in forum
// browser sniffing
IE5=NN4=NN6=false
if(document.all){IE5=true}
else if(document.layers){NN4=true}
else if(document.getElementById){NN6=true}
// utility
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
// MM dragLayer 
function MM_dragLayer(objName,x,hL,hT,hW,hH,toFront,dropBack,cU,cD,cL,cR,targL,targT,tol,dropJS,et,dragJS) { //v4.01
  //Copyright 1998 Macromedia, Inc. All rights reserved.
  var i,j,aLayer,retVal,curDrag=null,curLeft,curTop,IE=document.all,NS4=document.layers;
  var NS6=(!IE&&document.getElementById), NS=(NS4||NS6); if (!IE && !NS) return false;
  retVal = true; if(IE && event) event.returnValue = true;
  if (MM_dragLayer.arguments.length > 1) {
    curDrag = MM_findObj(objName); if (!curDrag) return false;
    if (!document.allLayers) { document.allLayers = new Array();
      with (document) if (NS4) { for (i=0; i<layers.length; i++) allLayers[i]=layers[i];
        for (i=0; i<allLayers.length; i++) if (allLayers[i].document && allLayers[i].document.layers)
          with (allLayers[i].document) for (j=0; j<layers.length; j++) allLayers[allLayers.length]=layers[j];
      } else {
        if (NS6) { var spns = getElementsByTagName("span"); var all = getElementsByTagName("div"); 
          for (i=0;i<spns.length;i++) if (spns[i].style&&spns[i].style.position) allLayers[allLayers.length]=spns[i];}
        for (i=0;i<all.length;i++) if (all[i].style&&all[i].style.position) allLayers[allLayers.length]=all[i]; 
    } }
    curDrag.MM_dragOk=true; curDrag.MM_targL=targL; curDrag.MM_targT=targT;
    curDrag.MM_tol=Math.pow(tol,2); curDrag.MM_hLeft=hL; curDrag.MM_hTop=hT;
    curDrag.MM_hWidth=hW; curDrag.MM_hHeight=hH; curDrag.MM_toFront=toFront;
    curDrag.MM_dropBack=dropBack; curDrag.MM_dropJS=dropJS;
    curDrag.MM_everyTime=et; curDrag.MM_dragJS=dragJS;
    curDrag.MM_oldZ = (NS4)?curDrag.zIndex:curDrag.style.zIndex;
    curLeft= (NS4)?curDrag.left:(NS6)?parseInt(curDrag.style.left):curDrag.style.pixelLeft; 
    if (String(curLeft)=="NaN") curLeft=0; curDrag.MM_startL = curLeft;
    curTop = (NS4)?curDrag.top:(NS6)?parseInt(curDrag.style.top):curDrag.style.pixelTop; 
    if (String(curTop)=="NaN") curTop=0; curDrag.MM_startT = curTop;
    curDrag.MM_bL=(cL<0)?null:curLeft-cL; curDrag.MM_bT=(cU<0)?null:curTop-cU;
    curDrag.MM_bR=(cR<0)?null:curLeft+cR; curDrag.MM_bB=(cD<0)?null:curTop+cD;
    curDrag.MM_LEFTRIGHT=0; curDrag.MM_UPDOWN=0; curDrag.MM_SNAPPED=false; //use in your JS!
    document.onmousedown = MM_dragLayer; document.onmouseup = MM_dragLayer;
    if (NS) document.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP);
  } else {
    var theEvent = ((NS)?objName.type:event.type);
    if (theEvent == 'mousedown') {
      var mouseX = (NS)?objName.pageX : event.clientX + document.body.scrollLeft;
      var mouseY = (NS)?objName.pageY : event.clientY + document.body.scrollTop;
      var maxDragZ=null; document.MM_maxZ = 0;
      for (i=0; i<document.allLayers.length; i++) { aLayer = document.allLayers[i];
        var aLayerZ = (NS4)?aLayer.zIndex:parseInt(aLayer.style.zIndex);
        if (aLayerZ > document.MM_maxZ) document.MM_maxZ = aLayerZ;
        var isVisible = (((NS4)?aLayer.visibility:aLayer.style.visibility).indexOf('hid') == -1);
        if (aLayer.MM_dragOk != null && isVisible) with (aLayer) {
          var parentL=0; var parentT=0;
          if (NS6) { parentLayer = aLayer.parentNode;
            while (parentLayer != null && parentLayer.style.position) {             
              parentL += parseInt(parentLayer.offsetLeft); parentT += parseInt(parentLayer.offsetTop);
              parentLayer = parentLayer.parentNode;
          } } else if (IE) { parentLayer = aLayer.parentElement;       
            while (parentLayer != null && parentLayer.style.position) {
              parentL += parentLayer.offsetLeft; parentT += parentLayer.offsetTop;
              parentLayer = parentLayer.parentElement; } }
          var tmpX=mouseX-(((NS4)?pageX:((NS6)?parseInt(style.left):style.pixelLeft)+parentL)+MM_hLeft);
          var tmpY=mouseY-(((NS4)?pageY:((NS6)?parseInt(style.top):style.pixelTop) +parentT)+MM_hTop);
          if (String(tmpX)=="NaN") tmpX=0; if (String(tmpY)=="NaN") tmpY=0;
          var tmpW = MM_hWidth;  if (tmpW <= 0) tmpW += ((NS4)?clip.width :offsetWidth);
          var tmpH = MM_hHeight; if (tmpH <= 0) tmpH += ((NS4)?clip.height:offsetHeight);
          if ((0 <= tmpX && tmpX < tmpW && 0 <= tmpY && tmpY < tmpH) && (maxDragZ == null
              || maxDragZ <= aLayerZ)) { curDrag = aLayer; maxDragZ = aLayerZ; } } }
      if (curDrag) {
        document.onmousemove = MM_dragLayer; if (NS4) document.captureEvents(Event.MOUSEMOVE);
        curLeft = (NS4)?curDrag.left:(NS6)?parseInt(curDrag.style.left):curDrag.style.pixelLeft;
        curTop = (NS4)?curDrag.top:(NS6)?parseInt(curDrag.style.top):curDrag.style.pixelTop;
        if (String(curLeft)=="NaN") curLeft=0; if (String(curTop)=="NaN") curTop=0;
        MM_oldX = mouseX - curLeft; MM_oldY = mouseY - curTop;
        document.MM_curDrag = curDrag;  curDrag.MM_SNAPPED=false;
        if(curDrag.MM_toFront) {
          eval('curDrag.'+((NS4)?'':'style.')+'zIndex=document.MM_maxZ+1');
          if (!curDrag.MM_dropBack) document.MM_maxZ++; }
        retVal = false; if(!NS4&&!NS6) event.returnValue = false;
    } } else if (theEvent == 'mousemove') {
      if (document.MM_curDrag) with (document.MM_curDrag) {
        var mouseX = (NS)?objName.pageX : event.clientX + document.body.scrollLeft;
        var mouseY = (NS)?objName.pageY : event.clientY + document.body.scrollTop;
        newLeft = mouseX-MM_oldX; newTop  = mouseY-MM_oldY;
        if (MM_bL!=null) newLeft = Math.max(newLeft,MM_bL);
        if (MM_bR!=null) newLeft = Math.min(newLeft,MM_bR);
        if (MM_bT!=null) newTop  = Math.max(newTop ,MM_bT);
        if (MM_bB!=null) newTop  = Math.min(newTop ,MM_bB);
        MM_LEFTRIGHT = newLeft-MM_startL; MM_UPDOWN = newTop-MM_startT;
        if (NS4) {left = newLeft; top = newTop;}
        else if (NS6){style.left = newLeft; style.top = newTop;}
        else {style.pixelLeft = newLeft; style.pixelTop = newTop;}
        if (MM_dragJS) eval(MM_dragJS);
        retVal = false; if(!NS) event.returnValue = false;
    } } else if (theEvent == 'mouseup') {
      document.onmousemove = null;
      if (NS) document.releaseEvents(Event.MOUSEMOVE);
      if (NS) document.captureEvents(Event.MOUSEDOWN); //for mac NS
      if (document.MM_curDrag) with (document.MM_curDrag) {
        
		if (typeof MM_targL =='number' && typeof MM_targT == 'number' &&
            (Math.pow(MM_targL-((NS4)?left:(NS6)?parseInt(style.left):style.pixelLeft),2)+
             Math.pow(MM_targT-((NS4)?top:(NS6)?parseInt(style.top):style.pixelTop),2))<=MM_tol) {
          if (NS4) {left = MM_targL; top = MM_targT;}
          else if (NS6) {style.left = MM_targL; style.top = MM_targT;}
          else {style.pixelLeft = MM_targL; style.pixelTop = MM_targT;}
          MM_SNAPPED = true; MM_LEFTRIGHT = MM_startL-MM_targL; MM_UPDOWN = MM_startT-MM_targT; }
		  
        if (MM_everyTime || MM_SNAPPED) eval(MM_dropJS);
        if(MM_dropBack) {if (NS4) zIndex = MM_oldZ; else style.zIndex = MM_oldZ;}
        retVal = false; if(!NS) event.returnValue = false; }
      document.MM_curDrag = null;
    }
    if (NS) document.routeEvent(objName);
  } return retVal;
}
// layer position control
function minimise(){
		OBJ=MM_findObj('newpost');
		OBJ.style.left='0px'; OBJ.style.top='0px';
}
function closePopUp(id)
{
	OBJ=MM_findObj(id);
	OBJ.style.visibility = 'hidden';
}
function maximise(id, cont)
{
	// find layer position in page
	if(IE5||NN6) myObj=MM_findObj(id);

	contObjY=0;
	contObjX=0;
	Scrolled=document.body.scrollTop;
	h=myObj.offsetHeight;
	w=myObj.offsetWidth;
	
	if(cont)
	{
		contObj=MM_findObj(cont);
		while(contObj.offsetParent)
		{
			contObjY += contObj.offsetTop;
			contObjX += contObj.offsetLeft;
			contObj = contObj.offsetParent;
		}
	}

	// compute center coordinate
	if(IE5){
		yc=Math.round((document.body.clientHeight/2)-(h/2)-contObjY)+Scrolled;
		xc=Math.round((document.body.clientWidth/2)-(w/2)-contObjX);
	}
	else if(NN6){
		yc=Math.round((window.innerHeight/2)-(h/2)-contObjY)+Scrolled;
		xc=Math.round((window.innerWidth/2)-(w/2)-contObjX);
	}
	
	// reposition div
	if(IE5||NN6){
		myObj.style.top = yc + "px";
		myObj.style.left = xc + "px";
	}
	
	// show element
	myObj.style.visibility = 'visible';
}

// basic initiate script
function init(){
	MM_findObj('newpostContainer').style.height = MM_findObj('newpost').offsetHeight;
	MM_dragLayer('newpost','',0,0,400,30,true,false,-1,-1,0,-1,0,0,150,'',false,'');
	//objName,x,hL,hT,hW,hH,toFront,dropBack,cU,cD,cL,cR,targL,targT,tol,dropJS,et,dragJS
}

// not used yet
function setOptionText(elm, options_array)
{
	for(var i=0; i<options_array.length; i++)
	{
		elm.options[i].value = (i+1);
		elm.options[i].text = options_array[i];
	}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

// confirm delete
function confirmDelete(location)
{
	if(confirm('Are you sure you wish to delete this item?')){
		location.href = location;
	} else {
		return false;
	}
}
// form validation //
// Function that returns true if a string contains only white space
function isblank(s) {
	for (var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != " ") && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}
// Function to check text has been entered in a field
function chkField(e) {
	if ((e.value == null) || (e.value == "") || isblank(e.value) || (e.value.length == 0 )) {
		return false;
	}
	else {
		return true;
	}
}

function verify(f) {
	var empty_fields = "";
	var msg = "";
	//Verify required fields
	if (!(chkField(f.firstname))) empty_fields += "\nFirstname";
	if (!(chkField(f.lastname))) empty_fields += "\nSurname";
	if (!(chkField(f.telephone))) empty_fields += "\nTelephone";
	if (!(chkField(f.email))) empty_fields += "\nEmail Address";
	if (chkField(f.email)) {
		if ((f.email.value.indexOf("@") < 0) || (f.email.value.indexOf(".") < 0)) {
			empty_fields += "\nEmail Address Formatted Correctly";
		}
	}
	if (!(chkField(f.position))) empty_fields += "\nPosition/Rank";
	//if (f.area.selectedIndex<1) empty_fields += "\nDistrict";
	if (f.area.value>=3 && f.briefing_doc_id.selectedIndex<1) empty_fields += "\nPreferred Briefing Document";
	if (f.area.value<3 && f.district.selectedIndex<1) empty_fields += "\nDistrict";
	// If any errors display the messages and return false to prevent form submission
	if (empty_fields) {
	msg = "===================================\n"
	msg += " Before you can continue please ensure\n";
	msg += " you have entered values in these fields.\n";
	msg += "===================================\n"
	msg += empty_fields + "\n";
	msg += "\nCorrect the field(s) and try again.\n";
	msg += "===================================\n"
		alert(msg);
		return false;
	}
	else {
		return true;
	}
}

function verifyUser(f) {
	var empty_fields = "";
	var msg = "";
	//Verify required fields
	if (!(chkField(f.username))) empty_fields += "\nUsername";
	if (!(chkField(f.passwd))) empty_fields += "\nPassword";
	// If any errors display the messages and return false to prevent form submission
	if (empty_fields) {
	msg = "===================================\n"
	msg += " Before you can continue please ensure\n";
	msg += " you have entered values in these fields.\n";
	msg += "===================================\n"
	msg += empty_fields + "\n";
	msg += "\nCorrect the field(s) and try again.\n";
	msg += "===================================\n"
		alert(msg);
		return false;
	}
	else {
		return true;
	}
}

var n = 0;
function pagesearch(str) {

  if(document.forms[0].where[0].checked) return true;
  
  var txt, i, found;

  if (str == '')
    return false;

  // Find next occurance of the given string on the page, wrap around to the
  // start of the page if necessary.

  if (document.all) {
    txt = document.body.createTextRange();

    // Find the nth match from the top of the page.

    for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
      txt.moveStart('character', 1);
      txt.moveEnd('textedit');
    }

    // If found, mark it and scroll it into view.

    if (found) {
      txt.moveStart('character', -1);
      txt.findText(str);
      txt.select();
      txt.scrollIntoView();
      n++;
    }

    // Otherwise, start over at the top of the page and find first match.

    else {
      if (n > 0) {
        n = 0;
        return pagesearch(str);
      }

      else
        alert('Not found.');
    }
  }

  return false;
}
///
///
///
function openSendMailWindow(url)
{
	openNewWindow(url,'console-scroll',545,160);
}
///
///
///
function SubmitSearchForm(e)
{
	var keynum;
	var keychar;
	
	if(window.event) // IE
	{
		keynum = e.keyCode
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which
	}

	keychar = String.fromCharCode(keynum);
	
	if(keychar == '\r')
	{
		document.searchForm.submit();
	}
}
