function openwin(page,id){
//alert(pid);
  //default window size
  x_res=480
  y_res=400
  //check for netscape and resize

  if (navigator.appName.indexOf("Netscape") >-1) {
    x_res=480
    y_res=400
  }

  window.open(page+'?id='+id,'_blank', 'title=window,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,width='+x_res+',height='+y_res+',top=30,left=30');
}
var lastMouseX;
var lastMouseY;
var curPopupWindow = null;
var helpWindow = null;

function setLastMousePosition(e) {
    e = window.event;
    lastMouseX = e.screenX;
    lastMouseY = e.screenY;
}

/**
 * Calls through to the openPopupFocus() with closeOnLoseFocus set to true.
 */
function openPopup(url, name, pWidth, pHeight, features, snapToLastMousePosition) {
		setLastMousePosition(event);
		openPopupFocus (url, name, pWidth, pHeight, features, snapToLastMousePosition, true);
}

/**
 * Handles popup windows.
 * If snapToLastMousePosition is true, then the popup will open up near the mouse click.
 * If closeOnLoseFocus is true, then it will close when the user clicks back into the browser window that opened it.
 */
function openPopupFocus(url, name, pWidth, pHeight, features, snapToLastMousePosition, closeOnLoseFocus) {
    closePopup();
    if (snapToLastMousePosition) {
        if (lastMouseX - pWidth < 0) {
            lastMouseX = pWidth;
        }
        if (lastMouseY + pHeight > screen.height) {
            lastMouseY -= (lastMouseY + pHeight + 50) - screen.height;
        }
        lastMouseX -= pWidth;
        lastMouseY += 10;
        features += "screenX=" + lastMouseX + ",left=" + lastMouseX + ",screenY=" + lastMouseY + ",top=" + lastMouseY;
    }
    if (closeOnLoseFocus) {
        curPopupWindow = window.open(url, name, features, false);
        curPopupWindow.focus ();
    } else {
        // assign the open window to a dummy var so when closePopup() is called it won't be assigned to curPopupWindow
        win = window.open(url, name, features, false);
        win.focus ();
    }
}

function closePopup() {
    if (curPopupWindow != null) {
       
        if (!curPopupWindow.closed) {
            curPopupWindow.close();
        }
        curPopupWindow = null;
    }
}

//Centers the popup window
function NewWindow(mypage, myname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
win = window.open(mypage, myname, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}


//Centers the popup window
function NewWindow2(mypage, myname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
win = window.open(mypage, myname, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function windowOpener(url, name, args) {
if (typeof(popupWin) != "object"){
popupWin = window.open(url,name,args);
} else {
if (!popupWin.closed){ 
popupWin.location.href = url;
} else {
popupWin = window.open(url, name,args);
}
}
popupWin.focus();
}

function validateInteger(theElement)
{
	if(isNaN(theElement.value)){
		alert('Please enter a numeric value')
		theElement.value = '';
		theElement.focus();
	}
	else
		theElement.value = Math.round(theElement.value);
}

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

// 11-9-2006 Have zip, phone, and ssn checking available on all form pages.  this is done to remove duplicates during loop
// FORM MASKING
// MG - 05/05/2006
// Call from form using: onKeyPress="return autoMask(this,event,'type');"

function autoMask(field, event, type){;
	var KeyTyped = String.fromCharCode(getKeyCode(event));

	//Check for zipcode type
	checkZipType(field,KeyTyped);

	if(getKeyCode(event) == 0)//added by Gabe Chino to skip the tab in firefox
		return;
	
	var targ = getTarget(event);
	var keyCount = targ.value.length;		
		
				
	// Determine type of input and setting mask pattern				
	switch (type){
			case "phone":
				sMask = "###-###-#####";
				break;
			case "date":
				sMask = "##/##/####";
				break;
			case "ssan":
				sMask = "###-##-####";
				break;
			case "zip":
				if(zipType == "us")
				{
					sMask = "#####-#####";
				}
				else if(zipType == "canada")
				{
					sMask = "### ###";
				}
				break;
	}
								
	// Allows use of backspace and delete key once user has reached masks length
	// Checks for a valid date
	// disabled - Auto Tabs to next input
				
	if (getKeyCode(event) == 8 || getKeyCode(event)==127)
	{
		return true;
	}
	if(keyCount + 1 == sMask.length){
		if (type == "date"){
			if (chkdate(field) == false){
				alert("That date is invalid. Please try again.");
				return false;
			}
		}
	}	
	
	// Compares key typed to mask and allows entry if not equal to mask placeholder
	if ((sMask.charAt(keyCount+1) != '#') && (sMask.charAt(keyCount+1) != '@' )){	
					
			
		if (type == 'phone')
		{								
			if (keyCount > '11') // For Phone Field, don't let user type more than 12 total characters
			{								
			return false;
			}
		}

		if (type == 'ssan')
		{
			if (keyCount > '10') // For SSN Field, don't let user type more than 11 total characters
			{
			return false;
			}
		}
		
		if (type == 'zip')
		{
		
			if(zipType == "us")
			{
				// this checks for the character before dash is entered, and prevents it.
				if (sMask.charAt(keyCount-1) && (!isNumber(KeyTyped)))
				{
					return false;
				}			

				if (keyCount > '9') // For Zip Code Field, don't let user type more than 10 total characters, including mask when applicable
				{
				return false;
				}				
			}
			
			if(zipType == "canada")
			{	
				if (keyCount > '6') // For Zip Code Field, don't let user type more than 7 total characters, including mask when applicable
				{
					return false;
				}			
			}
		}
		field.value = field.value + KeyTyped + sMask.charAt(keyCount+1);					
		
		return false;
	}												
							
			
	if ((sMask.charAt(keyCount) == '#') || (sMask.charAt(keyCount) == '-') || (sMask.charAt(keyCount) == ' '))
	{

		// adds the dash to phone number being inserted, if erased by user
		if (type == 'phone')
		{
			if ((keyCount == '3') || (keyCount == '7'))
			{
				if (isNumber(KeyTyped))
				{
					field.value = field.value + '-' + KeyTyped;
				}
				else
				{
					field.value = field.value + '-';
				}
			}
		}
				
		// adds the dash to ssn number being inserted, if erased by user
		if (type == 'ssan')
		{
			if ((keyCount == '3') || (keyCount == '6'))
			{
				if (isNumber(KeyTyped))
				{
					field.value = field.value + '-' + KeyTyped;
				}
				else
				{
					field.value = field.value + '-';
				}
			}
		}
				
		// adds the dash to US zip code being inserted, if erased by user
		// adds the space to Canadian zip code, if erased by user
		if (type == 'zip')
		{			

			if (keyCount == '3' && zipType == "canada")
			{
				if (!IsNumeric(field.value) && isAlphanumeric(field.value))
				{
					field.value = field.value + ' ' + KeyTyped;
				}
				else
				{
					field.value = field.value + ' ';
				}			
			}
			else if (keyCount == '5' && zipType == "us")
			{				
				if (IsNumeric(field.value) && isNumber(KeyTyped))
				{
					field.value = field.value + '-' + KeyTyped;
				}
				else
				{
					field.value = field.value + '-';
				}
			}
		}		
	}

	//Added by Sylvia
	//Check for Zip US and Canadian.
	if ((sMask.charAt(keyCount) == '#') && type =='zip'){		
		//US
		if(keyCount >= '4')	
		{
					
			if(IsNumeric(field.value.substring(0,5)))
			{
				if(isNumber(KeyTyped))
				{
					return true;
				}	
				else
				{
					return false;
				}
				if (keyCount > '9') // For Zip Code Field, don't let user type more than 10 total characters, including mask when applicable
				{
					return false;
				}		
			}		
		}
		
		//Canada
		if(keyCount >= '3')	
		{
			if(isAlphanumeric(field.value.substring(0,3)))
			{
				if (keyCount > '6') // For Zip Code Field, don't let user type more than 7 total characters, including mask when applicable
				{
					return false;
				}
									
			}
		}

		if(isAlphanumeric(KeyTyped))
		{
			return true;
		}
		else
		{
			return false;
		}			
				
		return true;	
	} 			
	
	// Check Number For all others but not Zipcode							
	if ((sMask.charAt(keyCount) == '#') && type !='zip'){													
		if(isNumber(KeyTyped))
		{
			return true;
		}			
	} 


	if (KeyTyped.charCodeAt(0) < 32){
		return true;
	}
					
	return false;
}//close AutoMask function

function checkZipType(field,KeyTyped)
{
	var Val = field.value + KeyTyped;
	if(Val != "")
	{
		if(IsNumeric(Val.substring(0,5)))
		{
			zipType = "us";
		}
		else
		{
			zipType = "canada";
		}
	}
}		

function getKeyCode(e) {
	if (e.srcElement){ //IE
		return e.keyCode
	}
	else if (e.which){//NETSCAPE
		return e.which
	}
	else if (e.target){//FIREFOX
	   return e.which
	}
}

function isAlphanumeric(alphane)
{
	var numaric = alphane;

	for(var j=0; j<numaric.length; j++)
	{
	  var alphaa = numaric.charAt(j);
	  var hh = alphaa.charCodeAt(0);
	  if((hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123))
	  {
	  }
	  else	{
		 alert('Enter Alphanumeric only.');				 
		 return false;
	  }
	}
 	return true;
}	

function getTarget(e){
	if (e.srcElement){
		return e.srcElement;
	}
	if (e.target){
		return e.target;
	}
}

				
function isNumber(n){
	var sNumbers = "01234567890";
	if (sNumbers.indexOf(n) == -1){
		alert('Enter numbers only.');
		return false;
	}
	
	else return true;
}

function IsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

function checkEmail(obj) { 		
	var TestVar = obj.value;
	
	// If blank, let it pass.
	if (TestVar == ""){ 
		return true; 
	  } 
	
	var pattern = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/;
	var flag = pattern.test(TestVar);
	if(!flag)
	{
	alert ("Please enter a valid email address");
	obj.focus();		
	obj.style.backgroundColor ="red";					
	return false;
	}
	obj.style.backgroundColor ="";	
	return true;
}

function checkPhone(obj) { 		
	var TestVar = obj.value;
		
	// If blank, let it pass.
	if (TestVar == ""){ 
		return true; 
	  } 
	
	if(TestVar.length > 0 && TestVar.length < 12)
	{
	alert ("Please enter phone number in the format xxx-xxx-xxxx.");
	obj.focus();		
	obj.style.backgroundColor ="red";					
	return false;
	}
	obj.style.backgroundColor ="";	
	return true;
}


function checkSelection(object)
{
	var nonchecked = true;
	for(i=1;i<object.length;i++)
	{
		if(object[i].checked)
			nonchecked = false;
	}
	
	if(nonchecked)
		object[0].checked = true;
	else
		object[0].checked = false; 
}

function checkConfirm(confirmObj,orgObj)
{
	if(checkEmail(orgObj))
	{
		if(confirmObj.value != orgObj.value)
		{
			alert ("Please enter the correct email address");
			confirmObj.focus();		
			confirmObj.style.backgroundColor ="red";		
			return false;		
		}
		else
		{
			confirmObj.style.backgroundColor ="";		
			return true;			
		}
	}
}	

