var searchBox;
var server = 'www.alberta-canada.net';

window.onload = function() {
	var wl = window.location.href;
	if (wl.indexOf('aed-webvm-1') != -1)
		server = 'aed-webvm-1:85';

	var currentPage = window.location.href;
	if (currentPage.indexOf('login.cfm') != -1) {
		if (document.getElementById('loginUsername').value.length > 0)
			if (document.getElementById('loginPassword').value.length > 0)
				document.getElementById('loginSubmit').focus();
			else
				document.getElementById('loginPassword').focus();
		else
			document.getElementById('loginUsername').focus();
	}
	else {
		searchBox = document.getElementById('searchBox');
		if (searchBox) {
			searchBox.value = '';
			searchBox.focus();
		}
	}

	if (navigator.appVersion.indexOf('MSIE 6') != -1)
		document.getElementById('breadcrumbs').style.marginBottom = '-2px';
}

function showPreview(photo) {
	window.open('http://www.alberta-canada.net/photos/preview/'+photo+'.jpg','Preview','width=420,height=420,directories=no,fullscreen=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no','false');
}

function addImageToCart(photoID, callingTag) {
	if (window.XMLHttpRequest)
		//	all browsers
		var xmlhttp = new XMLHttpRequest();
	else if (window.ActiveXObject)
		//	except IE, of course
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

	if (!xmlhttp) {
		alert('connection failed');
		return;
	}

	var url = 'http://' + server + '/modifyCart.cfm?photo=' + photoID + '&action=add';

	xmlhttp.open("get", url, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				var success = xmlhttp.responseXML.documentElement.getElementsByTagName('success')[0].firstChild.data;
				if (success == 'error') {
					alert('There was an error adding the image to your cart.  Please try again.');
					return false;
				}
				else if (success == 'alreadyInCart') {
					alert('This image is already in your cart.');
				}
				else if (success == 'cartFull') {
					alert('Your cart is already full.  You will now be taken to checkout so you may request the images in your cart.');
					window.location = 'checkout.cfm';
					return false;
				}

				setClass(callingTag, 'imageInCart');
				callingTag.innerHTML = 'in cart';

			}
			else
				alert('Connection error.');
		}
	}
	xmlhttp.send(null);
}

function removeImageFromCart(photoID) {
	var photoBox = document.getElementById('photoBox' + photoID);

	if (window.XMLHttpRequest)
		//	all browsers
		var xmlhttp = new XMLHttpRequest();
	else if (window.ActiveXObject)
		//	except IE, of course
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

	if (!xmlhttp) {
		alert('connection failed');
		return;
	}

	var url = 'http://' + server + '/modifyCart.cfm?photo=' + photoID + '&action=remove';

	xmlhttp.open("get", url, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				var success = xmlhttp.responseXML.documentElement.getElementsByTagName('success')[0].firstChild.data;
				if (success == 'error') {
					alert('There was an error removing the image from your cart.  Please try again in a few minutes.');
					return false;
				}

				var imagesLeft = success;

				//	remove that photo's info from page
				document.getElementById('shoppingCartPhotosContainer').removeChild(photoBox);

				if (imagesLeft == 0) {
					document.getElementById('photoCount_bottom').innerHTML = '0 photos';
					document.getElementById('photoCount_top').innerHTML = '0 photos';
					document.getElementById('content').removeChild(document.getElementById('shoppingCartStatus_bottom'));
					document.getElementById('shoppingCartStatus_top').removeChild(document.getElementById('proceedToCheckout_top'));
					document.getElementById('navCartCount').innerHTML = '';
				}
				else {
					if (imagesLeft == 1) {
						document.getElementById('photoCount_top').innerHTML = '1 photo';
						document.getElementById('photoCount_bottom').innerHTML = '1 photo';
						document.getElementById('navCartCount').innerHTML = ' (1 photo)';
					}
					else {
						document.getElementById('photoCount_top').innerHTML = imagesLeft + ' photos';
						document.getElementById('photoCount_bottom').innerHTML = imagesLeft + ' photos';
						document.getElementById('navCartCount').innerHTML = ' (' + imagesLeft + ' photos)';
					}
				}

			}
			else
				alert('Connection error.');
		}
	}
	xmlhttp.send(null);
}

function setClass(elem, className) {
//	IE bug/workaround 1
//	you cannot set the class of an element with setAttribute('class','NAME_OF_CLASS')
//	as you can in all standards-based browsers
	if (isCrappIE())
		elem.setAttribute('className', className);
	else
		elem.setAttribute('class', className);
}

function isCrappIE() {
//	determines if browser is Internet Explorer
	if (navigator.appName.substring(0,4) == "Micr")
		return true;
	else
		return false;
}

function checkoutFormCheck(form) {
	var imageUseOther = form.imageUseOther.value;
	var description = form.description.value;
	var anticipatedReach = form.anticipatedReach.value;
	var anticipatedReach_1 = form.anticipatedReach_1.value;
	var anticipatedReach_2 = form.anticipatedReach_2.value;
	var geographicOther = form.geographicOther.value;
	var shippingAddress = form.shippingAddress.value;

	for(var i=0; i < form.imageUse.length; i++) {
		if(form.imageUse[i].checked) var choiceMade = 1;
	}
	if(!choiceMade) {
		alert("Please select your use of the photograph(s).");
		form.imageUse[0].focus();
		return false;
	}

	if(form.imageUse[4].checked && !imageUseOther.length) {
		alert("Please describe what the images will be used for, if not an article, story, presentation, brochure or display.");
		form.imageUseOther.focus();
		return false;
	}

	if(!description.length) {
		alert("Please describe in detail what you intend to use the images for.");
		form.description.focus();
		return false;
	}

	if (anticipatedReach_1.length > 0 && anticipatedReach_2.length > 0) {
		alert("Please indicate the anticipated reach of your project in EITHER the 'Print Run' field OR the 'Audience Reach' field as appropriate.");
		form.anticipatedReach_1.focus();
		return false;
	}
	if (anticipatedReach_1.length > 0)
		anticipatedReach = anticipatedReach_1;
	else if (anticipatedReach_2.length > 0)
		anticipatedReach = anticipatedReach_2;

	if(!anticipatedReach.length) {
		alert("Please describe the anticipated reach of your project.");
		form.anticipatedReach.focus();
		return false;
	}

	for(var i=0; i < form.geographic.length; i++) {
		if(form.geographic[i].checked) var choiceMade2 = 1;
	}
	if(!choiceMade2) {
		alert("Please indicate in what geographical region(s) your project will be seen.");
		form.geographic[0].focus();
		return false;
	}

	if(form.geographic[5].checked && !geographicOther.length) {
		alert("Please indicate in what geographical region(s) your project will be seen.");
		form.imageUseOther.focus();
		return false;
	}

	for(var i=0; i < form.projectType.length; i++) {
		if(form.projectType[i].checked) var choiceMade3 = 1;
	}
	if(!choiceMade3) {
		alert("Please identify your business.");
		form.projectType[0].focus();
		return false;
	}

	for(var i=0; i < form.acquire.length; i++) {
		if(form.acquire[i].checked) var choiceMade4 = 1;
	}
	if(!choiceMade4) {
		alert("Please choose how you will receive the images.");
		form.acquire[0].focus();
		return false;
	}

	return true;
}

function loginFormCheck(form) {
	var username = form.username.value;
	var password = form.password.value;

	if(!username.length) {
		alert('Please enter your username.  It is the email address you used to register.');
		return false;
	}

	return true;
}

function registerFormCheck(form) {
	var firstName = form.firstName.value;
	var lastName = form.lastName.value;
	var company = form.company.value;
	var address1 = form.address1.value;
	var address2 = form.address2.value;
	var city = form.city.value;
	var province = form.province.value;
	var postal = form.postal.value;
	var country = form.country.value;
	var telephone = form.telephone.value;
	var email = form.email.value;
	var fax = form.fax.value;
	var cell = form.cell.value;
	var website = form.website.value;
	var useBlurb = form.useBlurb.value;

	if(!firstName.length) {
		alert("Please type your first name.");
		form.firstName.focus();
		return false;
	}
	if(!lastName.length) {
		alert("Please type your last name.");
		form.lastName.focus();
		return false;
	}
	if(address2.length && !address1.length) {
		form.address1.value = form.address2.value;
		form.address2.value = "";
	}
	if(!address1.length) {
		alert("Please type your address.");
		form.address1.focus();
		return false;
	}
	if(!city.length) {
		alert("Please type your city.");
		form.city.focus();
		return false;
	}
	if(!province.length) {
		alert("Please type your province or state.");
		form.province.focus();
		return false;
	}
	if(!postal.length) {
		alert("Please type your postal or zip code.");
		form.postal.focus();
		return false;
	}
	if(!country.length) {
		alert("Please type your country.");
		form.country.focus();
		return false;
	}
	if(!telephone.length) {
		alert("Please type your phone number.");
		form.telephone.focus();
		return false;
	}
	if(!email.length) {
		alert("Please type your email address.");
		form.email.focus();
		return false;
	}
	var goodEmail = verifyEmail(email);

	if(!goodEmail) {
		alert("Please enter a valid email address.");
		form.email.focus();
		return false;
	}

	if(!useBlurb.length) {
		alert("Please briefly describe what you will be using the images for.");
		form.useBlurb.focus();
		return false;
	}

	if(useBlurb.length > 10000) {
		alert("Please shorten your description of your use of the photos.");
		form.useBlurb.focus();
		return false;
	}
	return true;
}

function verifyEmail(emailAddress) {
	var at="@"
	var dot="."
	var lat=emailAddress.indexOf(at)
	var lstr=emailAddress.length
	var ldot=emailAddress.indexOf(dot)
	if (emailAddress.indexOf(at)==-1) return false;
	if (emailAddress.indexOf(at)==-1 || emailAddress.indexOf(at)==0 || emailAddress.indexOf(at)==lstr) return false;
	if (emailAddress.indexOf(dot)==-1 || emailAddress.indexOf(dot)==0 || emailAddress.indexOf(dot)==lstr) return false;
	if (emailAddress.indexOf(at,(lat+1))!=-1) return false;
	if (emailAddress.substring(lat-1,lat)==dot || emailAddress.substring(lat+1,lat+2)==dot) return false;
	if (emailAddress.indexOf(dot,(lat+2))==-1) return false;
	if (emailAddress.indexOf(" ")!=-1) return false;
	return true
}

function openAedTVPlayer(contentID, parentCategoryID, categoryID) {
	var mainPage = "Frames.aspx";
	var urlParams = "?";
	var windowParams = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=700,height=580";

	if (contentID != null) {
		urlParams += "&contentID=" + contentID;
	}

	if (parentCategoryID != null) {
		urlParams += "&parentCategoryID=" + parentCategoryID;
	}

	if (categoryID != null) {
		urlParams += "&categoryID=" + categoryID;
	}

	// Open the window.
	window.open("http://www.jetstreammedia.com/aedtv/Viewer/" + mainPage + urlParams, "aedtv", windowParams);
}

function openTravelAlbertaPlayer(contentID, parentCategoryID, categoryID) {
	var mainPage = "Frames.aspx";
	var urlParams = "?";
	var windowParams = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=700,height=580";

	if (contentID != null) {
		urlParams += "&contentID=" + contentID;
	}

	if (parentCategoryID != null) {
		urlParams += "&parentCategoryID=" + parentCategoryID;
	}

	if (categoryID != null) {
		urlParams += "&categoryID=" + categoryID;
	}

	// Open the window.
	window.open("http://www.jetstreammedia.com/travelAlbertaTV/Viewer/" + mainPage + urlParams, "travelAlbertaTV", windowParams);
}