/*
File:  		JsUtilities.js
Description: All the JavaScript Utilities


*/
// Pops up a child window with the given window name and dimensions.
// Takes the optional arguments to set the menubar, resize, and scrollbars attributes of Window.
function popupWindow(winname,  w, h, menu, resize, scroll) {
	if (winname == null) winname = "newWindow";
	if (w == null) w = 600;
	if (h == null) h = 600;
	if (resize == null) resize = 1;
	
	menutype   = "nomenubar";
	resizetype = "noresizable";
	scrolltype = "noscrollbars";
	if (menu) menutype = "menubar";
	if (resize) resizetype = "resizable";
	if (scroll) scrolltype = "scrollbars";
	
	cwin=window.open('', winname,
		"status," + menutype + "," + scrolltype + "," + resizetype + ",width=" + w + ",height=" + h);
return true;
}

function popupSign(winname,  w, h, menu, resize, scroll) {
	if (winname == null) winname = "newWindow";
	if (w == null) w = 600;
	if (h == null) h = 600;
	if (resize == null) resize = 1;
	
	menutype   = "nomenubar";
	resizetype = "noresizable";
	scrolltype = "noscrollbars";
	if (menu) menutype = "menubar";
	if (resize) resizetype = "resizable";
	if (scroll) scrolltype = "scrollbars";
	
	cwin=window.open('','newWindow','height=200,width=400,status=yes,toolbar=no,menubar=no,location=no');
return true;
}


function trim(string) {
   while (string.charAt(0) == " ")
      string = string.substring(1,string.length);

   while (string.charAt(string.length-1) == " ") 
      string = string.substring(0,string.length-1);

   return string;
}

function checkNumeric(element, whole, fractional) {

   if (fractional == 0) {
	 var value = parseInt(element.value);
   } else {
	 var value = parseFloat(element.value);
   }
//   if (trim(element.value) == "") return true;

   if (isNaN(value)) {
      alert("Please enter a number");
      element.focus();
      return false;
   }

   element.value = value;

   if (whole != null) {
      if (value >= Math.pow(10,whole)) {
         element.value = value;
         alert("Please enter a number less than " + Math.pow(10,whole));
         element.focus();
         return false;
      }

      if (fractional != null && fractional != 0) {
         temp = Math.floor((value-Math.floor(value)) * Math.pow(10,fractional)) 
         temp += Math.pow(10,fractional)
         temp += ""
         element.value = Math.floor(value) + "." + temp.substring(1,temp.length);
	  }
   }

   return true;
}

// A function to validate the E-mail
 function validEmail(email) {
    invalidChars = " /:,;"

  if (email == "") {      // cannot be empty
   return false
  }
  for (i=0; i<invalidChars.length; i++) { // does it contain any invalid characters?
   badChar = invalidChars.charAt(i)
   if (email.indexOf(badChar,0) > -1) {
    return false
   }
  }
  atPos = email.indexOf("@",1)   // there must be one "@" symbol
  if (atPos == -1) {
   return false
  }
  if (email.indexOf("@",atPos+1) != -1) { // and only one "@" symbol
   return false
  }
  periodPos = email.indexOf(".",atPos)
  if (periodPos == -1) {     // and at least one "." after the "@"
   return false
  }
  if (periodPos+3 > email.length) {  // must be at least 2 characters after the "."
   return false
  }
  return true
}

// Check form field content.
function checkField (thisform, fieldvalue) {
	if (thisform.BookmarkName.value == "") {
	   alert ('Please enter ' +  fieldvalue);
	   return false;
	}
	return true;
}

// Check form for E-mail addresses
 function checkForm (emailform) {
 	  if (emailform.email_target_email.value == "") {
	      alert ('Please enter "Send to" address.');
		  return false
	  }
	  else {  
		   if (!validEmail(emailform.email_target_email.value)) {
		    alert('Invalid "Send to" email address')
		    emailform.email_target_email.focus()
		    emailform.email_target_email.select()
		    return false
		   }
	  }
	  
	   if (emailform.email_from.value == "") {
	      alert ('Please enter "Your email" address.');
		  return false
	   }
	   else {
	   	   if (!validEmail(emailform.email_from.value)) {
		    alert('Invalid email address')
		    emailform.email_from.focus()
		    emailform.email_from.select()
		    return false
		   }
	   }

	  return true
 }

// Check thisform's multiple select list and give an error if selected 
// items count is more than maxcount.
function checkSelectionCount (thisform, selname, maxcount) {
   var j = 0;
   for (var i=0; i<thisform.elements[selname].options.length; i++) {
   	   if(thisform.elements[selname].options[i].selected) j++;
   }

   if (j > maxcount) {
	   alert("Please select " + maxcount + " or fewer items.\nCurrently selected items: " + j);
       return false;
   }
   else {
       return true;
   }

}

// Pops up a child window with the given window name and dimensions
/*
function popupWindow(winname,w,h) {
	if (winname == null) winname = "newWindow";
	if (w == null) w = 600;
	if (h == null) h = 600;
	cwin=window.open("", winname,
	"nomenubar,status,scrollbars,noresizable,width=" + w + ",height=" + h);
return true;
}
*/

// Submits the form associated with "thiswin" window and displays the results in 
// "popupname" window of dimensions 'w' and 'h'.  Uses popupWindow function. 
function submitClose(thiswin, popupname, w, h, menu, resize, scroll) {
	if (popupname == null) popupname = "resultswindow";
	if (w == null) w = 600;
	if (h == null) h = 600;
	popupWindow(popupname, w, h, menu, resize, scroll);
	
	thiswin.document.forms[0].submit();
	
	thiswin.close();
}

// Checks(status true) or unchecks(status false) all the checkboxes associated with the form.
function CheckUncheck(status) {
   for (i = 0; i < document.forms[0].length; i++) {
      document.forms[0].elements[i].checked = status
   }
}
