/* functions.js, MHCAZ.com Site 04/24/05 */



/////Comminuty Membership Form Functions /////

//////////////////////////////////////////////////////////
////// Functions to prepopulate the mail address  ////////
//////     with the comminuty information    /////////////
function InitSaveVariables(form) {
	MailAddress1 = form.mail_address1.value;
	MailAddress2 = form.mail_address2.value;
	MailCity = form.mail_city.value;
	MailZip = form.mail_zipcode.value;
	MailStateIndex = form.mail_state.selectedIndex;
	MailState = form.mail_state[MailStateIndex].value;
}

function mailToCommAddress(form) {
	if (form.mail_same.checked) {
		InitSaveVariables(form);
		form.mail_address1.value = form.comm_address1.value;
		form.mail_address2.value = form.comm_address2.value;
		form.mail_city.value = form.comm_city.value;
		form.mail_zipcode.value = form.comm_zipcode.value;
		form.mail_state.selectedIndex = form.comm_state.selectedIndex;
	}else{
		form.mail_address1.value = MailAddress1;
		form.mail_address2.value = MailAddress2;
		form.mail_city.value = MailCity;
		form.mail_zipcode.value = MailZip;       
		form.mail_state.selectedIndex = MailStateIndex;
   }
}
//////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////
//////         Number formating for dollars       ////////
function formatCurrency(num) {
	num = num.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 + '.' + cents);
	return (((sign)?'':'-') + num + '.' + cents);
}
//////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////
////// Functions to Calculate the MH & RV Spaces  ////////
//////     and Populate the various fields   /////////////

function setFormValues(){
	// Set the initial values
	document.getElementById('spanspaces_mh1').innerHTML = 0;
	document.getElementById('spanspaces_mh2').innerHTML = 0;
	document.getElementById('total_mh1').innerHTML = '0.00';
	document.getElementById('total_mh2').innerHTML = '0.00';
	document.getElementById('spanspaces_rv1').innerHTML = 0;
	document.getElementById('spanspaces_rv2').innerHTML = 0;
	document.getElementById('total_rv1').innerHTML = '0.00';
	document.getElementById('total_rv2').innerHTML = '0.00';
	
	document.getElementById('total_mh').innerHTML = '0.00';
	document.getElementById('total_rv').innerHTML = '0.00';
	
	document.getElementById('grandtotal').innerHTML = '0.00';
}

function calcTotal(form){

	var spaces_mh1 = "";
	var spaces_rv1 = "";
	var total_mh1 = "0";
	var total_mh2 = "0";
	var total_rv1 = "0";
	var total_rv2 = "0";
	var total_mh = "0";
	var total_rv = "0";
	var grandtotal = "0";
	document.getElementById('minmessage').innerHTML = "";
	
	// get all the values
	spaces_mh1 = form.spaces_mh.value;
	spaces_rv1 = form.spaces_rv.value;
	
	// Break up the values
	if(spaces_mh1 > 750){
		spaces_mh2 = (parseInt(spaces_mh1) - 750);
		spaces_mh1 = 750;
	}else{
		spaces_mh2 = 0;
	}
	
	if(spaces_rv1 > 1500){
		spaces_rv2 = (parseInt(spaces_rv1) - 1500);
		spaces_rv1 = 1500;
	}else{
		spaces_rv2 = 0;
	}
	
	// Cacluate the totals
	total_mh1 = spaces_mh1 * 6;
	total_mh2 = spaces_mh2 * 1;
	total_rv1 = spaces_rv1 * 3;
	total_rv2 = spaces_rv2 * 1;
	total_mh = (parseInt(total_mh1) + parseInt(total_mh2));
	total_rv = (parseInt(total_rv1) + parseInt(total_rv2));
	grandtotal = (parseInt(total_mh) + parseInt(total_rv));
	
	if((grandtotal != 0)&&(grandtotal < 150)){
		grandtotal = 150;
		document.getElementById('minmessage').innerHTML = "(Minimum amount of annual dues is $150)";
	}
	
	// Spit out the Final Values to the spans
	document.getElementById('spanspaces_mh1').innerHTML = spaces_mh1;
	document.getElementById('spanspaces_mh2').innerHTML = spaces_mh2;
	document.getElementById('total_mh1').innerHTML = formatCurrency(total_mh1);
	document.getElementById('total_mh2').innerHTML = formatCurrency(total_mh2);
	document.getElementById('total_mh').innerHTML = formatCurrency(total_mh);
	document.getElementById('spanspaces_rv1').innerHTML = spaces_rv1;
	document.getElementById('spanspaces_rv2').innerHTML = spaces_rv2;
	document.getElementById('total_rv1').innerHTML = formatCurrency(total_rv1);
	document.getElementById('total_rv2').innerHTML = formatCurrency(total_rv2);
	document.getElementById('total_rv').innerHTML = formatCurrency(total_rv);
	
	document.getElementById('grandtotal').innerHTML = formatCurrency(grandtotal);
}

//////////////////////////////////////////////////////////
//////        Function to pop a new window        ////////

function newWindow(theURL,winName,winWidth,winHeight,features) { //v2.0
	//winWidth = 460;
	//winHeight = 500;
	winName = 'popwindow';
	winOffX = (screen.width - winWidth - 22)/2;
	winOffY = (screen.height - winHeight)/2 - 50;
	features = "width=" + winWidth + ",height=" + winHeight + ",toolbar=0,menubar=0,scrollbars=0,resizable=0,location=0,directories=0,status=0,left=" + winOffX + ",top=" + winOffY;
	window.open(theURL,winName,features);
}

//////////////////////////////////////////////////////////
//////        Function to pop a new window        ////////

function citysearch_refresh() {
	search_city = document.form2.search_city.value;
	url = 'directory.php?s_type=city&strg='+search_city;
	window.location = url;
} // end of the 'citysearch_refresh()' function