/************** global variables ****************/
var miniwindow
var minisubwindow
var radiowindow
var currentwindowurl = "";
var currentsubwindowurl = "";
var currentradiowindowurl = "";

/************** window functions ****************/

// function is used to load up generic popup window
function openwindow(htmlpage,winx,winy) {
  var wininit = "width=" + winx + ",height=" + winy + ",scrollbars=yes,resizable=yes";
  miniwindow = window.open(htmlpage,'miniwindow',wininit);
  miniwindow.focus();
}

function opensubwindow(htmlpage,winx,winy) {
  var wininit = "width=" + winx + ",height=" + winy + ",scrollbars=yes,resizable=yes";
  minisubwindow = window.open(htmlpage,'minisubwindow',wininit);
  minisubwindow.focus();
}

function openradio(htmlpage,winx,winy) {
  var wininit = "width=" + winx + ",height=" + winy + ",scrollbars=no,resizable=no";
  radiowindow = window.open(htmlpage,'radiowindow',wininit);
  radiowindow.focus();
}

// function is used to close down all pop-up windows when exiting page
function closeminiwindow() {
  if (miniwindow) {
    if (!(miniwindow.closed)) miniwindow.close();
  }
}

function closeminisubwindow() {
  if (minisubwindow) {
    if (!(minisubwindow.closed)) minisubwindow.close();
  }
}

function closewindow() {
  closeminiwindow();
  closeminisubwindow();
}

/************** validation functions ****************/

// function is used for confirming delete
function confirmdelete(form,elementname,elementvalue) {
  accept = window.confirm("Are you sure you want to delete?");
  if (accept == true) {  
	form.elements[elementname].value = elementvalue;
	form.submit();
  }
  else
	return;
}

// function is used for setting a value and submitting form
function setValue(form,elementname,value,reload) {
  form.elements[elementname].value = value;
  if (reload) form.submit();
}

function setDefault(form,elementname,value) {
  if (form.elements[elementname].value == "")
    setValue(form,elementname,value,false);
}

// function is used to redirect parent window
function setUrl(location_obj,url) {
  location_obj.href = url;
}

function setClass(id,value) {
  obj = document.getElementById(id);
  if (obj)
    obj.className = value;
}

var feature_set_index = new Array();
var feature_set = new Array();

function findFeatureSet(name) {
  index = -1;
  
  for (var i=0;i<feature_set_index.length;i++) {
	if (name == feature_set_index[i])
      index = i;
  }
  
  return index;
}

function addFeatureSet(name,features) {
  index = findFeatureSet(name);

  if (index == -1) {
	index = feature_set_index.length;
  }

  feature_set_index[index] = name;
  feature_set[index] = features;

  return index;
}

function rotateFeature(id,name,time) {
  obj = document.getElementById(id);
  index = findFeatureSet(name);
  position = randomIndex(name);
  
  if (obj && index >= 0 && position >= 0) {
	obj.innerHTML = feature_set[index][position];
	setTimeout("rotateFeature('"+id+"','"+name+"',"+time+")",time);
  }
}

function randomIndex(name) {
  num = -1;
  index = findFeatureSet(name);
  
  if (index >= 0 && feature_set[index].length > 0)
	num = Math.floor(Math.random()*(feature_set[index].length-1));

  return num;
}

function hideLabel(label_id) {
  label_obj = document.getElementById(label_id);
  if (label_obj) label_obj.style.display = 'none';
}

function displayMsgWin(id,width) {
  xpos = Math.round((document.body.clientWidth/2)-(width/2));
  ypos = 200;
  
  obj = document.getElementById(id);
  if (obj) {
	obj.style.display = '';
	obj.style.left = xpos + "px";
	obj.style.top = ypos + "px";
	obj.style.zIndex = 1;
  }
}

function hideMsgWin(id) {
  obj = document.getElementById(id);
  if (obj) {
	obj.style.display = 'none';
	obj.style.zIndex = 0;
  }
}

function loadImage(id,path,img) {
  obj = document.getElementById(id);
  if (obj) {
	if (img != "")
	  obj.innerHTML = "<img src=\"" + path + img + "\" border=\"0\">";
	else
	  obj.innerHTML = "";
  }
}

function loadMaterial(id,path,file) {
  obj = document.getElementById(id);
  if (obj) {
	if (file != "")
	  obj.innerHTML = "<a href=\"javascript:opensubwindow('" + path + file + "',1000,800);\">View File</a>";
	else
	  obj.innerHTML = "";
  }
}
