/*====================================================================================================
Global Javascript - used by the majority of forms in the application
====================================================================================================*/
window.onbeforeunload = checkWindowUnload;
var isDirty = false;
var currentView;
var ctrlFlg=false;
var ctrlFlgIE=true;
var altFlg=false;
var yesToStartEdit; 
var msgDisplay=0;
var noRequired=true;

function testpresence()
{
    alert('What - don\'t you trust me?  I\'m still here!');
}

function navFocus() {
	var el = document.getElementById('lstView');
	if (arguments.length > 0) {
		if (arguments[0] != '') {
			el.value = arguments[0];
			currentView = arguments[0];
		}
	}

	if (el) {
		el.focus();
	}
	
}

function chkValid(fOK){

    for (var i=0; i<document.form1.elements.length; i++)
	{	
	
	    if (document.form1.elements[i].attributes['required'] != undefined) {

			var iReq=document.form1.elements[i].attributes['required'].value;
			switch(iReq)
			{
				case "1":
				//	alert(document.form1.elements[i].type);
					if (document.form1.elements[i].type == "select-one")
					{ // alert(document.form1.elements[i][document.form1.elements[i].selectedIndex].text);
						if(document.form1.elements[i].value == '' || document.form1.elements[i][document.form1.elements[i].selectedIndex].text == "Select One")
						{
							var sMsg = document.form1.elements[i].attributes['invalidMsg'].value;
							alert (sMsg);
							fOK=0;
							document.form1.elements[i].focus();
							break;
						}
					}
					else if (document.form1.elements[i].value == '')
					{
						var sMsg = document.form1.elements[i].attributes['invalidMsg'].value;
						alert (sMsg);
						fOK=0;
						document.form1.elements[i].focus();
						break;
					}
			}
		}
		if (!fOK){break;}	
	}
return fOK;
}

function doCommand(s) {

	var Opera = (navigator.userAgent.indexOf("Opera")!=-1);
	var MSIE = (navigator.appVersion.indexOf("MSIE")!=-1);
	var Moz = (navigator.userAgent.indexOf("Mozilla/5")!=-1);
	var el = document.getElementById('__mfcCommand');
	var f = document.getElementById('form1');
	var isSave = false;
	var isDelete = false;
	var fOK = true;
	//determine if 'save' is a part of the command...  if so, ignore isDirty variable	
	if (s.split('save').length > 1) {isSave = true}
	if (s.split('delete').length > 1) {isDelete = true}
	//if (s == 'undo') {isDirty = false;}
	if (s.split('undo').length > 1) {isDirty = false;}
	
	if (arguments.length > 1) {
		for (var a=1; a < arguments.length; a++) {
		    var elName = '__mfcArgument' + a;
            var elArg = document.getElementById(elName);
		    if (elArg)
		    {
		        elArg.value = arguments[a];
		    }
		}
	}
	
	if (s == 'View' | s == 'view') {		
		var tab = arguments[1];		
		if ((tab.toString()).length > 0) {			
			if (tab < 10000) {
				changeView(tab,1); 
				fOK = false;
			} else {
				fOK = false;
			}
		}
	}
	if (isDelete) {
		fOK = window.confirm('Are you sure you wish to delete this record?');
	}
	
	if (isSave) {
	    
		if (self.doValidate) {
			fOK = doValidate();
		} else { 
			fOK = true;
		}
		if (fOK) { isDirty = false }
		
		////////////////
		fOK = chkValid(fOK);
	
	}

	/// fOK = chkValid(fOK);
	if (fOK) {

        showDialogBg('commandBg'); 
        showPanel('commandWait');

//		if (arguments.length > 1) {
//			for (var a=1; a < arguments.length; a++) {
//				s = s + ',' + arguments[a];
//			}
//		}

		el.value = s;
	
		f.submit();
	}
	
}

function setCommand(s) {

	var Opera = (navigator.userAgent.indexOf("Opera")!=-1);
	var MSIE = (navigator.appVersion.indexOf("MSIE")!=-1);
	var Moz = (navigator.userAgent.indexOf("Mozilla/5")!=-1);
	var el = document.getElementById('__mfcCommand');
	var f = document.getElementById('form1');
	var isSave = false;
	var isDelete = false;
	var fOK = true;
	
	if (s.split('save').length > 1) {isSave = true}
	if (s.split('delete').length > 1) {isDelete = true}
	if (s.split('undo').length > 1) {isDirty = false;}
	
	if (arguments.length > 1) {
		for (var a=1; a < arguments.length; a++) {
		    var elName = '__mfcArgument' + a;
            var elArg = document.getElementById(elName);
		    if (elArg)
		    {
		        elArg.value = arguments[a];
		    }
		}
	}
	
	if (s == 'View' | s == 'view') {		
		var tab = arguments[1];		
		if ((tab.toString()).length > 0) {			
			if (tab < 10000) {
				changeView(tab,1); 
				fOK = false;
			} else {
				fOK = false;
			}
		}
	}
	if (isDelete) {
		fOK = window.confirm('Are you sure you wish to delete this record?');
	}
	
	if (isSave) {
	    
		if (self.doValidate) {
			fOK = doValidate();
		} else { 
			fOK = true;
		}
		if (fOK) { isDirty = false }
		
		fOK = chkValid(fOK);
	
	}

	if (fOK) {

        showDialogBg('commandBg'); 
        showPanel('commandWait');

		el.value = s;
	
	}
	
	return fOK;
	
}

//Function for changing bgColor of TextBox, DropDownBox, TextArea etc.
//-------------------------------------------------------------------

function startEdit(e,a) {

	var key;			
	flg=false;	
	
	// If condition for changing color of dropdown list in FireFox browser
	
	if(navigator.userAgent.indexOf("Mozilla/5")!=-1)
	{
		a.style.backgroundColor = "yellow";
		isDirty = true;
	}else
	{
	}
	
	if(window.event) 
	{			
		//alert("IE");	
		key=event.keyCode;		
		ctrlFlg=false;			
		altFlg=false;
		
		if(key==7 || key==10 || key==11 || key==13 || key==17 || key==20 || key==21)
		{
			ctrlFlg=true;
		}			
	}
	else if(e.which) 
	{
		// netscape		
		key = e.which; 		
		ctrlFlg = e.ctrlKey;	
		altFlg = e.altKey;
		
		if(ctrlFlg==true && (key==118 || key==86) || (key==120 || key==88))
		{
			ctrlFlg = false;
			//alert(ctrlFlg);
		}		
	}
	else {
		// no event, so pass through
		return true;
	}
	
		
	if(altFlg==false && ctrlFlg==false)
	{		
		a.style.backgroundColor = "yellow";	
		isDirty = true;	
	}else
	{
		
	}
	

}



function KeyCheck(e)
{

	var KeyID = (window.event) ? event.keyCode : e.keyCode;
	
	
	var el = event.srcElement;
	
	if (el.onkeypress) {
		var sFunc = el.onkeypress.toString();
		var i = sFunc.indexOf('startEdit');
		
		if (i != -1) {
			//alert('Editable');
			yesToStartEdit=true;
		} else {
			alert('Not Editable');			
		}
	} else {
		//alert('Not Editable outer');
		yesToStartEdit=false;
	}

	
	if(window.event.ctrlKey)
	{
		ctrlFlgIE=true;
	}else if(window.event.altKey)
	{
		altFlg=true;	
	}
	else
	{
		ctrlFlgIE=false;
		altFlg=false;
	}
	
	key=KeyID;
    
	if(ctrlFlgIE==true && ((key==118 || key==86) || (key==120 || key==88)))
	{
		//alert("x or v");		
		ctrlFlgIE = false;		
	}else
	{
		//alert("not x or v");
		ctrlFlgIE = true;			
	}
  
	if(altFlg==false && ctrlFlgIE==false && yesToStartEdit==true)
	{		
		//alert("change color");
		fld=document.getElementById(event.srcElement.id);			
		fld.style.backgroundColor = "yellow";
		isDirty = true;				
	}
	
   
}

document.onkeyup = KeyCheck;


//-------------------------------------------------------------------

function kill_navigation_notification()
{
	noRequired = false;
}

//Function for arranging footer in all browser
//-------------------------------------------------------------------
function chkBrowser(){			
	
	var Opera = (navigator.userAgent.indexOf("Opera")!=-1);
	var MSIE = (navigator.appVersion.indexOf("MSIE")!=-1);
	var Moz = (navigator.userAgent.indexOf("Mozilla/5")!=-1);
	var myframe = document.getElementById("panMain");
	//alert(navigator.appVersion.indexOf("Opera"));
	//alert(MSIE);
	//alert(Moz);
		
	if (Moz)
	{
		myframe.style.height="100%";					
		myframe.style.width="100%";					
	}
	if (Opera) 
	{	
		myframe.style.height=window.innerHeight;
		myframe.style.width="100%";	
	}

}

//-------------------------------------------------------------------

function checkWindowUnload() {
    if (isDirty && msgDisplay==0) {
	        
		if(noRequired==false)
		{
			//alert('no required');
			noRequired=true;
		}else
		{
			var msg = "You have not saved your changes.";
			msgDisplay=1;
			return msg;
		}
	}else
	{
	}
} 

function changeView(x,move)
{		
	var sTab = new String(x);
	location.href = changeArgs('tab', sTab)
}

function changeArgs(sArgList, sValueList) {

	var sURL = document.URL;
	var sWithout = sURL;
	
	var arrA = sArgList.split(',');
	var arrV = sValueList.split(',');
	
	for (var n=0; n < arrA.length; n++ ) {

		sArg = arrA[n];
		sValue = arrV[n];
		
		//first look for the argument preceded by a ?
		var i = sURL.indexOf('?' + sArg + '=');
		//if not found, look for it preceded by a &
		if (i == -1) { 	i = sURL.indexOf('&' + sArg + '=');	}

		if (i != -1) {
			//if either is found, get the leftmost part of the string
			var sLeft = sURL.substring(0, i);
			var x = i + sArg.length + 2
			//now get the rightmost part of the string (after the equal sign ie: "sArg=")
			var sRight = sURL.substring(x, sURL.length);
			
			//see if there are any more arguments following
			var y = sRight.indexOf('&');
			if (y == -1) {
				//if no additional arguments, then the url WITHOUT the specified parameter is the left portion (sLeft)
				sWithout = sLeft;
			} else {
				if (sLeft.indexOf('?') == -1) { 
					sLeft = sLeft + '?'; 
				} else {
					sLeft = sLeft + '&';
				}
				sWithout = sLeft + sRight.substring(y+1,sRight.length);
			}
		
		} 
		if (sWithout.indexOf('?') != -1) {
			sURL = sWithout + '&' + sArg + '=' + sValue;
		} else {
			sURL = sWithout + '?' + sArg + '=' + sValue;
		}
		
		sWithout = sURL;

	}
	
	return sURL;
}



//function for use CTRL + TAB key into textArea
function CheckTab(el) {
  // Run only in IE
  // and if tab key is pressed
  // and if the control key is pressed
  if ((document.all) && (9==event.keyCode) && (event.ctrlKey)) {
    // Cache the selection
    el.selection=document.selection.createRange(); 
    setTimeout("ProcessTab('" + el.id + "')",0)
  }
}

function ProcessTab(id) {
  // Insert tab character in place of cached selection
  document.all[id].selection.text=String.fromCharCode(9)
  // Set the focus
  document.all[id].focus()
}

/*===================================================================
Javascript needed to support the drag-n-drop panel
Code sample from http://www.brainjar.com/dhtml/drag/default.asp

 Do not remove this notice.

 Copyright 2001 by Mike Hall.
 See http://www.brainjar.com for terms of use.
===================================================================*/

// Determine browser and version.

function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

// Global object to hold drag information.

var dragObj = new Object();
dragObj.zIndex = 0;

function dragStart(event, id) {

  var el;
  var x, y;

  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;

    // If this is a text node, use its parent element.

    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  // Update element's z-index.

  //dragObj.elNode.style.zIndex = ++dragObj.zIndex;

  // Capture mousemove and mouseup events on the page.

  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}

function dragGo(event) {

  var x, y;

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Move drag element by the same amount the cursor has moved.

  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

   
  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}

function dragStop(event) {

  // Stop capturing mousemove and mouseup events.

  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}

function hidePanel(event,id) {

    var elHide;
    var elCommand;
    
    if (id)
        elHide = document.getElementById(id);
    else {
        if (browser.isIE) 
            elCommand = window.event.srcElement;
        
        if (browser.isNS)
          elCommand = event.target;
          
        elHide = elCommand.parentNode;
    }

    elHide.style.display = 'none';
    
}

function showPanel(event,id) {

    var elHide;
    var elCommand;
    
    if (id)
        elHide = document.getElementById(id);
    else {
        if (browser.isIE) 
            elCommand = window.event.srcElement;
        
        if (browser.isNS)
          elCommand = event.target;
          
        elHide = elCommand.parentNode;
    }

    elHide.style.display = 'inline';
 
}

function showPanel(id) {

    var elCenter;
    var elCommand;
    
    var iClientHeight = f_clientHeight();
    var iClientWidth = f_clientWidth();
        
    if (id)
        elCenter = document.getElementById(id);
        
    if (elCenter)
    {
        elCenter.style.display = "inline";
        var iElementWidth = elCenter.offsetWidth;
        var iElementLeft = (iClientWidth / 2) - (iElementWidth / 2)

        var iElementHeight = elCenter.offsetHeight;
        var iElementTop = ((iClientHeight / 2) - (iElementHeight / 2)) + f_scrollTop();

        elCenter.style.left = iElementLeft + "px";
        elCenter.style.top = iElementTop + "px";
        
        if (showWaiting) {showWaiting();}
    }
    
}

function showDialogBg(id) {

    var elBg;
    
    if (id)
        elBg = document.getElementById(id);
        
    elBg.style.display = "inline";        
}

function hideDialogBg(id) {

    var elBg;
    
    if (id)
        elBg = document.getElementById(id);
        
    elBg.style.display = "none";        
}


/*===================================================================
Functions to retrieve client window height / width / position
from http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
===================================================================*/
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
