//-------------------------------------------------------------------------------
// PropertyBoss PropertyWeb Form Validation Functions
//-------------------------------------------------------------------------------
// Copyright 2008 PropertyBoss Solutions, LLC
// These function scripts may be only used with this rental application
// They may be modified, but this copyright notice must be displayed

FirstTime= true

function isValidForm(theForm)
{
	if (!isValidEntries(theForm))
		return false
	if (FirstTime) {
		FirstTime= false
		theForm.validated.value= true
		theForm.submit()			//submit form
	} else {
		alert('Information already submitted --- Click Reset before resubmiting.')
		return false
	}
}

function entryLabel(entryName) {
if ((Label= document.getElementById(entryName +'_')) != null)
	return Label.innerHTML
else if ((Label= entry.className.indexOf('label:')) != -1)
	return entry.className.substring(Label+6, entry.className.indexOf(';', Label))
else return entryName
}

function isValidEntries(theForm)
{
var j= 0	//element index

while (j < theForm.elements.length)  {
	entry= theForm.elements[j++]
	if (entry.type == 'hidden')	continue
	if (entry.parentNode.style.display == 'none') continue
	if (entry.parentNode.parentNode.style.display == 'none') continue
	if (entry.parentNode.parentNode.parentNode.style.display == 'none') continue

	if (entry.className.indexOf('required') != -1 || entry.value != '') {
//			alert('name: ' +entry.name +'; type: ' +entry.type +'; value: ' +entry.value +'; class: ' +entry.className)
	if (entry.value == '') {
		if (entry.nodeName == 'SELECT')
			alert('Please select [' +entryLabel(entry.name) +'] and Submit again.')
		else
			alert('Please enter [' +entryLabel(entry.name) +'] and Submit again.')
		entry.focus()
		return false	
	} else if (entry.className.indexOf('email') != -1) {
	    if (isEmail(entry.value)) continue
		alert('Please enter a valid e-mail address [' +entryLabel(entry.name) +']')
		entry.focus()
		return false
	} else if (entry.className.indexOf('date') != -1) {
	    if (isDate(entry.value)) continue
		alert('Please enter a valid date [' +entryLabel(entry.name) +'] in mm/dd/yyyy format.')
		entry.focus()
		return false
	} else if (entry.className.indexOf('phone') != -1) {
		var stripped = entry.value.replace(/[\(\)\.\-\ ]/g, '')
    	if (isNaN(parseInt(stripped)) || stripped.length < 10 ) {
			alert('Please correct incorrectly formated phone number [' +entryLabel(entry.name) +']')
			entry.focus()
			return false
		} else continue
	} else if (entry.className.indexOf('socSec') != -1) {
		var stripped = entry.value.replace(/[\(\)\.\-\ ]/g, '')
    	if (isNaN(parseInt(stripped)) || !(stripped.length == 9)) {
			alert('Please correct incorrectly formated social security number [' +entryLabel(entry.name) +']')
			entry.focus()
			return false
		} else continue
	} else if (entry.type == 'radio' && entry.className.indexOf('required') != -1) {
		noSelect= true;
		radioButtons= eval('theForm.' +entry.name)
		for (i= 0; i < radioButtons.length; i++)
			if (radioButtons[i].checked) noSelect= false
		if (noSelect) {
			entry.focus()
			alert('Please select one of the options (buttons) for ' + entryLabel(entry.name))
			return false
		}
	} else if (entry.className.indexOf('num1') != -1 && (isNaN(entry.value) || entry.value < 1)) {
	    alert(entryLabel(entry.name) + ' must be greater than 1; reenter and try again.')
	    entry.focus()
	    return false
	} //if null entry
    } //if formrequired
	else if (entry.className.indexOf('Agree') != -1 && !entry.checked) {
		alert(entryLabel(entry.name) + ' - You must indicate acceptance of the agreement before we can process your application.')
		entry.focus()
		return false
	}
} //while
return true
}

function isEmail(Elem)
{
var AtSym    = Elem.indexOf('@')		//first @
var LastDot  = Elem.lastIndexOf('.')	//last period
var FirstDot = AtSym +Elem.substring(AtSym).indexOf('.')	//first period
var Space    = Elem.indexOf(' ')		//first space
var Length   = Elem.length

if ((AtSym < 1) ||		// '@' cannot be in first position
	(FirstDot < AtSym+3) ||	// at least two char between '@' and first dot
	(LastDot > Length-3) ||	// at least two char after last dot
	((LastDot != FirstDot)
	&& (LastDot < FirstDot+3)) ||	// only one dot or two char between them
	(Space  != -1))		// no empty spaces
		return false
return true
}

function isDate(Elem) {
   var re= /^\d{1,2}\/\d{1,2}\/\d{4}$/
   if (re.test(Elem)) {
      var Arg= Elem.split('/');
      var d= new Date(Elem);
      return d.getMonth() +1 == Arg[0] && d.getDate() == Arg[1] && d.getFullYear() == Arg[2];
   }
   else return false;
}

function ResetForm()
{
	FirstTime= true
	validated= false
}

function CheckForm(FormName)
{
if (!isValidEntries(FormName))
	return false
if (FirstTime) {
	FirstTime= false
	FormName.validated.value= true
	FormName.submit() //submit form
} else
	alert('Information already submitted --- Click Reset before resending.')
}

//-------------------------------------------------------------------------------
// Browser Detection
//-------------------------------------------------------------------------------

var detect = navigator.userAgent.toLowerCase()
isFirefox = ( detect != null && detect.indexOf( "firefox/" ) != -1 )
isIE = ( detect != null && detect.indexOf( "msie" ) != -1 )
// if (!isIE) document.write('<link href="../inc/menus_fireFox.css" rel="stylesheet" type="text/css" />')

//-------------------------------------------------------------------------------
// Capcha Code
//-------------------------------------------------------------------------------
// Captcha Verification
// Completely Automated Public Turing test to tell Computers and Humans Apart
// (c) 2008 PropertyBoss Solutions, LLC
// last modified: 2008.06.06.mgm

// html code (locate captcha image on page):
// <div><img id='captchaImage' width='110' height='40' src='sp.gif' align='left' style='margin-right: 15px' />
// Enter the characters from the image to the left below:<br />
// <input name='captcha' id='captcha' type='text' class='requiredz' onchange='captchaVerify()' /></div>

// html code (include with form variables):
// <input type='hidden' name='ccKernel' id='ccKernel' />

// JavaScript (onLoad):
// var kernel= Math.round(Math.random()*100001)
// document.getElementById('captchaImage').src= 'http://propertyboss.net/inc/captcha.php?width=110&height=40&characters=5&ccKernel=' +kernel
// document.getElementById('captchaImage').src= '../../../../inc/captcha.php?width=110&height=40&characters=5&ccKernel=' +kernel
// document.getElementById('ccKernel').value= 10 *kernel +5


function captchaVerify() {
	var req= null
	var captcha= document.getElementById('captcha').value
	var kernel= document.getElementById('ccKernel').value
	var url= '../inc/captcha.php?ccKernel=' +kernel +'&captcha=' +captcha

	if (window.XMLHttpRequest)  req= new XMLHttpRequest()
	else if (window.ActiveXObject) {
		try { req= new ActiveXObject("Msxml2.XMLHTTP") }
		catch (e) {
			try { req= new ActiveXObject("Microsoft.XMLHTTP") }
			catch (e) {}
		}
	}

	req.onreadystatechange= function() {
		if (req.readyState == 4) {
			if (req.status == 200 && req.responseText == 'oK') {
				document.getElementById('captcha').style.border= '1px solid #49c24f'
				document.getElementById('captcha').style.background= '#bfb'
				captchaNG= false
			} else {
				document.getElementById('captcha').style.border= '1px solid #c24949'
				document.getElementById('captcha').style.background= '#f88'
				captchaNG= true
			}
		}
	}
	req.open('GET', url, true)
//	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.send(null);
}

function captchaInit()
{
var captchaNG= true
var kernel= Math.round(Math.random()*100001)
document.getElementById('captchaImage').src= 'https://www.propertyboss.net/inc/captcha.php?width=110&height=40&characters=5&ccKernel=' +kernel
document.getElementById('ccKernel').value= 10 *kernel +5
}
//  End -->
