// JavaScript Document
//var absPath = 'http://www.priceedwards.com/';
var isIE = document.all?true:false;

function getElement(id){   
	if(document.getElementById){ 
		// test the most common method first. Most browsers won't get past this test        
		return document.getElementById(id);  
	 }
	 else if(document.all){ 
	 	// test older versions of IE        
		return document.all[id];    
	}
	else if(document.layers){ 
	// test older versions of Netscape        
		return document.layers[id];    
	}
	else{ // not sure what to do...return null        
		return null;    }
}

function getMousePosition(e) {
  if(!e){ var e = window.event; }
  
  var _x;
  var _y;
  if (!isIE) {
    _x = e.pageX;
    _y = e.pageY;
  }
  if (isIE) {
    if (document.documentElement && !document.documentElement.scrollTop)
    {
        // IE6 +4.01 but no scrolling going on
        _x = event.clientX;
        _y = event.clientY;
    }
    
    else if (document.documentElement && document.documentElement.scrollTop)
    {
        // IE6 +4.01 and user has scrolled
        _x = event.clientX + document.documentElement.scrollLeft;
        _y = event.clientY + document.documentElement.scrollTop;
    }
    else if (document.body && document.body.scrollTop)
    {
        // IE5 or DTD 3.2
        _x = event.clientX + document.body.scrollLeft;
        _y = event.clientY + document.body.scrollTop;
    }
  }
  return Array(_x,_y);
}

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return Array(myWidth, myHeight);
}

function isNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)){	return false; }

    return true;

}

var propertyTypes = new Array('Office', 'Retail', 'Industrial', 'Land', 'Multifamily');
var imagesForPreloading = new Array('images/office.gif','images/retail_selected.gif', 'images/industrial_selected.gif', 'images/multifamily_selected.gif', 'images/land_selected.gif');

function selectProperties(type, top, absPath)
{
	for(var i=0; i<propertyTypes.length; i++)
	{
		var imgUrl = '';
		if(propertyTypes[i] != type)
		{
			//var imgUrl = 'images/'+propertyTypes[i]+'.gif';
			//var imgUrl = '';
			getElement('properties'+propertyTypes[i]+'ContentDiv').style.visibility='hidden';
			getElement('properties'+propertyTypes[i]+'TitleDiv').style.top = top+'px';
		}
		else
		{
			//imgUrl = 'images/'+propertyTypes[i]+'_selected.gif';
			getElement('properties'+propertyTypes[i]+'ContentDiv').style.visibility='visible';
			getElement('properties'+propertyTypes[i]+'TitleDiv').style.top = (top+2)+'px';
		}
			var path = absPath+'images/typebg.gif';//imgUrl;
			getElement('properties'+propertyTypes[i]+'TitleDiv').style.backgroundImage = 'url('+path+')';
		//alert(document.getElementById('propertiesOfficeContentDiv').style.visibiliy);
		//document.getElementById('properties'+propertyTypes[i]+'TitleDiv').style.background-image = propertyTypes[i]+'.gif';
	}
}

function preloadImages(absPath)
{
	for(var i=0; i<imagesForPreloading.length; i++)
	{
		var pic = new Image();
		var path = absPath+imagesForPreloading[i];
		pic.src = path;
	}
}

function load()
{
	getElement('SearchTB').value = 'Search';
}


function showFollow(text, con, event)
{
	//if(con == 'graphInfoDiv'){alert(text.replace(/!/g,"\""));}
    var control = getElement(con);
	control.innerHTML = text.replace(/!/g,"\"");
    control.style.visibility = 'visible';
	
	var ws = getWindowSize();
	var mouse = getMousePosition(event);

    var width = ws[0];
    //alert('if '+window.event.y + ' > ' + document.body.clientHeight + ' - ' + control.style.height);
    
    if(mouse[1] > (ws[1] - parseInt(control.style.height)))
    {
        control.style.top = (mouse[1]-parseInt(control.style.height))+'px';
        //alert('true');
    }
    else
    {
        control.style.top = mouse[1]+10+'px';
    }
    control.style.left = parseInt(mouse[0])-(parseInt(control.style.width)*2)+'px';
}

function follow(con,e)
{
    var control = getElement(con);
    control.style.visibility = 'visible';
	
	var ws = getWindowSize();
	var mouse = getMousePosition(e);

    var width = ws[0];
    //alert('if '+window.event.y + ' > ' + document.body.clientHeight + ' - ' + control.style.height);
    
    if(mouse[1] > (ws[1] - parseInt(control.style.height)))
    {
        control.style.top = (mouse[1]-parseInt(control.style.height))+'px';
        //alert('true');
    }
    else
    {
        control.style.top = mouse[1]+10+'px';
    }
    control.style.left = parseInt(mouse[0])-parseInt(control.style.width)+'px';
}

function createInvestmentFlashMovie(id)
{
	document.write('<object type=\"application/x-shockwave-flash\" data=\"flash/investment/index.swf?id='+id+'\" width=\"750\" height=\"250\" id=\"index\" align="\middle\">');
	document.write('<param name=\"allowScriptAccess\" value=\"sameDomain\" />');
	document.write('<param name=\"movie\" value=\"flash/investment/index.swf?id='+id+'\"/>');
	document.write('<param name=\"quality\" value=\"high\" />');
	document.write('<param name=\"bgcolor\" value=\"#ffffff\" />');
    document.write('<param name=\"flashvars\" value=\"id='+id+'\" />');
	document.write('</object>');
}

function createGenericFlashMovie(path, width, height)
{
	document.write('<object type=\"application/x-shockwave-flash\" data=\"'+path+'\" width=\"'+width+'\" height=\"'+height+'\" id=\"index\" align="\middle\">');
	document.write('<param name=\"allowScriptAccess\" value=\"sameDomain\" />');
	document.write('<param name=\"movie\" value=\"'+path+'\" />');
	document.write('<param name=\"quality\" value=\"high\" />');
	document.write('<param name=\"bgcolor\" value=\"#ffffff\" />');
    document.write('<param name=\"flashvars\" value=\"'+path+'\" />');
	document.write('</object>');
}

function createGenericFlashMovieVars(path, width, height)
{
	document.write('<object type=\"application/x-shockwave-flash\" data=\"'+path+'\" width=\"'+width+'\" height=\"'+height+'\">');
	document.write('<param name=\"movie\" value=\"'+path+'\" />');
	document.write('<param name=\"quality\" value=\"high\" />');
	document.write('<param name=\"bgcolor\" value=\"#ffffff\" />');
    document.write('<param name=\"flashvars\" value=\"'+path+'\" />');
	document.write('</object>');
}

function createGenericFlashMovieVarsDiv(path, width, height, div)
{
	getElement(div).innerHTML = '<object type=\"application/x-shockwave-flash\" data=\"'+path+'\" width=\"'+width+'\" height=\"'+height+'\">'+
		'<param name=\"movie\" value=\"'+path+'\" />' +
		'<param name=\"quality\" value=\"high\" />' + 
		'<param name=\"bgcolor\" value=\"#ffffff\" />' +
		'<param name=\"flashvars\" value=\"'+path+'\" />' +
		'</object>';
}

function createGenericFlashMovieVarsTransparent(path, width, height)
{
	document.write('<object type=\"application/x-shockwave-flash\" data=\"'+path+'\" width=\"'+width+'\" height=\"'+height+'\">');
	document.write('<param name=\"movie\" value=\"'+path+'\" />');
	document.write('<param name=\"quality\" value=\"high\" />');
	document.write('<param name=\"wmode\" value=\"transparent\" />');
    document.write('<param name=\"flashvars\" value=\"'+path+'\" />');
	document.write('</object>');
}

	function hidePECMenu(which){
		getElement(which).style.display='none';
	}
	
	function showPECMenu(which){
		getElement(which).style.display = 'block';
	}
	
	function showMouseOverOutPL(whichImage, whichOverOut){

		getElement(whichImage).src='./images/propertyListings/'+whichImage+'_'+whichOverOut+'.jpg';
	}
	function hideFlash(){
		var plf = getElement('propertyListingsFlash');
		if(plf != null){
			plf.style.display = 'none';
		}
	}
	
	function showFlash(){
		var plf = getElement('propertyListingsFlash');
		if(plf != null){
			plf.style.display = 'block';
		}
	}
	
	
//Contact
function validate()
	{
		var name = getElement("name");
		var email = getElement("email");
		var phonenumber = getElement("phonenumber");
		var message = getElement("message");
		
		if(name.value == "" || email.value == "" || message.value == "")
		{
			alert("Please fill in your Name, E-mail, and enter a Message before sending");
		}
		else
		{
			document.contactForm.submit();
		}
		
	}
	
	function contact(name, id,e)
	{
		follow('contactDiv',e);
		
		var contactTitle = getElement('contactTitle');
		contactTitle.innerHTML = "Contact "+name;
		
		var sid = getElement('sid');
		sid.value = id;
	}
	
	function closeContact()
	{
		var div = getElement('contactDiv');
		div.style.visibility = 'hidden';
	}
	
	function closeSuccess()
	{
		var div = getElement('successDiv');
		div.style.visibility = 'hidden';
	}
	
	function success(name)
	{
		var div = getElement('successDiv');
		div.innerHTML += '<p style=\"text-align:center; font-size:14px;\">You have successfully sent a message to '+name+'.<br><br>Please wait for a response.</p>';
		div.style.visibility = 'visible';
	}
	
	function unsuccess(name)
	{
		var div = getElement('successDiv');
		div.innerHTML += '<p style=\"text-align:center; font-size:14px;\">Your message was not sent.  An administrator has been notified.  Try again later.</p>';
		div.style.visibility = 'visible';
	}
	
	function invalid(name)
	{
		var div = getElement('successDiv');
		div.innerHTML += '<p style=\"text-align:center; font-size:14px;\">Your message was not sent.  The text you entered did not match the text in the image. Please try again.</p>';
		div.style.visibility = 'visible';
	}
	
	
	//Survey
	
	function submitSurveyForm(){
		/*
		var key = getElement('key');
		if(key.value == ''){
			alert('You must enter the validation text in the image before submitting.');
			key.style.backgroundColor='#FFFF66';
			return;
		}*/
		if(!jobTitleSelected()){
			alert('Please select a job title.');
			return;		
		}
		if(!priceRangeSelected()){
			alert('Please select at least 1 price range.');
			return;
		}
		if(!assetTypeSelected()){
			alert('Please select at least 1 asset type.');
			return;
		}
		getElement('surveyForm').submit();
	}
	
function restrictLength(con,len){
	return getElement(con).value.length < len;
}

function validate()
	{
		var name = getElement("name");
		var email = getElement("email");
		var phonenumber = getElement("phonenumber");
		var message = getElement("message");
		
		if(name.value == "" || email.value == "" || message.value == "")
		{
			alert("Please fill in your Name, E-mail, and enter a Message before sending");
		}
		else
		{
			document.contactForm.submit();
		}
		
	}
	
	function closeSuccess()
	{
		var div = getElement('successDiv');
		div.style.visibility = 'hidden';
	}
	
	function success(name)
	{
		var div = getElement('successDiv');
		div.innerHTML += '<p style="text-align:center; font-size:14px;">You have successfully sent a message to '+name+'.<br/><br/>Please wait for a response.</p>';
		div.style.visibility = 'visible';
	}
	
	function unsuccess(name)
	{
		var div = getElement('successDiv');
		div.innerHTML += '<p style="text-align:center; font-size:14px;">Your message was not sent.  An administrator has been notified.  Try again later.</p>';
		div.style.visibility = 'visible';
	}
	
function preloadPropertyTypeImages(){
	var images = Array("./images/propertyListings/officeForSale_over.jpg",
					   "./images/propertyListings/officeForLease_over.jpg",
					   "./images/propertyListings/retailForSale_over.jpg",
					   "./images/propertyListings/retailForLease_over.jpg",
					   "./images/propertyListings/industrialForSale_over.jpg",
					   "./images/propertyListings/industrialForLease_over.jpg",
					   "./images/propertyListings/multifamilyForSale_over.jpg",
					   "./images/propertyListings/multifamilyForLease_over.jpg",
					   "./images/propertyListings/landForSale_over.jpg",
					   "./images/propertyListings/landForLease_over.jpg");
	
	for(i=0; i<images.length; i++){
		image = new Image();
		image.src = images[i];
	}
	
}

function getSelectedItemInList(list){
	for(i=0; i<list.options.length; i++){
		if(list.options[i].selected){
			return list.options[i];
		}
	}
}

function IsNumber(e)
{
    var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);
	reg = /\d/;
	if(reg.test(keychar) || IsValidGeneralCharacter(key)){
	    return true;
	}
	return false;
}

function IsValidGeneralCharacter(key)
{
    return (key==110|| key == 8 || key==37 || key==38 || key==39 || key==40 || key==13 || key==46 || key==9 || IsKeypad(key) || key==188);
}

function IsKeypad(key)
{
    return ((key >= 96 && key <= 105) || key == 111);
}

function formatCurrency2(val) {
        num = val.toString().replace(/\$|\,/g,'');

        if(isNaN(num)){
	        num = "0";
		}
        sign = (num == (num = Math.abs(num)));
        num = Math.floor(num*100+0.50000000001);
        cents = num%100;
        num = Math.floor(num/100).toString();
        if(cents<10){
    	    cents = "0" + cents;
		}
        for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++){
        	num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
		}
        return (((sign)?'':'-') + num);
}
function formatNum(val)
{
    return(val.toString().replace(",",""));
}
function highlightInvestmentListRow(id,img,event){
	//getElement('propertyImage').src = '';
	getElement('row'+id).className='investmentListRowOver';
	//getElement('propertyImage').src = 'http://www.priceedwards.com/properties/components/com_hotproperty/img/std/'+img;
	//getElement('propertyInfoDiv').style.display='block';
	//follow('propertyInfoDiv',event);
}
function unHighlightInvestmentListRow(id,className,event){
	getElement('row'+id).className=className;
	//getElement('propertyInfoDiv').style.display='none';	
}

/*****************************
Push Marketing
******************************/
function pushMarketingExpandRow(row){
	var pmA = getElement(row+'A');
	var pmDiv = getElement(row+'Div');
	
	if(pmA.innerHTML == '[+]'){
		pmDiv.style.display='block';
		pmA.innerHTML = '[-]';
	}else{
		pmDiv.style.display='none';
		pmA.innerHTML = '[+]';
	}
}

/*****************************
Investemnt Stats
*******************************/
function investmentSiteStatsShow(which){
	var divs = Array("investmentSiteRegisteredVisitorsDiv",
					 "investmentSiteUrlStatsDiv",
					 "investmentSitePushMarketingDiv");
	
	for(i=0;i<divs.length; i++){
		if(divs[i] != which){
			getElement(divs[i]).style.display='none';
		}
		getElement(which).style.display='block';
	}
}

function fixedSize_new(url,name,features) {
			//This launches a new window and then
			//focuses it if window.focus() is supported.
			win = window.open(url,name,features);
	}


function validateRegisterForm(){
	var name = getElement('name').value;
	var email = getElement('email').value;
	var address = getElement('address').value;
	var company = getElement('company').value;
	var city = getElement('city').value;
	var state = getElement('state').value;
	var zip = getElement('zip').value;
	var telephone_a = getElement('phone_a').value;
	var telephone_b = getElement('phone_b').value;
	var telephone_c = getElement('phone_c').value;
	var telephone_ext = getElement('ext').value;
	var messageDiv = getElement('messageDiv');
	var submitForm = true;
	messageDiv.innerHTML = "Validating...";
	if(name == ""){
		messageDiv.innerHTML += 'Name must be filled in <br/>';
		submitForm = false;
	}
	if(email == ""){
		messageDiv.innerHTML += 'Email must be filled in <br/>';
		submitForm = false;
	}else if(!isValidEmail(email)){
		messageDiv.innerHTML += 'Email must be valid e-mail address.<br/>';
		submitForm = false;
	}
	if(company == ""){
		messageDiv.innerHTML += 'Company must be filled in <br/>';
		submitForm = false;
	}
	if(address == ""){
		messageDiv.innerHTML += 'Address must be filled in <br/>';
		submitForm = false;
	}
	if(city == ""){
		messageDiv.innerHTML += 'City must be filled in <br/>';
		submitForm = false;
	}
	if(state == ""){
		messageDiv.innerHTML += 'State must be filled in <br/>';
		submitForm = false;
	}
	if(zip == ""){
		messageDiv.innerHTML += 'Zip must be filled in <br/>';
		submitForm = false;
	}
	if(telephone_a == "" || telephone_b == "" || telephone_c == ""){
		messageDiv.innerHTML += 'Phone must be filled in <br/>';
		submitForm = false;
	}else if(telephone_a.length<3 || telephone_b.length<3 || telephone_c.length<4){
		messageDiv.innerHTML += 'Phone number must be in the form XXX-XXX-XXXX';
		submitForm = false;
	}
	if(submitForm){
		getElement('registerForm').submit();
	}
}

function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;

   // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1) 
   {
      return false;
    } 
    return true; 
}
