/************** global variables ****************/
var miniwindow
var currentwindowurl = "";

/************** window functions ****************/

// function is used to load up generic popup window
function openwindow(htmlpage,winx,winy) {
  if (!miniwindow || miniwindow.closed || (currentwindowurl != htmlpage)) {
	var wininit = "width=" + winx + ",height=" + winy + ",scrollbars=yes,resizable=yes";
    miniwindow = window.open(htmlpage,'miniwindow',wininit);
  } 
  currentwindowurl = htmlpage;
  miniwindow.focus();
}

// function is used to close down all pop-up windows when exiting page
function closewindow() {
  if (miniwindow) {
    if (!(miniwindow.closed)) miniwindow.close();
  }
}

/************** validation functions ****************/

// function is used for confirming delete
function confirmdelete(formname,elementname,elementvalue) {
  accept = window.confirm("Are you sure you want to delete?");
  if (accept == true) {  
	document.forms[formname].elements[elementname].value = elementvalue;
	document.forms[formname].submit();
  }
  else
	return;
}

// function is used for changing a value and submitting form
function changeValue(form,elementname,value,reload) {
  form.elements[elementname].value = value;
  if (reload) form.submit();
}

var currentpulldown = "";

/* dynamic html calls */
function toggleDisplay(id) {
  if (currentpulldown != id) {
    if (currentpulldown.length != 0)
      document.getElementById(currentpulldown).style.display = 'none';
	currentpulldown = id;
  } else
	currentpulldown = "";
  if (document.getElementById(id).style.display == '') {
	document.getElementById(id).style.display = 'none';
  } else {
	document.getElementById(id).style.display = '';
	document.getElementById(id).focus();
  }
}

function closeDisplay() {
  if (currentpulldown.length != 0) {
    document.getElementById(currentpulldown).style.display = 'none';
    currentpulldown = "";
  }
}

function toggleSearchDetails(id) {
  if (document.getElementById(id).style.display == '') {
	document.getElementById(id).style.display = 'none';
  } else {
	document.getElementById(id).style.display = '';
  }
}

function trim(s) {
  var i = 0;
  var j = s.length - 1;
  
  while(i < s.length && s[i] == ' ') {
	i++;
  }
  
  while(j > i && s[j] == ' ') {
	j -= 1;
  }

  return s.substring(i, j+1);
}

function fmtListToArray(str) {
  var new_arr = new Array();
  str = trim(str);

  if (str.length > 0) {
	while (str.search(/' , '/) != -1) {
	  str = str.replace(/' , '/,"','");
	}
	while (str.search(/', '/) != -1) {
	  str = str.replace(/', '/,"','");
	}
	while (str.search(/' ,'/) != -1) {
	  str = str.replace(/' ,'/,"','");
	}
    
	if (str.length > 0) {
	  if ((str.split("','")).length > 1) {
	    if (str.substr(0,1) == "'")
	      str = str.substr(1);

	    if (str.substr(str.length-1,1) == "'")
	      str = str.substr(0,str.length-1);
	  } else {
	    if (str.substr(0,1) == "'" && str.substr(str.length-1,1) == "'") {
	      str = str.substr(1);
          str = str.substr(0,str.length-1);
		}
	  }
	}

	var tmp_arr = str.split("','");
    
	for (var i in tmp_arr) {
	  var item = trim(tmp_arr[i]);
	  if (item.length > 0) new_arr.push(item);
	}
  }

  return new_arr;
}

function addToSearchList(form,field,chosen_value,chosen_op) {
  valid = true;

  ops = document.getElementsByName(field + "_type[]");
  values = document.getElementsByName(field + "[]");

  if (chosen_value.length == 0) valid = false;
  
  if (valid && values.length > 0) {
	/* verify it doesn't already exists */
    for (i=0;i<values.length;i++) {
	  if (ops[i].value.toLowerCase() == chosen_op.toLowerCase() && values[i].value.toLowerCase() == chosen_value.toLowerCase())
		valid = false;
	}
  }
  
  if (valid) {
	/* find proper id */
	num = 1;
	while (document.getElementById(field + "_" + num + "_hidden") != null) {
	  num += 1;
	}
	
	/* filter chosen_op */
	display_chosen_op = findOpString(chosen_op);
	
	str = "<div id=\"" + field + "_" + num + "_hidden\">";
	str += "<input type=\"hidden\" name=\"" + field + "_type[]\" value=\""+ chosen_op + "\">";
	str += "<input type=\"hidden\" name=\"" + field + "[]\" value=\"" + chosen_value + "\">";
	str += "<table cellspacing=\"0\" cellpadding=\"0\" class=\"form_chosen\" border=\"0\">";
	str += "<tr>";
	str += "<td align=\"left\" valign=\"top\" width=\"110\"><div style=\"padding-left: 5px; padding-right: 5px;\"><b>" + display_chosen_op + "</b></div></td>";
	str += "<td align=\"left\" valign=\"top\" width=\"365\"><div style=\"padding-left: 5px; padding-right: 5px;\">" + chosen_value + "</div></td>";
	str += "<td align=\"center\" valign=\"top\" width=\"15\"><a href=\"javascript:removeFromSearchList('" + field + "','" + field + "_" + num + "_hidden');\">X</a></td>";
	str += "</tr>";
	str += "</table>";
	str += "</div>";
	document.getElementById(field + "_values").innerHTML += str;
	document.getElementById(field + "_values").style.display = "";
  }
}

function addSearchValue(form,field,chosen_value,chosen_op) {
  var values = fmtListToArray(chosen_value);

  for (var i in values) {
    addToSearchList(form,field,values[i],chosen_op);
  }
}

function removeFromSearchList(field,id) {
  document.getElementById(id).innerHTML = "";
  document.getElementById(id).style.display = "none";
  
  values = document.getElementsByName(field + "[]");
  
  if (values.length == 0) {
	document.getElementById(field + "_values").innerHTML = "";
	document.getElementById(field + "_values").style.display = "none";
  }
}

function clearSearchList(field) {
  document.getElementById(field + "_values").innerHTML = "";
  document.getElementById(field + "_values").style.display = "none";
}

function findOpString(str) {
  newstr = "";

  if (str == 'cn') newstr = "contains";
  if (str == 'eq') newstr = "equals";
  if (str == 'neq') newstr = "does not contain";
  if (str == 'lte') newstr = "<=";
  if (str == 'gte') newstr = ">=";

  return newstr;
}

function actionNav(myform,name,val) {
  switch (name) {
	case 'command':
	  changeValue(myform,'action',val,true);
	  break;
      
	default:
	  changeValue(myform,'action',name,false);
	  changeValue(myform,'action_value',val,true);
  }
}

function selectCheckBox(myform,toggle,fld,index) {
  if (toggle) {
	if (myform.elements[fld].length == undefined)
	  myform.elements[fld].checked = true;
	else {
	  if (index == null) {
	    for (i=0;i<myform.elements[fld].length;i++) {
		  myform.elements[fld][i].checked = true;
	    }
	  } else
	    myform.elements[fld][index].checked = true;
	}
  }
}

var pulldown_ids = new Array();
var pulldown_htmls = new Array();

function addPulldown(id) {
  pulldown_ids.push(id);
  
  obj = document.getElementById(id+"_display");
  pulldown_htmls.push(obj.innerHTML);
}

function clearPulldowns() {
  for (i=0;i<pulldown_ids.length;i++) {
	obj = document.getElementById(pulldown_ids[i]+"_display");
	obj.innerHTML = pulldown_htmls[i];
  }
  pulldown_ids = new Array();
  pulldown_htmls = new Array();
}