//#-------------------------------------------------------------------
//#
//# Confidential - Property of Symcell Corporation
//# Do not copy or distribute.
//# Copyright 2005-2007 Symcell Corporation. All rights reserved.
//#
//#-------------------------------------------------------------------

//Authored By: Daevid Vincent [daevid@symcell.com]

var rowShow = ( document.all ? 'block' : 'table-row' );

function showHideRowArray(myRows, on)
{
	if ( on )
	{
		for (var i in myRows) 
		{
			r = document.getElementById(myRows[i]);
			r.style.visibility = 'visible';
			r.style.display = rowShow;
		}
		
		return true;
	}
	else
	{
		for (var i in myRows) 
		{
			r = document.getElementById(myRows[i]);
			r.style.visibility = 'hidden';
			r.style.display = 'none';
		}
		
		return false;
	}
}

var plus = new Image();
plus.src = "images/icons/plus.gif";

var minus = new Image();
minus.src = "images/icons/minus.gif";

function toggleSection( section, image )
{
	var el;
	el = document.getElementById( section );
	if ( 'none' == el.style.display )
	{
		el.style.visibility = 'visible';
		el.style.display = 'block';
		image.src = minus.src;
	}
	else
	{
		el.style.visibility = 'hidden';
		el.style.display = 'none';
		image.src = plus.src;
	}
}

function imgOn(imgName){ if (document.images) document[imgName].src = eval(imgName + "_on.src"); }
function imgOff(imgName){ if (document.images) document[imgName].src = eval(imgName + "_off.src");	}

//// BEGIN of row rollover highlighting ////
var oldColor = '';
function highlightRow(row)
{
	oldColor = row.className;
	row.className = "rowHighlight";
}
// note that there is a part of this in the global.inc.php:
// define('TR_HIGHLIGHT', "onMouseOver='highlightRow(this)' onMouseOut='this.className = oldColor;'");
//// END of row rollover highlighting ////

//// END of row rollover highlighting ////

// check all the checkboxes on or off in a form
// <INPUT TYPE="checkbox" onClick="checkAll(this.form,this.checked);"> Check All/None
function checkAll(f,bool)
{
    for (var i = 0; i < f.elements.length; i ++)
         if (f.elements[i].type == "checkbox")
              f.elements[i].checked = bool;
}

// check all the checkboxes on or off in a form that have a name beginning with a given prefix
// <INPUT TYPE="checkbox" onClick="checkAll(this.form,this.checked,'prefix_');"> Check All/None
function checkAllByName(f,bool,startsWith)
{
    for (var i = 0; i < f.elements.length; i ++)
         if ((f.elements[i].type == "checkbox") && (f.elements[i].name.indexOf(startsWith) == 0))
              f.elements[i].checked = bool;
}

// just like checkAll() except works on a subset of checkboxes whose names begin with name_start
//TODO: [dv] this could have just been another parameter to checkAll()
function checkAllInNamedGroup(f,name_start,bool)
{
	var key_len = name_start.length;
    for (var i = 0; i < f.elements.length; i ++)
         if ((f.elements[i].type == "checkbox") && (f.elements[i].name.substr(0,key_len) == name_start))
              f.elements[i].checked = bool;
}

function countCheckedItems(f, items) 
{
	selectedItems = 0;

    for (var i = 0; i < f.elements.length; i ++)
			if (f.elements[i].type == "checkbox" && f.elements[i].checked) selectedItems++;


	if (selectedItems < 1) 
	{
		alert('You must check at least one ' + items + '.');
		return false;
	}

	/*
	if (selectedItems > available) 
	{
		alert('You are exceeding your available licenses. You must deselect ' + (selectedItems - available) + ' IP addresses\nPlease contact sales@<?=COMPANY_URL?> if you wish to purchase more IPs');
		return false;
	}
	*/
	return true;
}

function countCheckedItemsByName(f, items, startsWith) 
{
	selectedItems = 0;
	
	for (var i = 0; i < f.elements.length; i ++)
	{
		if ( f.elements[i].type == "checkbox" && 0 == f.elements[i].name.indexOf(startsWith) && f.elements[i].checked )
		{
			selectedItems++;
		}
	}
	
	if (selectedItems < 1) 
	{
		alert('You must check at least one ' + items + '.');
		return false;
	}
	
	return true;
}

function textCheck(id, str, textlength) 
{
	if (textlength > 0 && id.value.length < textlength)
	{
		alert('The ' + str + ' must contain at least ' + textlength + ' characters.');
		id.focus();
		id.select();
		return false;
	} else if (id.value.length < 1) {
		alert('The ' + str + ' can not be blank.');
		id.focus();
		id.select();
		return false;
	}
	return true;
}

//// Validation ////	
function emailCheck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
}

function isNumeric(strString)
{
   var strValidChars = "0123456789.-";
   var strChar;

   if (strString.length == 0) return false;

   for (i = 0; i < strString.length; i++)
   {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) return false;
   }

   return true;
}
		
function isValidDate(dateStr) {
	//if (dateStr == "") return true;
	
	if (dateStr == "0000-00-00") return true;
	
	//var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	var datePat = /(\d{4})-(\d{2})-(\d{2})/;
	
	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) {
		//alert(dateStr + " is invalid.\nDate must be of the format YYYY-MM-DD.")
		return false;
	}
	
	year = matchArray[1];
	month = matchArray[2]; 
	day = matchArray[3];
	
	if (month < 1 || month > 12) { // check month range
		alert("Month must be between 1 and 12.");
		return false;
	}
	
	if (day < 1 || day > 31) {
		alert("Day must be between 1 and 31.");
		return false;
	}
	
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month "+month+" doesn't have 31 days.")
		return false;
	}
	
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
			alert("February " + year + " doesn't have " + day + " days.");
			return false;
		}
	}
	
	return true;
}
//// Validation ////

function editCheck(id, item, AllowMultiple) 
{
	//'AllowMultiple' is optional if you aren't using a 'MULTIPLE' select box.

	//use like this: 
	//<INPUT TYPE="submit" CLASS="button-submit" NAME="action" VALUE="Edit" onClick="return editCheck(this.form.RangeID, 'range');">

    //for PHP arrays, you have to use this format...
	//variable = documents.forms[0].elements['var[]'];
	//so for example, use this function like this:
	//editCheck(this.form.elements['RID[]'], 'Device')

	if( typeof id == "undefined") {
		alert("There are no " + item + "s defined");
		return false;
	}

	if( typeof AllowMultiple == "undefined" || AllowMultiple == false) 
		AllowMultiple = false;
	else
		AllowMultiple = true;
		
	selectedItems = 0;
		
	if (id.length > 1) 
	{
	  for(i = 0; i < id.length; i++)
	    if (id[i].selected && id[i].value != "") selectedItems++;
	} 
	else 
	{
	    if (id.value != "") selectedItems++;
	}
	
	if( selectedItems < 1 ) {
		alert("Please select which " + item + " from the select box.");
		//document.webForm.id.focus()
		return false;
	}

	if( selectedItems > 1 && !AllowMultiple ) {
		alert("This action does not support Multiple selection on " + item + "s.");
		//document.webForm.id.focus()
		return false;
	}

	return true;
}

function selectCheck(id, item) 
{
	//use like this: 
	//<INPUT TYPE="submit" CLASS="button-submit" NAME="action" VALUE="Edit" onClick="return selectCheck(this.form.RangeID, 'range');">

    //for PHP arrays, you have to use this format...
	//variable = documents.forms[0].elements['var[]'];
	//so for example, use this function like this:
	//selectCheck(this.form.elements['RID[]'], 'Device')

	if( typeof id == "undefined") {
		alert("There are no " + item + "s defined");
		return false;
	}

	selectedItems = 0;
	
	if (id.length > 1) 
	{
	  for(i = 0; i < id.length; i++)
	    if (id[i].selected && id[i].value != "") selectedItems++;
	} 
	else 
	{
	    if (id.value != "") selectedItems++;
	}

	if( selectedItems < 1 ) {
		alert("Please select a " + item + " from the select box.");
		//document.webForm.id.focus()
		return false;
	}
	return true;
}


function deleteCheck(id, item) 
{
	//use like this: 
	//<INPUT TYPE="submit" CLASS="button-submit" NAME="action" VALUE="Delete" onClick="return deleteCheck(this.form.RangeID, 'range');">

    //for PHP arrays, you have to use this format...
	//variable = documents.forms[0].elements['var[]'];
	//so for example, use this function like this:
	//deleteCheck(this.form.elements['RID[]'], 'Device')

	if( typeof id == 'undefined' || id == null || id.type == 'undefined' ) {
		alert("There are no " + item + "s defined");
		return false;
	}

	selectedItems = 0;
	
	if (id.length > 1) 
	{
	  for(i = 0; i < id.length; i++)
	    if (id[i].selected && id[i].value != '') selectedItems++;
	} 
	else 
	{
	    if (id.value != "") selectedItems++;
	}

	if (selectedItems < 1) 
	{
		alert("Please select which " + item + " to Delete from the select box.\n\nCAUTION: There is no undo or recovery option.");
		return false;
	}
	
	if (selectedItems == 1) 
	{
		var selIndex = id.selectedIndex;
		var optionValue = id.options[selIndex].value;
		var optionText = id.options[selIndex].text;
	
		if (confirm("Are you sure you want to delete " + item + " '" + optionText + "'?\n\nCAUTION: There is no undo or recovery option.")) return true;
	}
	if (selectedItems > 1) 
		if (confirm("Are you sure you want to delete these " + selectedItems + " " + item + "s?\n\nCAUTION: There is no undo or recovery option.")) return true;

	return false;
}

function move_item(selectFrom, selectTo)
{
// two panes, named like:
// <SELECT name="selectLeft[]" multiple size=10 STYLE="width: 110px;">...
// <SELECT name="selectRight[]" multiple size=10 STYLE="width: 110px;">...
// three buttons
//	<INPUT TYPE="BUTTON" VALUE="Add" onClick="move_item(this.form.elements['selectLeft[]'], this.form.elements['selectRight[]']);">
//	<INPUT TYPE="BUTTON" VALUE="Remove" onClick="move_item(this.form.elements['selectRight[]'], this.form.elements['selectLeft[]']);">
// 	<INPUT TYPE="SUBMIT" CLASS="button-submit" NAME="section_button" VALUE="Continue" onClick="return checkRightPane(this.form.elements['selectRight[]'], 'Section');">

  var f;
  var SI; // selected Index
  if (selectFrom.options.length > 0)
  {
    for (i = 0; i < selectFrom.length; i++)
    {
      if (selectFrom.options[i].selected)
      {
        SI = selectFrom.selectedIndex;
        f = selectFrom.options[SI].index;
        selectTo.options[selectTo.length] = new Option(selectFrom.options[SI].text, selectFrom.options[SI].value);
        selectFrom.options[f] = null;
		//selectTo.options[selectTo.length-1].selected = true;
        i--; // make the loop go through them all
      } //if
    } //for
  } //if
  //else alert('Please select a section from the list to Remove.');
} //move_item()

function moveOptionUp(selectBox, itemName) 
{
   if ( selectBox.length < 1) {  // If the list is empty
      alert("There are no " + itemName + "s which can be moved!");
   } else {
      var selected = selectBox.selectedIndex;
      if (selected == -1) {
         alert("You must select an " + itemName + " to move up.");
      } else {  // Something is selected 
         if ( selectBox.length == 1 ) {  // If there's only one in the list
            alert("There is only one " + itemName + ".");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == 0 ) {
               alert("The first " + itemName + " in the list cannot be moved up.");
            } else {
               // Get the text/value of the one directly above the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = selectBox[selected-1].text;
               var moveText2 = selectBox[selected].text;
               var moveValue1 = selectBox[selected-1].value;
               var moveValue2 = selectBox[selected].value;
               selectBox[selected].text = moveText1;
               selectBox[selected].value = moveValue1;
               selectBox[selected-1].text = moveText2;
               selectBox[selected-1].value = moveValue2;
               selectBox.selectedIndex = selected-1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}

function moveOptionDown(selectBox, itemName) 
{
   if ( selectBox.length < 1) {  // If the list is empty
      alert("There are no " + itemName + "s which can be moved!");
   } else {
      var selected = selectBox.selectedIndex;
      if (selected == -1) {
         alert("You must select an " + itemName + " to move down.");
      } else {  // Something is selected 
         if ( selectBox.length == 1 ) {  // If there's only one in the list
            alert("There is only one " + itemName + ".");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == selectBox.length-1 ) {
               alert("The last " + itemName + " in the list cannot be moved down.");
            } else {
               // Get the text/value of the one directly below the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = selectBox[selected+1].text;
               var moveText2 = selectBox[selected].text;
               var moveValue1 = selectBox[selected+1].value;
               var moveValue2 = selectBox[selected].value;
               selectBox[selected].text = moveText1;
               selectBox[selected].value = moveValue1;
               selectBox[selected+1].text = moveText2;
               selectBox[selected+1].value = moveValue2;
               selectBox.selectedIndex = selected+1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}

function checkRightPane(id)
{
	//we must ensure all the options in the pane are selected = true before submitting the form element
	for(i = 0; i < id.length; i++)
	  id[i].selected = true;
}

function deleteLinkCheck(item)
{
	if (confirm("Are you sure you want to delete " + item + "?\n\nCAUTION: There is no undo or recovery option.")) return true;
	return false;
}

function deleteLinkCheckForward(item, target_url)
{
	if (confirm("Are you sure you want to delete " + item + "?\nThere is no undo or recovery option.")) location.href(target_url);
	return false;
}

function openNotesWindow(notes, id)
{
  notesWin = window.open('./notes_window.php?notes='+notes+'&id='+id, 'notes', 'width=500,height=300,scrollbars=yes,toolbar=no,resizable=yes,alwaysRaised=yes')
  if (notesWin.opener == null) notesWin.opener = self;
}

////////////////////////////////////////////////////////////////////////////////////////////////////

function jump_page(JumpForm) { if ( JumpForm.JumpItem.value != '' ) document.location = JumpForm.JumpItem.value; }

function addbookmark(){	if (document.all) window.external.AddFavorite(document.URL,document.title) }

function removeLoadingScreen() {
  document.getElementById("loader").style.visibility = "hidden";
  document.getElementById("content").style.visibility = "visible";
};

function validIP(o_theIP, s_theName)
{
/*
	pretty much everything is handled for you, just pass in the field object and the cosmeic name you want to call it
	with ( myForm ) {
		if (!validIP(scanner_ip_format, 'IP')) return false;
*/
	if ( o_theIP.value != null || o_theIP.value != "")
	{
		var pieces = o_theIP.value.split('.');
		//alert("pieces: '"+pieces[0]+"' '"+pieces[1]+"' '"+pieces[2]+"' '"+pieces[3]+"'");
		if ( pieces.length == 4 )
		{
			var success = true;
			for ( i = 0; i < 4; i++ )
			{
				if ( isNaN(pieces[i]) || pieces[i] == '' || pieces[i] == null || pieces[i] < 0 || pieces[i] > 255  )
				{
					//alert('pieces['+i+'] == '+pieces[i]);
					success = false;
					break;
				}
			}

			if ( success )
			{
				return true;
			}
		}
	}
	alert("Please enter a valid '" + s_theName + "' dotted quad address of the form AAA.BBB.CCC.DDD");
	o_theIP.focus();
	o_theIP.select();
   	return false;
}

//USAGE: <SELECT SIZE="5" MULTIPLE onmouseover="topSelectedOption(this);">
//TODO: it would be nice if it made the first selected option, the top selected option in the list as well.
function topSelectedOption(selbox)
{
	var sel = selbox.selectedIndex;
	if (sel > 0) selbox.options[sel].selected = true;
}

function expandImage(image,title,wid,hgt)
{
	sw = (screen.width - wid) / 2;
	sh = (screen.height - hgt) / 2;

	newwin=window.open('','newwin','width='+wid+',height='+hgt+',scrollbars=0,menubars=0,toolbars=0,location=0,directories=0,status=0,top='+sh+',left='+sw+'');
	newwin.document.open();
	newwin.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n');
	newwin.document.write('"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
	newwin.document.write('\n<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html;" />\n');
	newwin.document.write('<meta http-equiv="Imagetoolbar" content="no" />\n');
	newwin.document.write('<title>'+title+'</title>\n');
	newwin.document.write('</head>');
//	newwin.document.write('<body style="margin:0;padding:0">\n<center><img src="'+image+'" width="'+wid+'" height="'+hgt+'" /></center>');
	newwin.document.write('<body style="margin:0;padding:0">\n<center><img src="'+image+'" border=0 /></center>');
	newwin.document.write('\n</body></html>');
	newwin.document.close();
	newwin.focus();
}


