// This function checks empty form submissions on the login page
function submit_login() {
	if (document.ems_login.auth_username.value == "") {
		alert('Please provide your user account. ');
		return false;
	} 
	else if (document.ems_login.auth_password.value == "") {
		alert('Please provide your password. ');
		return false;
	}
	else {
		return true;
	}
}

// This function focus's the username field
function goto_login() {
  document.ems_login.auth_username.focus();
}

// This function opens a confirmation box
function confirm_submit(message) {
	var agree = confirm(message);
	if (agree) {
		return true;
	}
	else {
		return false;
	}
}

// This function opens a confirmation box and
// closes the window requesting it on false
function confirm_submit_close(message) {
	var agree = confirm(message);
	if (agree) {
		return true;
	}
	else {
        window.close();
		return false;
	}
}

// This function ticks all check boxes on a form
function tick_all(form, frame) {
  if(!frame) frame = 'MAIN';
    for (i = 0; i < parent[frame].document[form].length; i++) {
        if (parent[frame].document[form].elements[i].type=='checkbox') {
            parent[frame].document[form].elements[i].checked = true; 
        } 
    }
}

// This function unticks all check boxes on a form
function untick_all(form, frame) {
  if(!frame) frame = 'MAIN';
    for (i = 0; i < parent[frame].document[form].length; i++) {
        if (parent[frame].document[form].elements[i].type=='checkbox') {
            parent[frame].document[form].elements[i].checked = false; 
        } 
    }
}

// This function opens the Thawte Certificate details popup
function open_cert_details() { 
	config='height=450,width=500,toolbar=no,menubar=no,scrollbars=no,resizable=yes,location=no,directories=no,status=yes'; 
	window.open('https://www.thawte.com/cgi/server/certdetails.exe?code=NZEYED1', 'anew', config); 
}


// This function automatically submits a drop down menu
function menu_jump(targ, selObj, restore) {
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) {
		selObj.selectedIndex = 0;
	}
}

// This function generates a popup window with defined elements
function NewWindow(mypage, myname, w, h, scroll) {
    var win = null;
	var winl = (screen.width - w) / 8;
	var wint = (screen.height - h) / 4;
	var settings  ='height='+h+',';
		settings +='width='+w+',';
		settings +='top='+wint+',';
		settings +='left='+winl+',';
		settings +='scrollbars='+scroll+',';
		settings +='resizable=yes';
		win = window.open(mypage, myname, settings);
		if (win.focus) {
			win.focus();
		}
}
var refresh_val;
function refresh_page(frame)
{
  if(refresh_val == 'true')
  {
    refresh_val = 'false';
    frame.location.reload();
  }
}

// This function adds values to a drop down list
function AddToOptionList(OptionList) {
	newval = prompt("add a new value:", "");
	OptionPos = OptionList.length;
	OptionList[OptionPos] = new Option(newval, newval);
	OptionList.selectedIndex = OptionPos;
}

// Standard image rollover script
function MM_swapImgRestore() {
	var i,x,a=document.MM_sr;
	for (i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() {
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() {
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// This function returns a boolean value telling whether the first argument is an Array object. 
function isArray() 
{
  if (typeof arguments[0] == 'object') 
  {
    var criterion = arguments[0].constructor.toString().match(/array/i);
    return (criterion != null);
  }
  return false;
}

// this function is used for the dynamic javascript dropdowns
function ChangeDropdown(formname, dropdown1, dropdown2, dropdown2_array, dropdown2_selected)
{
  var checkArray = isArray(dropdown2_array[document[formname][dropdown1].options[document[formname][dropdown1].selectedIndex].value]);
  if (document[formname][dropdown1].options[document[formname][dropdown1].selectedIndex].value == 0 || checkArray == false)
  {
    oldLength = document[formname][dropdown2].length;
    for (i = oldLength; i > 1; i--)
    {
      document[formname][dropdown2].options[i] = null;
    }
  }
  else
  {
    num = document[formname][dropdown1].options[document[formname][dropdown1].selectedIndex].value;
    newLength = dropdown2_array[num].length + 1;
    oldLength = document[formname][dropdown2].length;

    // Clear Object
    for (i = oldLength; i > 1; i--)
    {
      document[formname][dropdown2].options[i] = null;
    }

    // Populate Object
    if (document[formname][dropdown2].options[i] != null)
    {
      for (i = 2; i <= newLength; i++)
      {
        document[formname][dropdown2].options[i] = dropdown2_array[num][i-2];
      }
    }
  }
  if(dropdown2_selected)
  {
    newLength = document[formname][dropdown2].length;
    document[formname][dropdown2].value = dropdown2_selected;
    if(document[formname][dropdown2].value == "")
    {
      document[formname][dropdown2].options[newLength] = Option(dropdown2_selected,dropdown2_selected);
      document[formname][dropdown2].value = dropdown2_selected;
    }
  }
  else
  {
    document[formname][dropdown2].options[0].selected = true;
  }
}

// These function are for the cropbox used by eyemage 
var aspect_ratio;
var image_width;
var image_height;
var image_x;
var image_y;

var cropbox_x;
var cropbox_y;
var cropbox_width;
var cropbox_height;

//-------------------------------------------------------------------------------------
function init(){
	image_width = document.cropimage.width;
	image_height = document.cropimage.height;

	// Change the image_x and image_y if the image isnt in the top-left corner
	image_x = 0;
	image_y = 0;	
	
    cropbox_x = cropbox_getleft()
    cropbox_y = cropbox_gettop()
	cropbox_width = cropbox_getwidth();
	cropbox_height = cropbox_getheight();

	document.getElementById("croptable").onmousedown = beginDrag;
	document.getElementById("resize").onmousedown = beginDrag;
}


//-------------------------------------------------------------------------------------
function doMove(e) {
	if (is_ie) e = event;
	
	// Calculates the difference from the last stored position to 
	// the current position. 
	//
        var difX=e.clientX-window.lastX;
        var difY=e.clientY-window.lastY;
	
	// Retrieves the X and Y position of cropbox.
	//
	if (is_ie) {
	        var newX = parseInt(document.getElementById("cropbox").style.pixelLeft) + difX;
	        var newY = parseInt(document.getElementById("cropbox").style.pixelTop) + difY;
	} else {
	        var newX = parseInt(document.getElementById("cropbox").style.left) + difX;
	        var newY = parseInt(document.getElementById("cropbox").style.top) + difY;
	}
	
	// Check to see if we have exceeded the size.
	//
	if (newX < image_x) newX = image_x;
	if (newY < image_y) newY = image_y;
	if ((newX + cropbox_width) > image_width) newX = image_width - cropbox_width;
	if ((newY + cropbox_height) > image_height) newY = image_height - cropbox_height;

	// Sets the new position for the cropbox div element. 
	//
	cropbox_x = newX;
	cropbox_y = newY;
	if (is_ie) {
		document.getElementById("cropbox").style.pixelLeft=newX;
        	document.getElementById("cropbox").style.pixelTop=newY;
	} else {
		document.getElementById("cropbox").style.left=newX+"px";
        	document.getElementById("cropbox").style.top=newY+"px";
	}

	// Stores the current mouse position as last position. 
	//
        window.lastX=e.clientX;
        window.lastY=e.clientY;
}

//-------------------------------------------------------------------------------------
function doResize(e) {
	if (is_ie) e = event;
	
	// Calculates the difference from the last stored position to 
	// the current position. 
	//
        var difX=e.clientX-window.lastX;
	var difY=e.clientY-window.lastY;

	// Retrieves the height and width position of cropbox.
	//
	if (difY == 0) {
		var newWidth = parseInt(document.getElementById("croptable").style.width) + difX;
		var newHeight = newWidth * aspect_ratio;
	} else {
		var newHeight = parseInt(document.getElementById("croptable").style.height) + difY;
		var newWidth = newHeight / aspect_ratio;
	}

	// Check to see if we have exceeded the size.
	//
	if ((newWidth + cropbox_x) > image_width) newWidth = image_width - cropbox_x;
	if ((newHeight + cropbox_y) > image_height) newHeight = image_height - cropbox_y;

	// Sets the new dimensions of the cropbox
	//
	cropbox_width = newWidth;
	cropbox_height = newHeight;
	document.getElementById("croptable").style.width=newWidth;
	document.getElementById("croptable").style.height=newHeight;
	

	// Stores the current mouse position as last position. 
	//
        window.lastX=e.clientX;
        window.lastY=e.clientY;
}

//---------------------------------------------------------------------------- 
function beginDrag(e) {
	if (is_ie) e = event;

	// Determine the type of move
	//
	if (is_ie) {
		changeMode = e.srcElement.id;
	} else {
		changeMode = e.target.id;
	}	

	// Stores the current mouse position
        window.lastX=e.clientX;
        window.lastY=e.clientY;
	
	// Registering doDrag event handler to receive onmousemove events. 
	//
	switch(changeMode) {
		case "resize":
			document.onmousemove = doResize;
			break;
		default:
			document.onmousemove = doMove;
	}

	// Registering endDrag event handler to receive onmouseup events. 
	//
        document.onmouseup=endDrag;
}

//----------------------------------------------------------------------------
function endDrag(e) {
	if (is_ie) e = event;

	// Release doDrag event handler assignment. 
	//
        document.onmousemove=null;
}

//----------------------------------------------------------------------------
function cropbox_getleft() {
	if (is_ie) {
	        return parseInt(document.getElementById("cropbox").style.pixelLeft);
	} else {
	        return parseInt(document.getElementById("cropbox").style.left);
	}
}
//----------------------------------------------------------------------------
function cropbox_gettop() {
	if (is_ie) {
	        return parseInt(document.getElementById("cropbox").style.pixelTop);
	} else {
	        return parseInt(document.getElementById("cropbox").style.top);
	}
}
//----------------------------------------------------------------------------
function cropbox_getwidth() {
	return parseInt(document.getElementById("croptable").style.width);
}
//----------------------------------------------------------------------------
function cropbox_getheight() {
	return parseInt(document.getElementById("croptable").style.height);
}

//-->