/*
 * Common JavaScript utility functions. These functions depend on the appropriate 'common_msgs.js'
 * which contains the translatable messages that are displayed by these functions.
 */
var popup = null;
function setFocus(form,field) 
{ 
	if (form != '') {
		try	{document.forms[form][field].focus();} catch(e) {}
	}
	else {
		try	{document.forms[0][field].focus();} catch(e) {}
	}
} 

function winpop(loc,w,h,scroll) {
	var name = loc.replace(/\W/g, "");
	window.open(loc,name,'width='+w+', height='+h+', location=no, directories=no, menubar=no, scrollbars='+scroll+', resizable=no, status=no, toolbar=no');
}

function newpop(url,name, style) {
	var win=window.open(url,name,style);
	try{ win.focus();} catch(e) {}
}

/* Add an onload function */
var gOnload = new Array();
function addOnload(f)
{

	if  (window.onload)
	{
		if (window.onload != runOnload)
		{
			gOnload[0] = window.onload;
			window.onload = runOnload;
		}		
		gOnload[gOnload.length] = f;
	}
	else
		window.onload = f;
}
function runOnload()
{
	for (var i=0;i<gOnload.length;i++)
		gOnload[i]();
}

/* Trim the whitespace from beginning and the end of the given string. */
function trim(str)
{
	return str.replace(/^\s+/,'').replace(/\s+$/,'');
}

/* Add a trim method to String's. */
function strtrim()
{
	return trim(this);
}
String.prototype.trim = strtrim;

/** Changes the action for the given form and calls submit. */
function submitForm(form, action)
{
  form.action = action;
  form.submit();
}
/** Replace the current page in the browser history with a call to the page with 
    form params encoded as GET parameters */
function locationReplaceForm(form, url) {
	url += '?';
	var tot = form.elements.length;
	for (var i = 0; i < tot; i++) {
	    var e = form.elements[i];
		if (! isEmpty(e)) {
			url += e.name + '=' + e.value;
			if (i < tot -1) {
				url+='&';
			}
		}
	}
	location.replace(url);	
}


function isEmpty(field) {
	if (field.type=='checkbox'||(field[0]&&field[0].type == 'checkbox')) {
		return !isCheckBoxChecked(field);
	}
	if (field.type=='radio'||(field[0]&&field[0].type == 'radio')) {
		return !isCheckBoxChecked(field);
	}
	//Try trim - will fail for input type="file".
	try
	{
		field.value = field.value.trim();
	}
	catch(e) {}
	if (field.value.length == 0) {
		return true;
	}
}

function isCheckBoxChecked(field) {
  if (field[0]) {
    for (i = 0;i<field.length;i++) {
      theField = field[i];
    	if (theField.checked) {
        return true; 	
    	}
    }
  	return false;   
  }
  else {
    if (!field.checked) {
    	return false;    	
  	}
  }
	return true;
}
/** Validates required field */
function validateRequiredField(field, name, dv)
{
	//Try trim - will fail for input type="file".
	try
	{
		field.value = field.value.trim();
		dv = dv.trim();
	}
	catch(e) {}

	if (field.value.length == 0 || field.value == dv)
	{
    	alert(MSG_REQ_FIELD.replace('%1', name));
    	try{field.focus();}catch(e){}
    	return false;
	}
	return true;
}

/** Validates that at least one checkbox in the form with the input field name is checked. */
function validateRequiredCheckbox(field, name, msg) {
  if (!isCheckBoxChecked(field)) {
		alert(msg.replace('%1', name));
		try{field.focus();}catch(e){}
		return false;    	
  }
	return true;
}

/** Validates required drop down */
function validateRequiredSelect(field, name, defaultValue) {
	if (field.value == null || field.value == '' || field.value == defaultValue) {
    	alert(MSG_REQUIRED_SELECT.replace('%1', name));
    	try{field.focus();}catch(e){}
    	return false;    	
	}
	else {
		return true;
	}
}

/** Validate that a field is less than or equal to the max length. */
function validateMaxLength(field, name, maxLength)
{
	var value = field.value;
	value = value.replace(/\n/g,'**'); // bug #4830 when the javascript validates it sees \n's and java validates it sees \r\n's so a string may pass javascript validation but fail java validation, solution validate on a copy of the string with all \n's replaced with 2 characters to simulate the java length
	if (value.length > maxLength)
	{
		var msg = MSG_MAX_LENGTH.replace('%1', name);
		msg = msg.replace('%2', maxLength);
    	alert(msg);
    	try{field.focus();}catch(e){}
    	return false;
	}
	return true;
}

/** Validate that a field is greater than or equal to the min length. */
function validateMinLength(field, name, minLength) {
	if (field.value.length < minLength) {
		var msg = MSG_MIN_LENGTH.replace('%1', name);
		msg = msg.replace('%2', minLength);
    	alert(msg);
    	try{field.focus();}catch(e){}
    	return false;    	
	}
	else {
		return true;
	}
}

/** Validates the email field.  If the field is not valid, focus is given to that field. */
function validateEmailField(emailField, name)
{
  emailField.value = emailField.value.trim();

  if (!checkEmail(emailField.value)) {
    alert(MSG_INVALID_EMAIL.replace('%1', emailField.value));
    try{emailField.focus();}catch(e){}
    return false;
  }

  return true;
}


/** Validates multiple email fields.  If the field is not valid, focus is given to that field. */
function validateMultipleEmailField(field, name, max) {
	field.value = field.value.trim();
	field.value = field.value.replace(/;/g, ',');
	field.value = field.value.replace(/,+/g, ',');
	field.value = field.value.replace(/^,/, '');
	field.value = field.value.replace(/,$/, '');
	//TODO: could check for duplicates...
	var array = field.value.split(",");
	if (array.length > max) {
	    alert(MSG_TOO_MANY_EMAILS_ADDRESSES.replace('%1', field.name).replace('%2', max));
	    try{field.focus();}catch(e){}
	    return false;
	}

	for (var i = 0 ; i < array.length ; i++) {
		array[i] = array[i].trim();
		if (!checkEmail(array[i])) {
		    alert(MSG_INVALID_EMAIL.replace('%1', array[i]));
		    try{field.focus();}catch(e){}
		    return false;
		}
	}
	return true;
}


/** Checks that a field contains only alphanumeric values */
function validateAlphaNumeric(field, name)
{
  var mask = /^[_0-9a-zA-Z-]*[_0-9a-zA-Z-]$/
  if (!mask.test(field.value)) {
    alert(MSG_ALPHA_NUMERIC.replace('%1', name));
    try{field.focus();}catch(e){}
    return false;
  }
  return true;
}

function validateAlphaNumeric_search(field, name)
{
  var mask = /^[_0-9a-zA-Z-*]*[_0-9a-zA-Z-*]$/
  if (!mask.test(field.value)) {
    alert(MSG_ALPHA_NUMERIC.replace('%1', name));
    try{field.focus();}catch(e){}
    return false;
  }
  return true;
}

/** Checks that a field contains only alphanumeric values */
function validateAlphaNumericWS(field, name)
{
  var mask = /^[_0-9a-zA-Z-\s]*[_0-9a-zA-Z-\s]$/
  if (!mask.test(field.value)) {
    alert(MSG_ALPHA_NUMERIC.replace('%1', name));
    try{field.focus();}catch(e){}
    return false;
  }
  return true;
}

/** Checks that a field contains only numeric values */
function validateNumeric(field, name)
{
  var val = trim(field.value);
  field.value = val;
  var mask = /^-?[0-9\s]*[0-9\s]$/
  if (!mask.test(val)) {
    alert(MSG_NUMERIC.replace('%1', name));
    try{field.focus();}catch(e){}
    return false;
  }
  return true;
}

/** Checks that a field contains only numeric values and is > a minimum value. */
function validateMinNumeric(field, name, minValue)
{
  if (!validateNumeric(field, name)) return false;
  var value = trim(field.value);
  if (value < minValue) {
    alert(MSG_NUMERIC_MIN.replace('%1', name).replace('%2', minValue));
    try{field.focus();}catch(e){}
    return false;
  }
  return true;
}

/** Checks that a field contains only numeric values and is < a maximum value. */
function validateMaxNumeric(field, name, maxValue)
{
  if (!validateNumeric(field, name)) return false;
  var value = trim(field.value);
  if (value > maxValue) {
    alert(MSG_NUMERIC_MIN.replace('%1', name).replace('%2', maxValue));
    try{field.focus();}catch(e){}
    return false;
  }
  return true;
}

/** Checks that a field contains only numeric values */
function validateNumericWS(field, name)
{
  var val = trim(field.value);
  var mask = /^[0-9-\s]*[0-9-\s]$/
  if (!mask.test(val)) {
    alert(MSG_NUMERIC.replace('%1', name));
    try{field.focus();}catch(e){}
    return false;
  }
  return true;
}

/** Checks that a field contains only alphanumeric values */
function validateAlpha(field, name)
{
  var mask = /^[_a-zA-Z-\s~]*[_a-zA-Z-\s~]$/
  if (!mask.test(field.value)) {
    alert(MSG_ALPHA.replace('%1', name));
    try{field.focus();}catch(e){}
    return false;
  }
  return true;
}

/** Validates the first field is greater than or equal to the second field*/
function confirmNoLess(field,name,field2,name2) 
{
	if (Number(field.value) < Number(field2.value))
	{
		var msg = MSG_TO_AGE_MUST_BIGGER.replace('%1',name);
		msg = msg.replace('%2',name2);
		alert(msg);
	    field.focus();
		return false;
	}else
	{
		return true
	}
}

/** Validates that two fields have the same value and ask for confirmation*/
function confirmTwoFields(field,name,field2,name2) 
{
	if (field.value == field2.value)
	{
		var msg = MSG_CONFIRM_TWO_FIELDS;
	    field.focus();
		return confirm(msg);
	}else
	{
		return true
	}
	
}

/** Validates that at least one of the specified fields is present */
function validateAtLeastOneField(field,name,field2,name2) {
	if (isEmpty(field)&&isEmpty(field2))
	{
			var msg = MSG_AT_LEAST_ONE_FIELD.replace('%1', name);
			msg = msg.replace('%2', name2)
    	alert(msg);
    	try{field.focus();}catch(e){}
    	return false;
	}
	return true;
}

/** Validates that two fields have the same value */
function validateTwoFields(field,name,field2,name2) {
	if (field.value != field2.value){
		var msg = MSG_TWO_FIELDS.replace('%1', name);
		msg = msg.replace('%2', name2);
	    alert(msg);
	    try{field.focus();}catch(e){}
	    return false;
	}
	return true;
}

/** Validates that two fields have the same value, disregarding case */
function validateTwoFieldsIgnoreCase(field,name,field2,name2) {
	if (field.value.toLowerCase() != field2.value.toLowerCase()){
		var msg = MSG_TWO_FIELDS.replace('%1', name);
		msg = msg.replace('%2', name2);
	    alert(msg);
	    try{field.focus();}catch(e){}
	    return false;
	}
	return true;
}

/** Validates that two fields do not have the same value */
function validateNotTwoFields(field,name,field2,name2) {
	if (field.value == field2.value){
		var msg = MSG_NOT_TWO_FIELDS.replace('%1', name);
		msg = msg.replace('%2', name2);
	    alert(msg);
	    try{field.focus();}catch(e){}
	    return false;
	}
	return true;
}

/**
 * Reference: Sandeep V. Tamhankar (stamhankar@hotmail.com),
 * http://javascript.internet.com
 */
function checkEmail(emailStr) {
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			return false;
	   }
	}

	if (user.match(userPat)==null) {
		return false;
	}

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false;
			}
		}
		return true;
	}

	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			return false;
		}
	}

	if (domArr[len-1].length < 2) {
		return false;
	}

	if (len<2) {
		return false;
	}

	var mask=/@date.com/i;
	if (mask.test(emailStr.toLowerCase())) {
		return false;
	}
	mask=/@date.net/i;
	if (mask.test(emailStr.toLowerCase())) {
		return false;
	}
	mask=/^(root|abuse|webmaster|help|postmaster|sales|resumes|contact|advertising)@/i;
	if (mask.test(emailStr.toLowerCase())) {
		return false;
	}

	return true;
}


/** Validates a URL. */
function validateURL(field)
{
  var str = field.value;
  
  if ( str.indexOf("*") != -1 ||
       str.indexOf('"') != -1 ||
       str.indexOf("'") != -1 ) {
    alert(MSG_INVALID_URL);
    try{field.focus();}catch(e){}
    return false;
  }
  return true;
}

/** Returns true if the string only contains digits. */
function validateNumber(str, scale, precision)
{
  if (precision == 0) {
    var format = new RegExp('^\\s*\\d{0,'+scale+'}\\s*$');
    return format.test(str);
  }
  
  var format = new RegExp('^\\s*\\d{0,'+(scale-precision)+'}\\.\\d{0,'+precision+'}\\s*$');
  if ( format.test(str) ) {
    return true;
  } else {
    format = new RegExp('^\\s*\\d{0,'+scale+'}\\s*$');
    return format.test(str);
  }
}

/** THE FOLLOWING ARE NOT CURRENTLY IN USE **/
/** Validates a date string returns true if it is of the form MM/DD/YYYY. */
function validateDate(str)
{
  // Validate format
  var dateformat = /^\s*\d{1,2}\/\d{1,2}\/\d{2,4}\s*$/;  // MM/DD/YYYY

  if ( !dateformat.test(str) ) {
    alert(MSG_INVALID_DATE_FORMAT);
    return false;
  }

  var elements = str.split('/');
  
  // check month
  var month = elements[0];
  if (month < 1 || month > 12) {
    alert(MSG_INVALID_MONTH.replace('%1', str));
    return false;
  }

  // check day
  var day = elements[1];
  if (!validDate(month, day)) {
    alert(MSG_INVALID_DAY.replace('%1', str));
    return false;
  }

  // check year
  var year = elements[2];
  if (year < 100 && year > 50) year += 1900;
  else if (year > 0 && year < 50) year += 2000;

  if (year < 1900) {
    alert(MSG_INVALID_YEAR.replace('%1', str));
    return false;
  }  


  return true;
}

/** Checks to make sure the number of days in the month is correct.  This does 
    not work in all cases (ie February) */
function validDate(month, value) 
{
  var monthMax = new Array (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) ;
  month = month - 1;

  var top = monthMax[month];
  if (value > top) return false; // value is greater than highest for the month
  else return true;
}

var bname = navigator.appName;
var bver = parseInt(navigator.appVersion);

// provides focus for specific form element on page load
function giveFocus(frm, elm)
{
  eval("document."+frm+"."+elm+".focus()");
}

/** Toggles the highlighting for a row table. */
function toggleRowHighlight(cb)
{
  var e = cb;
  if (document.all?1:0) {  // IE vs Netscape
    while (e.tagName!="TR") {
      e=e.parentElement;
    }
  }
  else {
    return;  // row highlight not supported for Netscape
    //while (e.tagName!="TR") {
    //  e=e.parentNode;
    //}
  }

  if (cb.checked) e.className = "H";
  else e.className = "";
}

/** Given a select list, this will sort the options in alphabetical order. */
function sortOptions(src)
{
  var list = new Array();
  for (var i = 0; i < src.length; i++) {
    var opt = new Option(src[i].text, src[i].value, false, true);
    opt.selected = src[i].selected;
    list[i] = opt;
  }

  list.sort(compareOptions);

  src.options.length=0;
  for (var i = 0; i < list.length; i++) {
    //var opt = new Option(list[i].text, list[i].value, false, true);
    src.options[src.length] = list[i];
  }
}

/** Compares to options. */
function compareOptions(a,b)
{
  if (a.text < b.text) return -1;
  if (a.text > b.text) return 1;
  return 0;
}

function getById(tag)
{
  if (document.getElementById) //  Netscape, Mozilla, etc. 
  {
    return document.getElementById(tag);
  }
  else if (document.all)      //  IE, Konqueror, etc.
  {
	return document.all[tag];
  }
}

function displaydiv(div1_id, div2_id, form)
{
	if (document.getElementById){
	    if(!document.getElementById(div1_id)) return ;
	    if(!(document.getElementById(div1_id).style)) return ;
	    if(!(document.getElementById(div1_id).style.display)) return ;
	    
		var state1 = document.getElementById(div1_id).style.display;
		if(state1=="none") {
        		document.getElementById(div1_id).style.display="block";
        		document.getElementById(div2_id).style.display="none";		
                if (form != null)
                {
                   form[div1_id].value='true';
                   form[div2_id].value='false';
                 }
	     }
	}
	else if (document.all)	{
	    if(!document.all[div1_id]) return ;
	    if(!(document.all[div1_id].style)) return ;
	    if(!(document.all[div1_id].style.display)) return ;
	
		var state1 = document.all[div1_id].style.display;	
		if(state1=="none") {
		        document.all[div1_id].style.display = "block";
		        document.all[div2_id].style.display = "none";
                if (form != null)
                {
                   form[div1_id].value='true';
                   form[div2_id].value='false';  
                }			
		}	
    }
}

function switchdiv(div1_id, div2_id, form)
{
	if (document.getElementById)	{
	    if(!document.getElementById(div1_id)) return ;
	    if(!(document.getElementById(div1_id).style)) return ;
	    if(!(document.getElementById(div1_id).style.display)) return ;
	
		var state1 = document.getElementById(div1_id).style.display;
		if(state1=="none") {
        		document.getElementById(div1_id).style.display="block";
        		document.getElementById(div2_id).style.display="none";		
                if (form != null)
                {
                   form[div1_id].value='true';
                   form[div2_id].value='false';
                 }
	     }
	    if(state1=="block") {
        		document.getElementById(div2_id).style.display="block";		
        		document.getElementById(div1_id).style.display="none";
                if (form != null)
                {
                   form[div1_id].value='false';
                   form[div2_id].value='true';
                 }
	     }
	}
	else if (document.all)	{
	    if(!document.all[div1_id]) return ;
	    if(!(document.all[div1_id].style)) return ;
	    if(!(document.all[div1_id].style.display)) return ;
	
		var state1 = document.all[div1_id].style.display;	
		if(state1=="none") {
		        document.all[div1_id].style.display = "block";
		        document.all[div2_id].style.display = "none";
                if (form != null)
                {
                   form[div1_id].value='true';
                   form[div2_id].value='false';
                 }			
		}	
		if(state1=="block") {
        		document.getElementById(div1_id).style.display="none";
        		document.getElementById(div2_id).style.display="block";		
                if (form != null)
                {
                   form[div1_id].value='false';
                   form[div2_id].value='true';
                 }
	     }
    }
}

function getRefToDiv(divID) {
    if( document.layers ) { //Netscape layers
        return document.layers[divID]; }
    if( document.getElementById ) { //DOM; IE5, NS6, Mozilla, Opera
        return document.getElementById(divID); }
    if( document.all ) { //Proprietary DOM; IE4
        return document.all[divID]; }
    if( document[divID] ) { //Netscape alternative
        return document[divID]; }
    return false;
}

function selectAll(field, state)
{
	if(field == null)
	{
		return;
	}

	if(field.length == null)
	{
		field.selected = field.checked = state;
	}

	for (var i = 0 ; i < field.length ; i++)
	{
		field[i].selected = field[i].checked = state;
	}
}

function checkedCount(field)
{
	var	checked	= 0;

	if (field != null) {
		if (field.length == null)
		{
			if (field.checked == true)
			{
				checked++;
			}
		}
		else
		{
			for	(var i = 0 ; i < field.length	; i++)
			{
				if (field[i].checked == true)
				{
					checked++;
				}
			}
		}
	}
	
	return checked;
}

function isChecked(field)
{
	if (checkedCount(field) == 0)
	{
		return false;
	}
	return true;
}

function selectedCheck(field){
    var checkCounter = 0;
    if(!field.length) return field.value;
    for (i = 0; i < field.length; i++)
    {
    	if (field[i].checked){
		    return field[i].value;
		    break;
		}
    }
}        

function isOneChecked(field)
{
	if (checkedCount(field) == 1)
	{
		return true;
	}
	return false;
}

function addToDate(formName,yearName,monthName,dayName,offset)
{
	var form = document.forms[formName];
	var yearSelect = form[yearName];
	var monthSelect = form[monthName];
	var daySelect = form[dayName];
	var year = yearSelect[yearSelect.selectedIndex].value;
	var month = monthSelect[monthSelect.selectedIndex].value;
	var day = daySelect[daySelect.selectedIndex].value;
	var date = new Date(year,month-1,day);
	date.setDate(date.getDate()+offset);
	daySelect[daySelect.selectedIndex].value = date.getDate();
	monthSelect[monthSelect.selectedIndex].value = date.getMonth()+1;
	yearSelect[yearSelect.selectedIndex].value = date.getYear();
}

function updateDay(change,formName,yearName,monthName,dayName)
{
	var form = document.forms[formName];
	var yearSelect = form[yearName];
	var monthSelect = form[monthName];
	var daySelect = form[dayName];
	var year = yearSelect[yearSelect.selectedIndex].value;
	var month = monthSelect[monthSelect.selectedIndex].value;
	var day = daySelect[daySelect.selectedIndex].value;

	if (change == 'month' || (change == 'year' && month == 2))
	{
		var i = 31;
		var flag = true;
		while(flag)
		{
			var date = new Date(year,month-1,i);
			if (date.getMonth() == month - 1)
			{
				flag = false;
			}
			else
			{
				i = i - 1;
			}
		}

		daySelect.length = 0;
		daySelect.length = i;
		var j = 0;
		while(j < i)
		{
			daySelect[j] = new Option(j+1,j+1);
			j = j + 1;
		}
		if (day <= i)
		{
			daySelect.selectedIndex = day - 1;
		}
		else
		{
			daySelect.selectedIndex = daySelect.length - 1;
		}
	}
}

function checkCR(formName,e)
{
	if (e.keyCode == 13) {
		if(eval('validate'+formName+'()'))
		document.forms[formName].submit();
	}
}

function checkCRPro(formName,e,custFunc)
{
	if (e.keyCode == 13) {
		if(eval('validate'+formName+'()'))
		{
			eval(custFunc+'()');
			document.forms[formName].submit();
		}
	}
}

function checkIt(obj){
	if(obj == null) return;
	obj.checked='true';
}


//pop-under
var flag = "1";

function clearFlag()
{
	flag = "0";
}

function pop()
{
	if(flag == '0')
	{
		winme=window.open('/jsp/common/popad.jsp','','toolbar=no,location=no,scrollbars=no,resizable=no');
	}
}

function setFlag()
{
	flag++;
}

// disable right-click
//var mes_disable_right="Function not supported.";
var mes_disable_right="Function not supported.";
function clickIE4()
{
	if (event.button==2)
	{
		alert(mes_disable_right);
		return false;
	}
}

function clickNS4(e)
{
	if (document.layers||document.getElementById&&!document.all)
	{
		if (e.which==2||e.which==3)
		{
			alert(mes_disable_right);
			return false;
		}
	}
}

function disable_right_click()
{
	if (document.layers)
	{
		document.captureEvents(Event.MOUSEDOWN);
		document.onmousedown=clickNS4;
	}else if (document.all&&!document.getElementById)
	{
		document.onmousedown=clickIE4;
	}
	document.oncontextmenu=new Function("alert(mes_disable_right);return false")
}

/*
 * Returns true if the field (a state or city required field) is valid
 * (if the location/zip radio has chosen to specify the location by
 * location rather than zip).
 */
function validateLocationStateCity(field, name, defaultValue, radioField) {
	if (typeof radioField == 'undefined') {
		return validateRequiredSelect(field, name, defaultValue);
	}
	else {
		if (radioField[0].checked) { // Zip field checked.
			return true;
		}
		else {
			return validateRequiredSelect(field, name, defaultValue);		
		}
	}
}

function validateLocationZip(field, name, countryField, radioField) {
	// Zip field may not be present.
	if (typeof field == 'undefined') {
		return true;
	}
	if (typeof radioField == 'undefined') {
		return validateZip(field, name, countryField);
	}
	else {
		if (radioField[0].checked) {
			var zip = field.value.replace(/[-\s]+/,"");
			for(i = 0; i < zip.length; ++i) {
				zip = zip.replace(/[-\s]+/,"");
			}
			if (trim(zip).length == 0) {
				alert(MSG_REQ_FIELD.replace('%1', name));
				try {
					field.focus();
				}
				catch(e) {
					// Do nothing.
				}
				return false;
			}
			else {
				return validateZip(field, name, countryField);
			}
		}
		else {
			return true;
		}
	}
}

function validateZip(field, name, countryField) 
{
  var country;
  if (countryField.type == "select-one") {
    country = countryField[countryField.selectedIndex].value;
  }
  else {
  	country = countryField.value;
  }

  // Only validate Canada and US
  if ( country!='CA' && country != 'US' && country!='Canada' && country!='USA') {
    return true;
  }

  var value = field.value.replace(/[-\s]+/,"");

  // Required Field for US and Canada
  value = trim(value);  
  if (value.length == 0) {
    alert(MSG_REQ_FIELD.replace('%1', name));
    try{field.focus();}catch(e){}
    return false;    	
  }

  if(country=='CA' || country=='Canada') {
  	return validatePostalCode(field, name)
  } else if(country=='US' || country=='USA') {
    return validateZipCode(field, name)
  }
}

function validateZipCode(field, name) {
	// Remove hyphen and white space
	var value = field.value.replace(/[-\s]+/,"");
	for (i=0;i<value.length;++i) {
		value= value.replace(/[-\s]+/,"");
	}
	field.value = value;

	if(value.length==5){
		var expr = new RegExp("^[0123456789]{5}");
	} else if(value.length==9){
		var expr = new RegExp("^[0123456789]{9}");
	} else {
		alert(MSG_ZIP_5);
		errormessage=true;
		try{field.focus();}catch(e){}
		return false;
	}

	if (!expr.test(value)){
		alert(MSG_INVALID_ZIP_CODE_FORMAT);
		errormessage=true;
		try{field.focus();}catch(e){}
		return false;
	}
	return true;
}

function validatePostalCode(field, name) {
	// Remove hyphen and white space
	var value = field.value.replace(/[-\s]+/,"");
	for (i=0;i<value.length;++i) {
		value = value.replace(/[-\s]+/,"");
	}
	field.value = value;

	if (value.length == 6) {
		expr = new RegExp("^[a-zA-Z]{1}[0123456789]{1}[a-zA-Z]{1}[0123456789]{1}[a-zA-Z]{1}[0123456789]{1}");
	} else {
		alert(MSG_INVALID_POSTAL_CODE_FORMAT);
		errormessage=true;
		try{field.focus();}catch(e){}
		return false;
	}

	if (!expr.test(value)){
		alert(MSG_INVALID_POSTAL_CODE_FORMAT);
		errormessage=true;
		try{field.focus();}catch(e){}
		return false;
	}
	return true;
}



//IM related DHTML
  IE4 = (document.all) ? 1 : 0; // initialize browser..
  NS4 = (document.layers) ? 1 : 0; // identification and...
  NS6 = (document.getElementById) ? 1 : 0;
  ver4 = (IE4 || NS4 || NS6) ? 1 : 0; // DHTML variables
  if(NS4){
   layerRef = "parent.document.layers";
   styleSwitch = "";
  }else if (IE4){
   layerRef = "parent.document.all";
   styleSwitch = ".style";
  }else if (NS6) {
   layerRef = "parent.document.getElementById";
   styleSwitch = ".style";
  }
  
  
  var win_width = 300, win_height = 200;
  if( typeof( window.innerWidth ) == 'number' ) {
      //Non-IE
       win_width = window.innerWidth;
       win_height = window.innerHeight;
  }else if( document.documentElement &&
     ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
       //IE 6+ in 'standards compliant mode'
        win_width = document.documentElement.clientWidth;
        win_height = document.documentElement.clientHeight;
  }else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        win_width = document.body.clientWidth;
        win_height = document.body.clientHeight;
  }
  x = (win_width-230)/2;
  y = (win_height-160)/2;
  if(x<0) x=0
  if(y<0) y=0

  
  function showHideLayer(layerName, visibility){
    try{
	  if (NS6) 
	     eval('parent.document.getElementById("' +layerName + '")' +styleSwitch+'.visibility = "'+visibility+'"');
		else 
		   eval(layerRef+'["'+layerName+'"]'+styleSwitch+'.visibility = "'+visibility+'"');
		   }catch(e) {}
  }
  function closeIMPopup(otherhandle){
  	  if (NS6)
	     eval('parent.document.getElementById("closeimpopup")'+'.src="/UpdatePendingIMStatus.do?status=8&otherhandle='+otherhandle+'"');
      else 
		   eval(layerRef+'["closeimpopup"]'+'.src="/UpdatePendingIMStatus.do?status=8&otherhandle='+otherhandle+'"');
		  showHideLayer('imalert', 'hidden');
  }
  imBegin = '<iframe id="closeimpopup" name="closeimpopup" src="" scrolling=no width=1 height=1 frameborder=0 marginheight=0 marginwidth=0 hspace=0 vspace=0></iframe>' +
                '<DIV ID="imalert" ALIGN=CENTER STYLE="POSITION: ABSOLUTE; TOP:'+y+'; LEFT:'+x+';WIDTH:230; HEIGHT:160; BORDER: solid 1px #000000; BACKGROUND:#4C6E89;VISIBILITY:hidden; Z-INDEX:10;">'+
                '<DIV style="HEIGHT: 5px;"><img src="images/spacer.gif" border=0></DIV>'+
                '<DIV align=center style="width: 217px; border: solid 1px #395266; border-right: solid 2px #395266; border-bottom: solid 2px #395266; background: #FFFFCC; padding-top: 10px; padding-bottom: 11px;">'+
                '  <img src="dyn-images/datelogo_im.gif" border=0 style="margin-bottom: 7px;">'+
                '  <div style="height: 1px; width: 74px; background: #E5E5E5;"><img src="images/spacer.gif" border=0></div>' +
                '<div ID="imdhtml" style="padding-top: 9px; padding-bottom: 13px; font-family: Arial; font-size: 9pt; color: #000000;">';
                
  function imMiddle(handle, otherhandle) {
    return  '<span class="head2">'+otherhandle+'</span> is requesting an IM conversation.'+
            '   <table><tr>' +
            '      <td align=center><div style="border: solid 1px #8F0100; width: 75px;"><div style="width: 73px; padding-top: 2px; padding-bottom: 2px; border: solid 1px #FFFFFF; background: #8F0100;"><a href="javascript:responseIC(\''+handle+'\',\''+otherhandle+'\')" style="font-family: Verdana; font-size: 7.5pt; text-decoration: none; text-transform: uppercase; color: #FFFFFF; font-weight: bold;">Accept IM</a></div></div></td>'+
            '      <td with=10></td>' +
            '      <td align=center><div style="border: solid 1px #8F0100; width: 75px;"><div style="width: 73px; padding-top: 2px; padding-bottom: 2px; border: solid 1px #FFFFFF; background: #8F0100;"><a href="javascript:closeIMPopup(\''+otherhandle+'\')" style="font-family: Verdana; font-size: 7.5pt; text-decoration: none; text-transform: uppercase; color: #FFFFFF; font-weight: bold;">Close</a></div></div></td>'+
            '    </tr></table>';
   }
                
  imEnd =   '</div></div><div align=right style="height: 18px; padding-top: 1px; padding-right: 10px;"><a href="/EditSettings.do#imsettings" style="font-family: Arial; font-size: 8pt; color: #FFFFFF; text-decoration: none;">Settings</a>'+
            ' | <a href="/imsupport.do" style="font-family: Arial; font-size: 8pt; color: #FFFFFF; text-decoration: none;">Help</a>'+
            '</div>'+
            '</DIV>';
  
  function imAlert(handle, otherhandle){
        document.write(imBegin+imMiddle('','')+imEnd);
  }
  function writeTabLayer(name, w, h,z, handle, otherhandle){
      if(handle.length>0&&otherhandle.length>0)
        document.write(imBegin+imMiddle(handle, otherhandle)+imEnd);
      else
        document.write(imBegin+imEnd);
  }


	function requestIC( userID, destinationUserID )
	{
		var popupWindowTest = window.open( "/StartIM.do?otherhandle=" + destinationUserID+"&winOpener=main&recheck=true", "ICWindow_" + replaceAlpha(userID) + "_" + replaceAlpha(destinationUserID), "width=360,height=420,toolbar=0,directories=0,menubar=0,status=0,location=0,scrollbars=0,resizable=0" );
		if( popupWindowTest == null )
		{
        showHideLayer('imalert', 'visible');
		}else{
		    popupWindowTest.focus();
		}
	}
	function showNewIM(handle, otherhandle) {
	  try{
	     parent.document.getElementById('imdhtml').innerHTML=imMiddle(handle, otherhandle);
		}catch(e) {}
	
	}
	
	function responseIC( userID, destinationUserID )
	{
		var popupWindowTest = window.open( "/ResponseIM.do?response=accept&otherhandle=" + destinationUserID+"&winOpener=iframe", "ICWindow_" + replaceAlpha(userID) + "_" + replaceAlpha(destinationUserID), "width=360,height=420,toolbar=0,directories=0,menubar=0,status=0,location=0,scrollbars=0,resizable=0" );
		if( popupWindowTest == null )
		{
		    showNewIM(userID, destinationUserID);
		    showHideLayer('imalert', 'visible');
    }else{
        showHideLayer('imalert', 'hidden');
		    popupWindowTest.focus();
		}
	}
	function replaceAlpha( strIn )
	{
		var strOut = "";
		for( var i = 0 ; i < strIn.length ; i++ )
		{
			var cChar = strIn.charAt(i);
			if( ( cChar >= 'A' && cChar <= 'Z' )
				|| ( cChar >= 'a' && cChar <= 'z' )
				|| ( cChar >= '0' && cChar <= '9' ) )
			{
				strOut += cChar;
			}
			else
			{
				strOut += "_";
			}
		}
		
		return strOut;
	}

  // determine if is Windows IE (up_is_win_ie)
  var up_agt 			= navigator.userAgent.toLowerCase();
  var up_appVer 		= navigator.appVersion.toLowerCase();
  var up_is_mac 		= up_agt.indexOf('mac') != -1;
  var up_is_safari 	= up_agt.indexOf('safari') != -1 && up_is_mac;
  var up_is_khtml  	= up_is_safari || up_agt.indexOf('konqueror') != -1;
  var up_is_ie  	 	= up_appVer.indexOf('msie') != -1 && up_agt.indexOf("opera") == -1 && !up_is_khtml;
  var up_is_win   	= up_is_mac ? false : (up_agt.indexOf("win") != -1 || up_agt.indexOf("16bit") != -1);
  var up_is_win_ie 	= up_is_win && up_is_ie; 
  

  var imrecheck=0;
  var up_iCheckSeconds = 15;
  var up_icCheckImage = null;
  var up_timeoutID = null;
  var newim='0';
  
  function up_checkIC()
  {
	  if( up_is_win_ie )
	  {
	    imrecheck++;
	    
		  up_icCheckImage = new Image();
		  up_icCheckImage.onLoad = up_onImageLoad();
		  up_icCheckImage.src = "/CheckPendingIM.do?recheck=" + imrecheck+"&rand="+Math.floor( Math.random() * 100000000000) ;
	  }
	  else
	  {
	    getById("IM").src="/ListPendingIM.do?refresh=true";
	  }
  }
  
  function initIM() {
    //Preload images
    pixel1 = new Image();
    pixel1.src="/images/pixel1.jpg";
    pixel2 = new Image();
    pixel2.src="/images/pixel2.jpg";
    pixel3 = new Image();
    pixel3.src="/images/pixel3.jpg";
    
    if(newim=='0'){//No new IM
		  setTimeout("up_checkIC()", 1000 * up_iCheckSeconds);
    }else{ //New IM
      up_checkIC();
    }
  }

  function up_onImageLoad()
  {
	  clearTimeout( up_timeoutID );
	
	  if (!up_icCheckImage.complete)
	  {
		  up_timeoutID = setTimeout("up_onImageLoad()", 250);
	  }
	  else
	  {
		  if( up_icCheckImage.height == 2)
		  {
		     getById("IM").src="/ListPendingIM.do?refresh=false";
		     up_timeoutID = setTimeout("up_checkIC()", 1000 * up_iCheckSeconds);
		  }
		  else if( up_icCheckImage.height == 3 )
		  {
          try{
            document.getElementById('IM-interface').style.display='none';
            document.getElementById('temponline').style.display='none';
            document.getElementById('tempoffline').style.display='block';
          }catch(e) {}
		     //Stop refresh
		  }
		  else
		  {
          try{
            document.getElementById('IM-interface').style.display='none';
          }catch(e) {}
		   	
		   	up_timeoutID = setTimeout("up_checkIC()", 1000 * up_iCheckSeconds);
		  }
	 }
  }

/** Validates Mobile Number. */
function validateMobileNumberField(field, name, countryField) {
	var value = field.value;
	try {
		value = value.trim();
	} catch(e) {}
	field.value = value;

	if (value.length != 0) {
		value = value.replace(/[()\-\.\s]+/g,'');

		var country = countryField.value;

		var mask = /^[0-9]*$/
		if (!mask.test(value)) {
			alert(MSG_MOBILE_NUMBER_VALID_CHARACTERS.replace('%1', name));
			try{field.focus();}catch(e){}
			return false;
		}

		if (('US' == country || 'CA' == country) && value.length != 10) {
			alert(MSG_MOBILE_NUMBER_NORTH_AMERICA.replace('%1', name));
			try{field.focus();}catch(e){}
			return false;
		}

		if (value.length < 10) {
			var msg = MSG_MIN_LENGTH.replace('%1', name);
			msg = msg.replace('%2', 10);
			alert(msg);
			try{field.focus();}catch(e){}
			return false;
		}
		
		if (value.length > 26) {
			var msg = MSG_MAX_LENGTH.replace('%1', name);
			msg = msg.replace('%2', 26);
			alert(msg);
			try{field.focus();}catch(e){}
			return false;
		}

		if (field.value.length > 48) {
			var msg = MSG_MAX_LENGTH.replace('%1', name);
			msg = msg.replace('%2', 48);
			alert(msg);
			try{field.focus();}catch(e){}
			return false;
		}
	}
	return true;
}

/** Validates Mobile Carrier. */
function validateMobileCarrierField(field, name, dv, numberField) {
	if (!isEmpty(numberField)) return validateRequiredSelect(field, name, dv)
	else return true;
}
