function getAjaxRequest() {	
var xmlHttp;

	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
		return xmlHttp;
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			return xmlHttp;
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				return xmlHttp;
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
}

function checkDate (img)
{
		
	var request = getAjaxRequest();
	var url = "";
	var month = document.getElementById("month").value;
	var day = document.getElementById("day").value;
	var year = document.getElementById("year").value;

	if (month.length==1)
		month = '0'+month;
	if (day.length==1)
		day = '0'+day;
	if (year.length!=4)
		document.getElementById("month").value = "----";
	
	date = month+day+year;
	
	request.onreadystatechange=function()
    {
		if(request.readyState==4)
		{
			document.getElementById("availability_form").innerHTML = request.responseText;
		}
    }

	url = "kwu_ajax.php?cmd=checkdate";
	url += "&date="+date;
	
	//document.getElementById("availability_form").innerHTML = "<img src=\"image/loading_white.gif\">";
	
	request.open("GET",url,true);
    request.send(null);
}

function clickNext ()
{
	var limit = (gallery_page+1)*28;
	if (limit > num_images)
		limit = num_images;

	if (photo_index+1 < limit)
		clickThumb(photo_index+1);
	else
	{
		if (gallery_page+1 < num_pages)
		{			
			clickThumb(photo_index+1);
			gallery_page += 1;
			showThumbs();
		}
	}
		

	
}

function clickBack ()
{
	if (photo_index > 0)
	{
		if (photo_index-1 < gallery_page*28)
		{
			clickThumb(photo_index-1);
			gallery_page -= 1;
			showThumbs();
		}
		else
			clickThumb(photo_index-1);	
	}
}

function clickThumb (index)
{

	photo_index = index;

	var html = '<img class="gallery_item" src="'+image_array[photo_index]+'">';

	if (orientation_array[photo_index]=='landscape')
	{	
		document.getElementById("gallery_container").style.left = "10px";
		document.getElementById("gallery_container").style.width = "411px";
		document.getElementById("gallery_container").style.height = "284px";
		document.getElementById("gallery_container").style.padding = "18px 0px 0px 18px";
		document.getElementById("gallery_container").style.backgroundImage = "url(\"images/landscape_bg.gif\")";
	}
	else
	{	
		document.getElementById("gallery_container").style.left = "60px";
		document.getElementById("gallery_container").style.width = "299px";
		document.getElementById("gallery_container").style.height = "432px";
		document.getElementById("gallery_container").style.padding = "16px 0px 0px 16px";
		document.getElementById("gallery_container").style.backgroundImage = "url(\"images/portrait_bg.gif\")";
	}		
		
	document.getElementById("gallery_container").innerHTML = html;
}

function changePage (page)
{
	
	gallery_page = page;
	showThumbs();
	clickThumb(gallery_page*28);
	
}

function showThumbs ()
{
	var start = gallery_page*28;
	var limit = (gallery_page+1)*28;
	var i=0;
	var html = "";

	if (limit > num_images)
		limit = num_images;

	for (i = start; i < limit; i++)
		html += "<img src=\""+thumb_array[i]+"\" class=\"thumb\" onclick=\"clickThumb("+i+");\">\n";
		
	html += "<br>&nbsp;\n";	
		
	for (i = 0; i < num_pages; i++)
	{
		if (gallery_page==i)
			html += (i+1)+"&nbsp;";
		else
			html += "<a href=\"#\" onclick=\"changePage("+i+");\">"+(i+1)+"</a>&nbsp;";	
	}
	
	html += "<br>\n";
	
	document.getElementById("thumb_container").innerHTML = html;
	//alert(html);

}

function updatePackageTotal (img)
{
		
	var request = getAjaxRequest();
	var url = "kwu_ajax.php?cmd=packagetotal";
	var result;
	
	var hours = document.forms["wedding_details"].elements["package_hours"].value;
	//var add_photo = document.forms["wedding_details"].elements["add_photo"].value;
	var q5x5 = document.forms["wedding_details"].elements["q5x5"].value;
	var q7x7 = document.forms["wedding_details"].elements["q7x7"].value;
	var q10x10 = document.forms["wedding_details"].elements["q10x10"].value;
	var code = document.forms["wedding_details"].elements["code"].value;
	var express = document.forms["wedding_details"].elements["express"].checked;
	var photomingle = document.forms["wedding_details"].elements["photomingle"].checked;

	url += "&hours="+hours;
	url += "&express="+express;
	url += "&photomingle="+photomingle;
	//url += "&add_photo="+add_photo;
	url += "&q5x5="+q5x5;
	url += "&q7x7="+q7x7;
	url += "&q10x10="+q10x10;
	url += "&code="+code;
	
	request.onreadystatechange=function()
    {
		if(request.readyState==4)
		{
			document.getElementById("package_total").innerHTML = '$'+request.responseText;
		}
    }
	
	document.getElementById("package_total").innerHTML = "<img src=\"images/loading.gif\">";
	
	//alert(url);
	
	request.open("GET",url,true);
    request.send(null);
}

function showPackage (p)
{
	if (p=='a')
	{
		document.forms["wedding_details"].elements["package_hours"].value = 12;
		document.forms["wedding_details"].elements["add_photo"].value = 0;
		document.forms["wedding_details"].elements["q5x5"].value = 0;
		document.forms["wedding_details"].elements["q7x7"].value = 0 ;
		document.forms["wedding_details"].elements["q10x10"].value = 1;
		document.forms["wedding_details"].elements["package_credits"].value = 500;		
	}
	if (p=='b')
	{
		document.forms["wedding_details"].elements["package_hours"].value = 8;
		document.forms["wedding_details"].elements["add_photo"].value = 0;
		document.forms["wedding_details"].elements["q5x5"].value = 0;
		document.forms["wedding_details"].elements["q7x7"].value = 1;
		document.forms["wedding_details"].elements["q10x10"].value = 0;
		document.forms["wedding_details"].elements["package_credits"].value = 250;		
	}
	if (p=='c')
	{
		document.forms["wedding_details"].elements["package_hours"].value = 4;
		document.forms["wedding_details"].elements["add_photo"].value = 0;
		document.forms["wedding_details"].elements["q5x5"].value = 1;
		document.forms["wedding_details"].elements["q7x7"].value = 0;
		document.forms["wedding_details"].elements["q10x10"].value = 0;
		document.forms["wedding_details"].elements["package_credits"].value = 125;		
	}	
	
	updatePackageTotal();

}