// BC B&Bs Only! Javascript functions

var availData = ""
var pageLoaded = false

function availbb(availDate) {
	var idx = 0;
	var temp = "";
	var path = "http://www.bcbbonly.com/images/availbb/"

//	Initialize variables for the first time.
	if (!pageLoaded) {
		pageLoaded = true;
		availData = document.updateavailbb.availData.value;
	}

//	Check to see if date is already in availData. If so, it needs to be removed. If not, add it.
//	Switch image sources.

	idx = availData.indexOf(availDate.substring(1,11));
	if (idx != -1) {
		availData = availData.substring(0,idx) + availData.substring(idx+11, availData.length);
		document.images[availDate].src = (path + availDate.substring(9,11) + "v.gif");
		document.images[availDate].alt = "";
	} else {
		availData += (availDate.substring(1,11) + ":");
		document.images[availDate].src = (path + availDate.substring(9,11) + "nv.gif");
		document.images[availDate].alt = "100% booked";
	}
	document.updateavailbb.availData.value = availData;
}

function weatherSpawn() {
	var opts = "width=150,height=90";
	var city = document.weather.city.options[document.weather.city.selectedIndex].value;

	nW = window.open("", "Weather", opts);
	nW.document.write('<html><head><title></title></head>');
	nW.document.write('<body TOPMARGIN=0 LEFTMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0 onBlur="setTimeout(\'self.close()\',500)">');

	nW.document.write('<script language=javascript src =\'http://www.theweathernetwork.com/inter/weathercentre/weatherbutton/can/static/'+city+'.js\' RunAt=\'Server\'>');
	nW.document.write('</script>');

	nW.document.close();
	nW.focus();
}

function translate(lang) {
	var babelLang = new Array();
	babelLang[1] = "en_zh"; //Chinese
	babelLang[2] = "en_nl"; //Dutch
	babelLang[3] = "en_fr"; //French
	babelLang[4] = "en_de"; //German
	babelLang[5] = "en_el"; //Greek
	babelLang[6] = "en_it"; //Italian
	babelLang[7] = "en_ja"; //Japanese
	babelLang[8] = "en_ko"; //Korean
	babelLang[9] = "en_pt"; //Portuguese
	babelLang[10] = "en_ru"; //Russian
	babelLang[11] = "en_es"; //Spanish

	var url = document.location.href;
	url = url + "?t=1";
	var babelfish = ("http://babelfish.altavista.com/babelfish/tr?doit=done&tt=url&intl=1&url=" + url + "&lp=" + babelLang[lang]);

	nW = window.open(babelfish, "translate", "");
	nW.focus();
}

function map(city,postalcode) {
//	Remove MapQuest, August 23, 2006
//	postalcode = postalcode.replace(/[+]/gi, "");
//	city = city.replace(/[+]/gi, "");
//	city = city.replace(/[%27]/gi, "");
//	if (city != "") { // city map
//		var mapblast = ("http://www.mapquest.com/maps/map.adp?formtype=address&searchtype=address&country=CA&addtohistory=&address=&city=" + city + "&state=BC&zipcode=");
//	} else { // address, using postal code
//		var mapblast = ("http://www.mapquest.com/maps/map.adp?formtype=address&searchtype=address&country=CA&addtohistory=&address=&city=&state=BC&zipcode=" + postalcode);
//	}

//	Add Google Maps, August 23, 2006
	postalcode = postalcode.replace(/[ ]/gi, "%20");
	city = city.replace(/[ ]/gi, "%20");
	city = city.replace(/[~]/gi, "%27");
//	if (city != "") { // city map
	if (postalcode == "") {
		var googlemaps = ("http://www.google.ca/maps?f=q&hl=en&q=" + city + ",%20BC,%20CA");
	} else {
		var googlemaps = ("http://www.google.ca/maps?f=q&hl=en&q=" + city + ",%20BC,%20" + postalcode + ",%20CA");
	}
//	} else { // address, using postal code
//		var googlemaps = ("http://www.google.ca/maps?f=q&hl=en&q=BC,%20" + postalcode + ",%20CA");
//	}
	nW = window.open(googlemaps, "map");
	nW.focus();
}

function searchmap() {
	var idx = document.search.city.selectedIndex;
	var city = document.search.city.options[idx].value;
	city = city.replace(/[ ]/gi, "+");
	city = city.replace(/[']/gi, "%27");
	map(city,"");
}

function bookmark(url) {
	if (document.all) {
		window.external.AddFavorite(("http://www.bcbbonly.com"+url), 'BC B+Bs Only!');
	} else {
		alert('Click OK, then type CTRL-D.');
	}
}

function opt() {
	var totalPrice = 0;
	var discount = 1;

	// 5% GST
	var gst = 105;
	if (document.step1.optiona.checked) {
		totalPrice += 30;
	}
	if (document.step1.optionb[1].checked) {
		totalPrice += 40;
	}
	if (document.step1.optionb[2].checked) {
		totalPrice += 72;
	}
	if (document.step1.optionb[3].checked) {
		totalPrice += 125;
	}
	if (document.step1.optionc.checked) {
		totalPrice += (45 * discount);
	}
	totalPrice = (Math.round(totalPrice * gst))/100;
	totalPrice += "00";
	if (totalPrice == "000") {
		totalPrice = "0";
	}
	if (totalPrice.lastIndexOf(".") == -1 && totalPrice != "0") {
		totalPrice = totalPrice.substring(0, totalPrice.length-2) + "." + totalPrice.substring(totalPrice.length-2, totalPrice.length);
	}
	totalPrice = totalPrice.substring(0, totalPrice.indexOf(".") + 3);
	document.step1.totalPrice.value = "$" + totalPrice + "/year";
	document.step1.price.value = totalPrice;
	if (!document.step1.optiona.checked && !document.step1.optionb[1].checked && !document.step1.optionb[2].checked && !document.step1.optionb[3].checked && !document.step1.optionc.checked) {
		alert('\rYou must select at least one option \rbefore proceeding. \r\rThank you.'); 
		return false;
	} else {
		if ((document.step1.optionb[1].checked || document.step1.optionb[2].checked || document.step1.optionb[3].checked) && !document.step1.optionc.checked) {
			document.step1.optionc.checked = "1";
			opt();
			alert('\rPlease Note: \r\rA banner ad must be purchased \rwith a display ad. \r\rThank you.');
		} else if (document.step1.optiona.checked && !document.step1.optionc.checked) {
			alert('\rPlease Note: \r\rAn email/website link works well with \ra display ad. \r\rIf you do not have a personal web site, \rit would be better to purchase a display ad. \r\rThank you.');
			return true;
		}
	}
}

function words(field) {
	if (field == "1") {
		var text = document.application.writeup.value
	} else if (field == "2") {
		var text = document.application.ratedetail.value
	}
	var words = 0
	var eol = unescape( "%0D" ) + unescape( "%0A" )

	for ( var x = 0; x < text.length; x++) {
		if (text.charAt(x) == " ") {
			words++
		} else if (text.substring(x,x+4) == (eol+eol)) {
			words++
			x += 3
		} else if (text.substring(x,x+2) == (eol)) {
			words++
			x++
		}
	}
	words++
	alert( "" + words + " words." )
}

function enlarge(id) {
	var opts = "width=50,height=50";
	bigWin = window.open("http://www.bcbbonly.com/enlarged-photo.php?id="+id, "BBImage"+id, opts);
	bigWin.focus();
}

function writeup() {
	var opts = "width=320,height=318";
	bigWin = window.open("http://www.bcbbonly.com/writeup-suggestions.php", "Writeup", opts);
	bigWin.focus();
}

function spellcheckit() {
	var opts = "width=450,height=500";
	bigWin = window.open("bcbbonly_spellcheck.php?form=application&fields=caption1,caption2,caption3,writeup,ratedetail", "Spellcheck", opts);
	bigWin.focus();
}

function currency_calculator(amount) {
	var opts = "width=400,height=430,resizeable=0,scrollbars";
	if (amount == undefined || amount == null) {
		bigWin = window.open("http://www.bcbbonly.com/currency-calculator.php?from=CAD&to=USD", "currency", opts);
	} else {
		bigWin = window.open("http://www.bcbbonly.com/currency-calculator.php?from=CAD&to=USD&amount="+amount, "currency", opts);
	}
	bigWin.focus();
}

function availability(account) {
	var opts = "width=560,height=545,resizeable=0,scrollbars";
	bigWin = window.open("http://www.bcbbonly.com/availbb.php?bb="+account, account, opts);
	bigWin.focus();
}

function secure() {
	alert("Secure transactions for BC B&Bs Only! \rare done via IslandNet.com \rwho use a Thawte Certificate to ensure \rsecure transmission of your information. \r\rClick the 'lock' icon in the \rbottom right of your browser \rto confirm.");
}

function log_out() {
	var url = document.location.href;
	document.location.replace(url);
}

function checkChoice() {
	var choice = document.contact.subject[document.contact.subject.selectedIndex].value;
	var loginAddress = "http://www.bcbbonly.com/login.php";
	var photosAddress = "http://www.bcbbonly.com/update-your-photos.php";
	var yes = false;

	// Depending on what is selected, give a message box.
	if (choice == 0 || choice == 7 || choice == 9) {
		// Do nothing for dividers
		return;
	} else if (choice >= 1 && choice <= 5) {
		// Login page
		if(confirm("You can update your information \ronline at anytime by visiting \rthe login page. \r\rClick 'OK' to login now, \ror 'Cancel' to continue \ryour message.")) {
			document.location.replace(loginAddress);
		}
	} else if (choice == 6) {
		// Photos page
		if (confirm("Would you like to visit our \r'Update Your Photos' \rinformation page? \r\rClick 'OK' to go there now, \ror 'Cancel' to continue \ryour message.")) {
			document.location.replace(photosAddress);
		}
	} else if (choice == 8) {
		// General Inquiry
		alert("Please Note: \r\rB.C. B&Bs Only! is not a reservation service. \r\rIf you are inquiring about availability, \rplease contact the B&B you are interested in \rdirectly using either the email address or \rthe phone number on their listing.\r");
	} else if (choice == 10) {
		// Personal website
		if (!confirm("Are you looking for a web designer \rfor your personal website \r(eg. www.YourBBName.com)? \r\rClick 'OK' to continue your message.")) {
			document.contact.subject.selectedIndex = 0;
		}
	}
}

function autocorrect(mode) {
	var phone = document.application.phone.value;
	var fax = document.application.fax.value;
	var tollfree = document.application.tollfree.value;
	var postalcode = document.application.postalcode.value;
	var email = document.application.email.value;

	if (mode == "phone") {
		phone = phone.replace(/\D/g,"");
		if (phone != "") {
			phone = "(" + phone.substring(0,3,phone) + ") " + phone.substring(3,6,phone) + "-" + phone.substring(6,10,phone);
			document.application.phone.value = phone;
		}

	} else if (mode == "fax") {
		fax = fax.replace(/\D/g,"");
		if (fax != "") {
			fax = "(" + fax.substring(0,3,fax) + ") " + fax.substring(3,6,fax) + "-" + fax.substring(6,10,fax);
			document.application.fax.value = fax;
		}

	} else if (mode == "tollfree") {
		tollfree = tollfree.replace(/\D/g,"");
		if (tollfree != "") {
			if (tollfree.substring(0,1,tollfree) != "1") {
				tollfree = "1" + tollfree;
			}
			tollfree = tollfree.substring(0,1,tollfree) + "-" + tollfree.substring(1,4,tollfree) + "-" + tollfree.substring(4,7,tollfree) + "-" + tollfree.substring(7,11,tollfree);
			document.application.tollfree.value = tollfree;
		}

	} else if (mode == "postalcode") {
		postalcode = postalcode.toUpperCase();
		postalcode = postalcode.replace(/ /g,""); 
		postalcode = postalcode.substring(0,6,postalcode); 
		postalcode = postalcode.substring(0,3,postalcode) + " " + postalcode.substring(3,6,postalcode); 
		document.application.postalcode.value = postalcode;

	} else if (mode == "email") {
		email = email.toLowerCase();
		email = email.replace(/ /g,""); 
		document.application.email.value = email;
	}
}

function bbe(account) {
	 // sends an email to the B&B owner.
	 var url = "http://www.bcbbonly.com/redirect.php?m=4&a=" + account;
	 nW = window.open(url, account);
	 nW.focus();
}

function bbr(account) {
	 // sends an email to the B&B owner.
	 var url = "http://www.bcbbonly.com/redirect.php?m=6&a=" + account;
	 nW = window.open(url, account);
	 nW.focus();
}

function bbpaypal() {
	 // Alerts user that they need to click on the "Buy Now" button.
	 alert("Please click on the 'Buy Now' button\nto complete your subscription and PayPal Purchase.\n\nYou do not need a PayPal account to pay with your credit card.\n\nThank you.");
}

// End of javascript functions