function submitWithRedirect(url)
{
  f = document.frmClient;
  f.redirectUrl.value = url;
  f.submit();
}

function isNull(a) {
  return typeof a == 'object' && !a;
}

function hasCharsInSet(s,setChars)
{
  var hasCharsInSet = true;
  var c;

  for (i=0;i<s.length && hasCharsInSet==true;i++) 
  { 
    c = s.charAt(i); 
    if (setChars.indexOf(c)==-1) 
    {
      hasCharsInSet = false;
    }
  }
return hasCharsInSet;
}

function removeCharsInSet(s,setChars)
{
  var sFiltered;
  var c;

  sFiltered="";
  for (i=0;i<s.length;i++) 
  { 
    c = s.charAt(i); 
    if (setChars.indexOf(c)==-1) 
    {
      sFiltered = sFiltered + c
    }
  }
return sFiltered;
}

function hasDateChars(s)
{
   return hasCharsInSet(s,"0123456789/");
}

function hasIntChars(s)
{
   return hasCharsInSet(s,"0123456789");
}

function hasMoneyChars(s)
{
   return hasCharsInSet(s,"0123456789$,.");
}

function hasRealChars(s)
{
   return hasCharsInSet(s,"0123456789,.");
}

// Accept a date of the form "mm/dd/yy", and return a date
// object if valid, or null on parse error.
function getDateObject(s)
{
  // check for valid date chars only
  if(!hasDateChars(s)) return null;
 
  // split by yy/dd/mm slash seperator
  sArray = s.split('/');
  if(sArray.length!=3) return null;
  
  mm = sArray[0];
  dd = sArray[1];
  yyyy = sArray[2];

  // check for 2 digit year
  if(yyyy.length!=4) return null;
   
  mmInt = parseInt(parseFloat(mm));
  ddInt = parseInt(parseFloat(dd));
  yyyyInt = parseInt(parseFloat(yyyy));
  if(mmInt<1 || mmInt>12) return null;
  if(ddInt<1 || ddInt>31) return null;
  if(yyyyInt<1900 || yyyyInt>2100) return null;
  
  // create a default date with midnight time portion
  var dateObj = new Date("January 01, 2000 00:00:00");
  
  // set fullYear, then month, and day of month last
  dateObj.setFullYear(yyyyInt);
  dateObj.setMonth(mmInt-1);
  dateObj.setDate(ddInt);

  // if invalid date like Feb 31st, the object will roll to Mar 3rd
  // to 'do the right thing' and the date as read back won't match.
  if(ddInt!=dateObj.getDate()) return null;
 
  return dateObj;
}

//Validate date format "mm/yy"
function checkMonthYearDate(fieldName)
{
  f = document.frmClient;
  var dateField = f[fieldName];
  var dateValue = dateField.value;
  var retVal = true;

  if(dateValue.length<1) return true; // if they clear the field, don't validate
  
  // check for valid date chars only
  if(!hasDateChars(dateValue)) retVal = false;
 
  // split by mm/yy slash seperator
  sArray = dateValue.split('/');
  if(sArray.length!=2) retVal = false;
  
  mm = sArray[0];
  yy= sArray[1];

  if(mm == null || yy == null)
  { 
	retVal = false;
  }else if(mm.length!=2 || yy.length!=2)
  { 
	retVal = false;
  }
   
  mmInt = parseInt(parseFloat(mm));
  if(mmInt<1 || mmInt>12)  retVal = false;
   
  if(!retVal)
  {
	alert("Invalid expiration date.");
	dateField.select();
	return retVal;
  }
  return retVal;
}
// Validate a date and range.  On error display a message and return false
function checkStartDate(startFieldName,endFieldName)
{
  f = document.frmClient;
  var startField = f[startFieldName];
  var endField   = f[endFieldName];
  var start = startField.value;
  var end   = endField.value;

  if(start.length<1) return true; // if they clear the field, don't validate
  
  var startDate = getDateObject(start);
  if(isNull(startDate))
  {
	alert("Invalid date");
	startField.select();
	return false;
  }
  
  var endDate = getDateObject(end);
  if(!isNull(endDate) && startDate.getTime()>endDate.getTime())
    {
	  alert("Please confirm that your finish date does not preceed your start date, and follow the date entry format shown in the example.");
	  startField.select();
	  return false;
	}
	
  return true;
}

function checkEndDate(startFieldName,endFieldName)
{
  f = document.frmClient;
  var startField = f[startFieldName];
  var endField   = f[endFieldName];
  var start = startField.value;
  var end   = endField.value;

  if(end.length<1) return true; // if they clear the field, don't validate
  
  var endDate = getDateObject(end);
  if(isNull(endDate))
  {
	alert("Invalid date");
	endField.select();
	return false;
  }
  
  var startDate = getDateObject(start);
  if(!isNull(startDate) && startDate.getTime()>endDate.getTime())
    {
	  alert("Please confirm that your finish date does not preceed your start date, and follow the date entry format shown in the example");
	  endField.select();
	  return false;
	}
	
  return true;
}

function checkDate(fieldName)
{
  f = document.frmClient;
  var dateField = f[fieldName];
  var dateValue = dateField.value;

  if(dateValue.length<1) return true; // if they clear the field, don't validate
  
  var dateObj = getDateObject(dateValue);
  if(isNull(dateObj))
  {
	alert("Invalid date");
	dateField.select();
	return false;
  }
	
  return true;
}

function checkStartYear(startFieldName,endFieldName)
{
  f = document.frmClient;
  var startField = f[startFieldName];
  var endField   = f[endFieldName];
  var start = startField.value;
  var end   = endField.value;

  if(start.length<1) return true; // if they clear the field, don't validate
  
  var startDate = getDateObject( "1/1/" + start);
  if(isNull(startDate))
  {
	alert("Invalid year");
	startField.select();
	return false;
  }
  
  if(end.length<1) return true; // if they clear the field, don't validate

  var endDate = getDateObject( "1/1/" + end);
  if(!isNull(endDate) && startDate.getTime()>endDate.getTime())
    {
	  alert("Invalid Year range");
	  startField.select();
	  return false;
	}
	
  return true;
}

function checkEndYear(startFieldName,endFieldName)
{
  f = document.frmClient;
  var startField = f[startFieldName];
  var endField   = f[endFieldName];
  var start = startField.value;
  var end   = endField.value;

  if(end.length<1) return true; // if they clear the field, don't validate
  
  var endDate = getDateObject( "1/1/" + end);
  if(isNull(endDate))
  {
	alert("Invalid year");
	endField.select();
	return false;
  }
  
  if(start.length<1) return true; // if they clear the field, don't validate

  var startDate = getDateObject( "1/1/" + start);
  if(!isNull(startDate) && startDate.getTime()>endDate.getTime())
    {
	  alert("Invalid Year Range");
	  endField.select();
	  return false;
	}
	
  return true;
}

function checkYear(fieldName)
{
  f = document.frmClient;
  var dateField = f[fieldName];
  var dateValue = dateField.value;

  if(dateValue.length<1) return true; // if they clear the field, don't validate

  if ( dateValue.length != 4 )
  {
	alert("Invalid year");
	dateField.select();
	return false;
  }
  
  var dateObj = getDateObject( "1/1/" + dateValue);
  if(isNull(dateObj))
  {
	alert("Invalid year");
	dateField.select();
	return false;
  }
	
  return true;
}

function checkDigits(field,digits)
{
	if(field.value.length<1) return true; // if they clear the field, don't validate

	if(!hasIntChars(field.value))
	{
		alert("Invalid digits");
		field.select();
		return false;
	}
	
	if(field.value.length!=digits)
	{
		alert("Invalid length");
		field.select();
		return false;
	}
	
	return true;
}

function checkPhone(field,digits)
{
	if(field.value.length<1) return true; // if they clear the field, don't validate

	if(!hasIntChars(field.value))
	{
		alert("Invalid phone number");
		field.select();
		return false;
	}
	
	if(field.value.length!=digits)
	{
		alert("Invalid phone number");
		field.select();
		return false;
	}
	
	return true;
}

function checkInt(field)
{
	if(field.value.length<1) return true; // if they clear the field, don't validate

	if(!hasIntChars(field.value))
	{
		alert("Invalid number");
		field.select();
		return false;
	}
	
	return true;
}

function checkReal(field)
{
	if(field.value.length<1) return true; // if they clear the field, don't validate

	if(!hasRealChars(field.value))
	{
		alert("Invalid number");
		field.select();
		return false;
	}
	
	return true;
}

function checkMoney(field)
{
	if(field.value.length<1) return true; // if they clear the field, don't validate

	if(!hasMoneyChars(field.value))
	{
		alert("Invalid amount");
		field.select();
		return false;
	}

	field.value = removeCharsInSet(field.value,"$,")
	field.value = Math.round(field.value)
	
	return true;
}

function checkZipcode(field,digits)
{
	if(field.value.length<1) return true; // if they clear the field, don't validate

	if(!hasIntChars(field.value))
	{
		alert("Invalid zipcode");
		field.select();
		return false;
	}
	if(field.value.length!=digits)
	{
		alert("Invalid zipcode");
		field.select();
		return false;	
	}
	return true;
}

function textCounter(field,  maxlimit) 
{
	if (field.value.length > maxlimit) // if too long...trim it!
	field.value = field.value.substring(0, maxlimit);
}
function getCurrentState()
{
	var strHref = window.location.href;
	var strState = '';
	
	if(strHref.indexOf("prurealty") >= 0)
		strState = "CA";
	else if(strHref.indexOf("prunevada") >= 0)
		strState = "NV";
	else if(strHref.indexOf("prudentialtexas") >= 0)
		strState = "TX";
	else if(strHref.indexOf("gotucson") >= 0)
		strState = "AZ";
	else
		strState = "CA";
	return strState;
	
}
function validateStateZip(strPostalCode, strState)
{
	// validate postal code
	switch (strState) { 
			case "CA":
				if(strPostalCode.indexOf("90") != 0 && strPostalCode.indexOf("91") != 0 && strPostalCode.indexOf("92") != 0
					&& strPostalCode.indexOf("93") != 0 && strPostalCode.indexOf("94") != 0 && strPostalCode.indexOf("95") != 0
					&& strPostalCode.indexOf("960") != 0 && strPostalCode.indexOf("961") != 0)
				{
					return strPostalCode + " is not a valid California zip code.\n";
				}
				break;
			case "NV":
				if(strPostalCode.indexOf("889") != 0 && strPostalCode.indexOf("89") != 0)
				{
					return strPostalCode + " is not a valid Nevada zip code.\n";
				}
				break;
			case "TX":
				if(strPostalCode.indexOf("733") != 0 && strPostalCode.indexOf("75") != 0 && strPostalCode.indexOf("76") != 0
						&& strPostalCode.indexOf("77") != 0 && strPostalCode.indexOf("78") != 0 && strPostalCode.indexOf("79") != 0
						&& strPostalCode.indexOf("885") != 0)
				{
					return strPostalCode + " is not a valid Texas zip code.\n";
				}
				break;
			case "AZ":
				if (strPostalCode.indexOf("85") != 0 && strPostalCode.indexOf("86") != 0)
				{
					return strPostalCode + " is not a valid Arizona zip code.\n";
				}
				break;
			default:
				break;		
	}
	return "";
}
function IsNumeric(sVal)
{
	if(sVal.length<1) return false;

	if(!hasIntChars(sVal))
	{
		return false;
	}
	
	return true;
} 