
// isEmail (STRING s [, BOOLEAN emptyOK])
// whitespace characters
var whitespace = " \t\n\r";

// 
// Email address must be of form a@b.c ... in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
function isValidEmail(s)
{   
	if (isEmpty(s)) return false;
   
	// is s whitespace?
	if (isWhitespace(s)) return false;
	    
	// there must be >= 1 character before @, so we
	// start looking at character position 1 
	// (i.e. second character)
	var i = 1;
	var sLength = s.length;

	// look for @
	while ((i < sLength) && (s.charAt(i) != "@"))
	{ i++
	}

	if ((i >= sLength) || (s.charAt(i) != "@")) return false;
	else i += 2;

	// look for .
	while ((i < sLength) && (s.charAt(i) != "."))
	{ i++
	}

	// there must be at least one character after the .
	if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
	else return true;
}

// Check whether string s is empty.
function isEmpty(s)
{   
	return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace(s)
{   
	var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.
    for (i = 0; i < s.length; i++)
    {   
		// Check that current character isn't whitespace.
		var c = s.charAt(i);

		if (whitespace.indexOf(c) == -1) return false;
    }
    // All characters are whitespace.
    return true;
}

function checkForm(formID) { 
	formbox = eval("document." + formID); 
	if (isEmpty(formbox.elements['First Name'].value)) {
	formbox.elements['First Name'].style.backgroundColor='#fff0f5';
	formbox.elements['First Name'].style.fontWeight='bold';
	formbox.elements['First Name'].style.color='#660000';
		alert("Please enter a valid First Name");
		formbox.elements['First Name'].focus();
		return false;
	} else if (isEmpty(formbox.elements['Last Name'].value)) {
	formbox.elements['Last Name'].style.backgroundColor='#fff0f5';
	formbox.elements['Last Name'].style.fontWeight='bold';
	formbox.elements['Last Name'].style.color='#660000';
		alert("Please enter a valid Last Name");
		formbox.elements['Last Name'].focus();
		return false;
	} else if (!isValidEmail(formbox.elements['Email Address'].value)) {
	formbox.elements['Email Address'].style.backgroundColor='#fff0f5';
	formbox.elements['Email Address'].style.fontWeight='bold';
	formbox.elements['Email Address'].style.color='#660000';
		alert("Please enter a valid Email Address. (name@host.com)");
		formbox.elements['Email Address'].focus();
		return false;
	} else {
		return true;
	}
	
	
	
}
//--> 

// scripts.js

function toggleMe() {
var div2 = document.getElementById('div2');
var div1 = document.getElementById('div1');
	if ( div2.className == 'hidden' ) {
		div2.className = 'visible';
		div1.className = 'hidden';
	} else {
		div2.className = 'hidden';
		div1.className = 'visible';
	}
}



var _T = "locked";
var _F = "unlocked";
function lockIt(_P)  {

	var _L = document.theForm.lck.value;
	
	if(_L==_P)return;
	document.theForm.city.disabled=(document.theForm.lck.value=(_L==_F)?_T:_F)==_T;
	document.theForm.zipcode.disabled=(document.theForm.lck.value=(_L==_F)?_T:_F)!=_T;
}


function lockIt_2(_P)  {

	var _L = document.theForm.lck_2.value;
	
	if(_L==_P)return;
	document.theForm.streetnumber1.disabled=(document.theForm.lck_2.value=(_L==_F)?_T:_F)!=_T;
	document.theForm.street_number_start.disabled=(document.theForm.lck_2.value=(_L==_F)?_T:_F)==_T;
	document.theForm.street_number_end.disabled=(document.theForm.lck_2.value=(_L==_F)?_T:_F)==_T;
	
}

function CheckAll() {
  for (var i = 0; i < document.thisForm.elements.length; i++) {
    if(document.thisForm.elements[i].type == 'checkbox'){
      document.thisForm.elements[i].checked =  false;
	}
  }
}


function compHomePopUp(url) {
	window.open(url,"MyPopup",'height=300,width=500');
} 


/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1997;
var now = new Date();
var maxYear = now.getFullYear();
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day");
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date");
		return false;
	}
return true
}

function ValidateForm(){
	var dt=document.theForm.sdate;
	var dt1=document.theForm.edate;
	var epur=document.theForm.epurchaseprice;
	var spur=document.theForm.spurchaseprice;
	
	
	if (!IsNumeric(spur.value))  { 
		alert('Please enter only numbers for the Purchase Price.') 
		spur.focus(); 
		return false; 
	}
	  
	if (!IsNumeric(epur.value)) { 
		alert('Please enter only numbers for the Purchase Price.') 
		epur.focus(); 
		return false; 
	} 
	
	if (spur.value.length != 0 && epur.value.length == 0) { 
		alert('Please enter an ending Purchase Price.') 
		epur.focus(); 
		return false; 
	} 
	
	if (parseInt(spur.value) > parseInt(epur.value)) { 
		alert('Your Ending Purchase Price needs to be higher than you Starting.') 
		epur.focus(); 
		return false; 
	} 
    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;
}

