//This is the master JavaScript file for the Webnotize Platform.

//Global Variables
//For function submitClick 

  var bConfirm = false;

//For function using ScrollObjects
  var lTop = 0;
  var lLeft = 0;
  var lscrollheight = 0;
  var lscrollwidth = 0;
  var lMaxLeft = new Array(); 
  var lMaxTop  = new Array(); 


function SetImageFrame(ImageFrame, imgSrc)  {
var ImageFrame = parent.frames[ImageFrame];
if (ImageFrame != null)
{
var imgTitle = ImageFrame.document.getElementById("imgTitle");
if (imgTitle != null)
  {

   imgSrc = "./images/" + imgSrc + ".jpg";
   imgTitle.src = imgSrc;}

}
}

function ResizeFrame(sDimensions,sFrame,sFrameset,sTarget,SetRows) {
	Frameset = parent.document.getElementById(sFrameset);
	if (Frameset != null) {
		if (SetRows) {
			Frameset.rows = sDimensions;
		}
		else  {
			Frameset.cols = sDimensions;
		}
		if (sTarget != '' && sFrame != '')  {
			parent.frames[sFrame].location.href= sTarget;
		}
	}	
}

function submitClick(sSubmitType, confirmChanges, doValidation, confirmSubmitType) 
{
    if (navigator.appName.toLowerCase() != 'netscape') {
        if (document.readyState.toLowerCase() != 'complete')  {
            alert('Please wait until the document is fully loaded on to your browser.');
            return false;
        }
    }
    gbDoNotValidate = true;
    var sSubmitFirst = sSubmitType.substr(0, sSubmitType.indexOf("_"));

    if (bConfirm && (sSubmitFirst=="PrevBlock" || sSubmitFirst=="NextBlock" || sSubmitFirst=="FilterTable"))  {
        var sSaveChanges = window.confirm("Click 'OK' to save or 'Cancel' to ignore any changes you have made.");
        if (sSaveChanges)  {
            gbDoNotValidate = false;
            sSubmitType = sSubmitType + "|Save";
        }
    }
    if (confirmChanges && bConfirm)  {
        var sSaveChanges = window.confirm("You have made changes on the screen.\nClick 'OK' to save or 'Cancel' to ignore any changes you have made.");
        if (sSaveChanges)  {
            gbDoNotValidate = false;
	    if (confirmSubmitType != null)  {
		sSubmitType = confirmSubmitType;
	    }
	    else  {
		if (document.Form.NewClose != null)  {
		    sSubmitType = "NewClose";
		}
		else if (document.Form.SaveClose != null) {
		    sSubmitType = "SaveClose";
		}
		else  {
		    gbDoNotValidate = true;
		}
	    }
        }
	else  {
	    doValidation = false;
	}
    }
    document.Form.SubmitType.value = sSubmitType;
    while (!isNaN(sSubmitType.substr(sSubmitType.length - 1)))  {
        sSubmitType = sSubmitType.substr(0, sSubmitType.length - 1);
    }
    if (doValidation || sSubmitType=="Save" || sSubmitType=="New" || sSubmitType=="Login" || sSubmitType=="Logon" || sSubmitType=="Register")
        gbDoNotValidate = false;

    return true;
}

function SetControlPosition(ScrollObjects) 
{
}

function AdjustControlTop(ScrollObjects) 
{
}

// Selection control functions
var selectedList = new Array(); 
var availableList = new Array();
function createListObjects(cnt){
    for(j=0; j<cnt; j++){
        availableList[j] = document.getElementById("avlOpts" + j);
	selectedList[j] = document.getElementById("selOpts" + j);
	var selValues = document.getElementById("selVal" + j).value;
	if (selValues != "")  {
	    selValues = selValues.substr(1);
	    while(selValues != "") {
		pos = selValues.indexOf("|");
		id = selValues.substr(0, pos)
		for (k = 0; k < availableList[j].length; k++) {
		    if (availableList[j].options[k].value==id) {
		        availableList[j].selectedIndex = k;
			addAttribute(j);
			break; 
		    }
		}
		selValues = selValues.substr(pos + 1);
	    }
	}
    }
}
function delAttribute(id){
   var selIndex = selectedList[id].selectedIndex;
   if(selIndex < 0)
      return;
   availableList[id].appendChild(selectedList[id].options.item(selIndex));
   selectNone(selectedList[id],availableList[id],id);
}
function addAttribute(id){
   var addIndex = availableList[id].selectedIndex;
   if(addIndex < 0)
      return;
   selectedList[id].appendChild(availableList[id].options.item(addIndex));
   selectNone(selectedList[id],availableList[id],id);
}
function selectNone(list1,list2,id){
    list1.selectedIndex = -1;
    list2.selectedIndex = -1;
    addIndex = -1;
    selIndex = -1;
    SetSelValues(id);
}
function delAll(id){
    var len = selectedList[id].length -1;
    for(i=len; i>=0; i--){
        availableList[id].appendChild(selectedList[id].item(i));
    }
    selectNone(selectedList[id],availableList[id],id);    
}
function addAll(id){
    var len = availableList[id].length -1;
    for(i=len; i>=0; i--){
        selectedList[id].appendChild(availableList[id].item(i));
    }
    selectNone(selectedList[id],availableList[id],id);
}
function SetSelValues(id){
    var len = selectedList[id].length -1;
    var selVals = document.getElementById("selVal" + id);
    selVals.value = "|";
    for(i=len; i>=0; i--){
        selVals.value += selectedList[id].item(i).value + "|";
    }
}
//EOF Selection control

// AddTable Rows
function addRow(templ, id, cnt, popArray) { 

var tbody = document.getElementById(id).getElementsByTagName("tbody")[0]; 
var row = document.createElement("TR");
row.setAttribute("id","tr" + cnt); // set ID of the row
for (var i in templ) {
	 var td1 = document.createElement("TD");

	/* create the input element as a seperate reference */ 

	var input = document.createElement(templ[i]["createtext"]);

	/* append the attributes to the element, not td */ 

	 input.setAttribute("id",templ[i]["source"] + "_n" + cnt);	 
	 input.setAttribute("name",templ[i]["name"] + "_n" + cnt); 
	 if (templ[i]["source"] != "DummyInput") {
		if (popArray != null) {
		 	input.setAttribute("value",popArray[i]);
		}
		else {
		 	input.setAttribute("value",document.getElementById(templ[i]["source"]).value);
		}
 	 }
 

	 if(templ[i]["type"] == "SELECT"){
		 var oOption = document.createElement("OPTION");
		if (popArray != null) {
			oOption.text=popArray[i]['text'];
			oOption.value=popArray[i]['value'];
		}
		else {
			oOption.text=document.getElementById(templ[i]["source"]).options[document.getElementById(templ[i]["source"]).selectedIndex].text;
			oOption.value=document.getElementById(templ[i]["source"]).options[document.getElementById(templ[i]["source"]).selectedIndex].value;
		}
		 input.options.add(oOption);
 	}

	 td1.appendChild(input);

	 row.appendChild(td1);
}
// document.getElementById(id).appendChild(row); 
tbody.appendChild(row); 
 
cnt = cnt + 1;
return cnt;  
} 

function delrows() {
 var num_rows = cnt;
 var rows_to_delete = new Array();
 for(var i = 0; i < num_rows; i++) {
  if(document.getElementById("Colinput1"+i) && document.getElementById("Colinput1"+i).checked) { 
   document.getElementById("myTable").deleterow(document.getElementById("tr" + i).rowIndex);
  }
 }
}
//EOF AddTableRows

//Dynamic Selects
function changeSelect(Source,ChildArray,Target,ClearedStyle) {

var mySourceListBox=document.getElementById(Source);

if 	(mySourceListBox.selectedIndex != -1)
	{
	var optionSelected=mySourceListBox.selectedIndex;
	var optionParent=mySourceListBox.options[optionSelected].value;


	for (j=Target.length-1;j>=0;j--)
		{
		var myTargetListBox=document.getElementById(Target[j]);
		for (i=myTargetListBox.length;i>0;i--)
			{
        		myTargetListBox.options[i]=null;
    			}
		myTargetListBox.selectedIndex = -1;
		myTargetListBox.disabled = false;
		myTargetListBox.style.display = "";
		if (j!=0)
			{//myTargetListBox.size = myTargetListBox.options.length;
			if (ClearedStyle == "Hide") 
				{myTargetListBox.style.display = "none";}
			if (ClearedStyle == "Disable") 
				{myTargetListBox.disabled = true;}

			}


		}
//The very first listbox named in the array is the target listbox.  The dependents of the target have now been cleared


   	for (i=0;i<ChildArray.length;i++)
    		{
		if (ChildArray[i]["P"].indexOf("|")!=-1)
			{
			var CompareArray = ChildArray[i]["P"]
			CompareArray = CompareArray.split("|");
			}
		else
			{
			var CompareArray = new Array(1)
			CompareArray[0] = ChildArray[i]["P"];
			}

		for (j=0;j<CompareArray.length;j++)
			{
			//alert(CompareArray[j] + ' compares to ' + optionParent)
			if (CompareArray[j]==optionParent)
        			{
				myTargetListBox[myTargetListBox.options.length] = new Option(ChildArray[i]["V"],ChildArray[i]["K"]);
				}
			}
       	    	}
		if (myTargetListBox.options.length == 1 && ClearedStyle == "Hide") 
				{myTargetListBox.style.display = "none";}
		myTargetListBox.value = 'null';
	}	

}
//EOF Dynamic Selects
// This function is called when a Table row is selected - the selected row is highlighted and the row's key value stored
var oldBgColour;
var oldTxtColour;
function highlightRow(tableName, rowId, bgColour, txtColour, bgHLTColour, txtHLTColour)  {
	var tableRows = document.getElementById(tableName).rows;
	if (bgHLTColour == '') {bgHLTColour = '#000080'};
	if (txtHLTColour == '') {txtHLTColour = '#FFFFFF'};
	if (bgColour == '') {bgColour = 'transparent'};
	if (txtColour == '') {txtColour = 'black'};
	for (i = 0; i < tableRows.length; i++)  { 
		//alert('row ' + i + ' bgcolour: ' + tableRows[i].bgColor + ' bgHLTColour: ' + bgHLTColour + ' textcolour: ' + tableRows[i].style.color + ' txtHLTColour: ' + txtHLTColour)
		if (tableRows[i].className != 'TableHeading' 
		&& tableRows[i].className != 'TableFilter' 
		&& tableRows[i].bgColor.toLowerCase() == bgHLTColour.toLowerCase() 
		&& tableRows[i].style.color.toLowerCase() == txtHLTColour.toLowerCase())  {
			//alert('row ' + i + ' is highlighted')
			tableRows[i].bgColor = bgColour;
			tableRows[i].bgColor = oldBgColour;
			tableRows[i].style.color = txtColour;
			tableRows[i].style.color = oldTxtColour;
		}
	}
	oldBgColour = tableRows[rowId].bgColor;
	tableRows[rowId].bgColor = bgHLTColour;
	oldTxtColour = tableRows[rowId].style.color;
	tableRows[rowId].style.color = txtHLTColour;
	if (document.getElementById(tableName + '_selItem') != null)  {
		document.getElementById(tableName + '_selItem').value = tableRows[rowId].id.substr(tableName.length + 4) //.replace('_','|');
	}
}
//EOF highlightRow

function ww_ShowDiv(divdlg, Div) {
for (var i in divdlg) {
	if (divdlg[i] == Div.id) {
		document.getElementById(divdlg[i]).style.display = "";
		document.getElementById("DivDlg" + Div.id.substr(13, 1) + "_CurrDiv").value = Div.id;
	}
        else {
		document.getElementById(divdlg[i]).style.display="none";
	}
  }
}
//Focus Tracking
function wwFocus(element, setToClass) {
	element.className = setToClass;
	element.HasFocus = true;
}

function wwBlur(element, setToClass) {
	element.className = setToClass;
	element.HasFocus = false;
}

function wwMouseOver(element, setToClass) {
	if (!element.HasFocus) {
	    element.className = setToClass;
	}
}

function wwMouseOut(element, setToClass) {
	if (!element.HasFocus) {
	    element.className = setToClass;
	}
}
//EOF FocusTracking

// Confirm deletion of highlighted row
function confirmDelete(elementId) {
  var elementValue = document.getElementById(elementId + '_selItem').value;
  if(elementValue != '' && confirm('This will delete the record(s) from the Database - are you sure you want to continue?')) {
    document.getElementById('chk' + elementId + '_' + elementValue).checked = false;
    document.Form.Save.click();
    return true;
  }
  return false;
}
//EOF Confirm delete
//If user clicks 'Change' default selected to row to first row in table
function setDefault(elementId) {
  var elementValue = document.getElementById(elementId + '_selItem').value
  if(elementValue == '') {
    var tableRows = document.getElementById(elementId).rows;
    for (i = 0; i < tableRows.length; i++)  {
      if (tableRows[i].className != 'TableHeading' && tableRows[i].className != 'TableFilter')  {
        tableRows[i].click();
        break;
      }
    }
  }
  document.getElementById('Change' + elementId).click();
}
//EOF setDefault
//Page has been clicked on Paging
function changePage(listName, pageNo) {
  if (document.getElementById('CurrBlock_' + listName))  {
    document.getElementById('CurrBlock_' + listName).value=pageNo;
  }
  if (document.getElementById('PrevBlock_' + listName)) {
    document.getElementById('PrevBlock_' + listName).click();
    return;
  }
  if (document.getElementById('NextBlock_' + listName)) {
    document.getElementById('NextBlock_' + listName).click();
  }
}
//EOF setDefault
function isBlank(s)
{
  var v = s.value;
  if (v == 'null') {return true};
  for(var i = 0; i < v.length; i++) {
    var c = v.charAt(i);
    if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
  }
  return true;
}

