function _parseInt(value)
{
	value = parseInt(value);
		
	if( isNaN(value) || value < 1)
	{
		return 0;
	}	
	else return value;
}

function _parseFloat(value)
{
	value = parseFloat(value);
		
	if( isNaN(value))
	{
		return 0;
	}	
	else return value;
}

function recalculate_renew_order()
{
	ad = document.getElementById('ad');
	ad1 = document.getElementById('ad1');
	ad2 = document.getElementById('ad2');
	ad3 = document.getElementById('ad3');
	ad_total = document.getElementById('ad_total');
	
	var renew_total = 0;
	if(ad.value != "")
	{
		renew_total  += _parseFloat(renewal);
	}
	if(ad1.value != "")
	{
		renew_total  += _parseFloat(renewal);
	}
	if(ad2.value != "")
	{
		renew_total  += _parseFloat(renewal);
	}
	if(ad3.value != "")
	{
		renew_total  += _parseFloat(renewal);
	}
	
	ad_total.value = '$'+renew_total;		
}

function recalculate_rental_order()
{
	
	condo_th = document.getElementById('condo_th');
	house = document.getElementById('house');
	highr = document.getElementById('highr');
	
	condo_th_count = document.getElementById('x1');
	house_count = document.getElementById('x2');
	highr_count = document.getElementById('x3');
	
	sqft_1500_value = document.getElementById('price_low');
	sqft_2000_value = document.getElementById('price_middle');
	sqft_2500_value = document.getElementById('price_high');
	sqft_3000_value = document.getElementById('price_max');
	
	sqft_1500_count = document.getElementById('x4');
	sqft_2000_count = document.getElementById('x5');
	sqft_2500_count = document.getElementById('x6');
	sqft_3000_count = document.getElementById('x7');
	
	elem_subtotal = document.getElementById('subtotal');
	elem_discount = document.getElementById('discount');
	elem_total = document.getElementById('total');
	
	var setup_subtotal = 0;
	var video_subtotal = 0;
	
	if(condo_th.checked)
	{
		setup_subtotal  += 	_parseInt(condo_th_count.value)*_parseFloat(set_up_fee_house);
	}
	if(house.checked)
	{
		setup_subtotal  += 	_parseInt(house_count.value)*_parseFloat(set_up_fee_house);
	}
	if(highr.checked)
	{
		setup_subtotal  += 	_parseInt(highr_count.value)*_parseFloat(set_up_fee_high_rise);
	}
	if(sqft_1500_value.checked)
	{
		if(highr.checked == false)
		{
			video_subtotal  += 	_parseInt(sqft_1500_count.value)*_parseFloat(video_fee_sqft_1500);
		}
		else
		{
			video_subtotal  += 	_parseInt(sqft_1500_count.value)*_parseFloat(video_fee_sqft_1500_high_rise);
		}
	}
	if(sqft_2000_value.checked)
	{
		if(highr.checked == false)
		{
			video_subtotal  += 	_parseInt(sqft_2000_count.value)*_parseFloat(video_fee_sqft_2000);
		}
		else
		{
			video_subtotal  += 	_parseInt(sqft_2000_count.value)*_parseFloat(video_fee_sqft_2000_high_rise);
		}
	}
	if(sqft_2500_value.checked)
	{
		if(highr.checked == false)
		{
			video_subtotal  += 	_parseInt(sqft_2500_count.value)*_parseFloat(video_fee_sqft_2500);
		}
		else
		{
			video_subtotal  += 	_parseInt(sqft_2500_count.value)*_parseFloat(video_fee_sqft_2500_high_rise);
		}
	}
	if(sqft_3000_value.checked)
	{
		if(highr.checked == false)
		{
			video_subtotal  += 	_parseInt(sqft_3000_count.value)*_parseFloat(video_fee_sqft_3000);
		}
		else
		{
			video_subtotal  += 	_parseInt(sqft_3000_count.value)*_parseFloat(video_fee_sqft_3000_high_rise);
		}
	}

	
	elem_subtotal.value = setup_subtotal+video_subtotal;
	video_discount = video_subtotal*parseFloat(discount)/100;

	elem_discount.value = video_discount;
	
	//elem_total.InnerHTML = setup_subtotal+video_subtotal-video_discount;
	elem_total.value = setup_subtotal+video_subtotal-video_discount;
}




/* code */

var initPage = function()
{
	initPriceForm('price', 'terms', 'paypal');
	initPriceForm('pricing_center', 'terms1', 'paypal2');
	initHovers();
	initBottomProperties();
}

var initHovers = function()
{
	var linkContact = document.getElementById("contact_link");
	var linkSend = document.getElementById("send-to-friend_link");
	if(linkContact)
	{
		linkContact.onclick = function(){
			this.parentNode.style.position = 'relative';
			this.parentNode.className += ' hover';
		};
		var linkContactClose = document.getElementById("close_contact");
		if(linkContactClose)
		{	
			linkContactClose.onclick = function(){
				var liContact = document.getElementById("contact");
				liContact.style.position = 'static';
				liContact.className = liContact.className.replace(' hover','');
			};
		}
	}
	
	if(linkSend)
	{	
		linkSend.onclick = function(){
			if (this.parentNode.className.indexOf(' hover') == -1) {
				this.parentNode.style.position = 'relative';
				this.parentNode.className += ' hover';
			}
			else
			{
				this.parentNode.style.position = 'static';
				this.parentNode.className = this.parentNode.className.replace(' hover','');
			}
			var linkSendClose = document.getElementById("close_send_to_friend");
			if(linkSendClose)
			{	
				linkSendClose.onclick = function(){
					var liSend = document.getElementById("send-to-friend");
					liSend.style.position = 'static';
					liSend.className = liSend.className.replace(' hover','');
				};
			}
		};
	}
}

var initPriceForm = function(form, terms, paypal) {
	var form = document.getElementById(form)
	if(form)
	{
		var terms = document.getElementById(terms);
		if(terms)
		{
			form.onsubmit = function() {
				if(terms.checked == false)
				{
					alert('You must agree with terms below');
					return false;
				}
				return true;
			}
		}
	}
}

var initBottomProperties = function()
{
	var form = document.getElementById('properties');
	if(form)
	{
		var link = form.getElementsByTagName("a");
		if(link) {
			for (var i = 0; i < link.length; i++) {
				if(link[i].rel == 'search')
				{
					link[i].onclick = function(){
						var property_url = document.getElementById('property_url');
						property_url.value = this.href;
						var form = document.getElementById('properties');
						form.submit();
						return false;
					}
				}
			}
		}
		
		var more = document.getElementById('more-prop');
		if(more)
		{
			if(more.rel == 'search')
			{
				more.onclick = function(){
					var pager = this.href.replace('/?pager=','').replace('http://','').replace('www.','').replace(document.location.host,'');
					var form_search = document.getElementById('form_search');
					var hidden_pager = document.getElementById('hidden_pager');
					hidden_pager.value = pager;
					form_search.submit();
					return false;
				}
			}
		}
		
		var btn_search_submit = document.getElementById('btn-search-submit');
		if(btn_search_submit)
		{
			btn_search_submit.onclick = function(){
				var form_search = document.getElementById('form_search');
				var hidden_pager = document.getElementById('hidden_pager');
				hidden_pager.value = 0;
				form_search.submit();
				return false;
			}
		}
		
	}
}
function contact_submit()
{
	var form_contact = document.getElementById('form-contact');
	if (!form_contact) return;
	if (form_contact.elements.form_name.value == '' || form_contact.elements.form_email.value == '' || form_contact.elements.form_comment.value == '')
	{
		alert("Please make sure all required fields are completed.");
		return false;
	}
	if (form_contact.elements.form_email.value == '' || !form_contact.elements.form_email.value.match(new RegExp('^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$')))
	{
		alert("Please specify valid E-mail address.");
		try {form_contact.elements.form_email.focus()} catch(e){}
		return false;
	}
	form_contact.submit();
}

/* common */
var browsers = 'all';
var currentState = {};

var opera = window.opera;
var safari = navigator.appVersion.indexOf("Safari") != -1;
var ie = document.all && !window.opera;
var ff = navigator.userAgent.indexOf("Firefox") != -1;

var addEvent = function(browsers,event,initMethod)
{
	if (opera && (browsers.match(/Opera/) || browsers.match(/all/))) {
		window.addEventListener(event, initMethod, false);
	} else if (safari && (browsers.match(/Safari/) || browsers.match(/all/))) {
		window.addEventListener(event, initMethod, false);
	} else if (ie && (browsers.match(/IE/) || browsers.match(/all/))) {
		window.attachEvent("on" + event, initMethod);
	} else if (ff && (browsers.match(/FF/) || browsers.match(/all/))) {
		window.addEventListener(event, initMethod, false);
	} else if (!ie && !ff && !safari && !opera) {
		if (window.addEventListener){
			window.addEventListener(event, initMethod, false);
		}
		else if (window.attachEvent){
			window.attachEvent("on" + event, initMethod);
		}
	} 
}

addEvent(browsers,'load',initPage);

