function moveArrow(offVal) {
	if(offVal!='') $('navBox').morph({'background-position': offVal+'px 25px'});
	else $('navBox').morph({'background-position': $('original_offset').value+'px 25px'});
}
function runSlideShow() {
	var id_string = $('id_string').value.split('#');
	var nextid = parseInt($('currentID').value) + 1;
	if(nextid==(parseInt($('total').value) + 1)) nextid = 1;
	$('currentID').value = nextid;
	$('frontImg').style.backgroundImage = $('backImg').style.backgroundImage;
	$('frontImg').fade('show');
	$('backImg').style.backgroundImage = 'url('+id_string[nextid]+')';
	setTimeout("performSwap()",$('timeout').value);
}
function performSwap() {
	$('frontImg').fade('out');
	setTimeout("runSlideShow()",600);
}
function showLabel(id) {
	$('text'+id).style.display = '';
	$('text'+id).fade(0.9);
}
function hideLabel(id) {
	$('text'+id).fade('out');
}
function hideTextBoxes(cntr) {
	for(var t=1;t<=parseInt(cntr);t++) {
		$('text'+t).fade('hide');
	}
}
function setHeightWidth() {
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
  	}
	$('boxCheck').style.height = yWithScroll+'px';
}
function closeItemDescription() {
	$('boxCheck').fade('out');
	$('changeBox').fade('out');
	document.body.style.height = '';
	setTimeout("$('boxCheck').style.display = 'none';$('changeBox').style.display = 'none';",600);
}

function getHTTPObject() {
    if (typeof XMLHttpRequest != 'undefined') {
        return new XMLHttpRequest();
    }
    try {
        return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
    }
    return false;
}

var http = getHTTPObject();

function loadItemDescription(itemID) {
	http.open("GET", "includes/get_item.php?itemID="+itemID, true);
	http.onreadystatechange = useHttpResponse;
	http.send(null);
}

var ie5 = (document.all && document.getElementById) ? true : false;
function useHttpResponse() {
	if(http.readyState==4) {
		// 0 - itemID   1 - description_img src   2 - item_name   3 - item_wood   4 - item_finishes   5 - other items   6 - items_options_table
		var textout = http.responseText.split("@#@");
        $('description_img').src = textout[1];
        $('item_name').innerHTML = textout[2];
        $('item_wood').innerHTML = textout[3];
        $('item_finishes').innerHTML = textout[4];
		$('item_link').itemID = textout[0];
		$('item_link').onclick = function() { print_PDF(this.itemID); }

		if(textout[5]!='') {
			$('other_items_row1').style.display = '';
			$('other_items_row2').style.display = '';
			var target = $('other_items');
			target.innerHTML = '';
			var other_items = textout[5].split('(-)');
            div_cnt = 0;
			for(var t=0;t<other_items.length;t++) {
                if(div_cnt==0) {
                    spaceDiv = document.createElement("div");
                    spaceDiv.innerHTML = " ";
            	    target.appendChild(spaceDiv);
                }
				var parts = other_items[t].split('||');
				var img = document.createElement("img");
				img.itemID = parts[0];
				img.style.cursor = 'hand';
				img.style.cursor = 'pointer';
				img.onclick = function() { $('changeBox').fade('out');setTimeout("loadItemDescription('"+this.itemID+"')",500); }
				img.src = parts[1];
				img.align = 'top';
				img.vspace = 5;
				img.hspace = 5;
            	spaceDiv.appendChild(img);
                div_cnt++;
                if(div_cnt==6) div_cnt = 0;
			}
		} else {
			$('other_items_row1').style.display = 'none';
			$('other_items_row2').style.display = 'none';
		}

		if(textout[6]!='') {
			var target = $('items_options_table');
		    if(ie5) { if(target.hasChildNodes()) while (target.childNodes.length>1) target.removeChild(target.lastChild); }
		    else { if(target.hasChildNodes()) while (target.childNodes.length>2) target.removeChild(target.lastChild); }
			var items_options = textout[6].split('(-)');
			for(var t=0;t<items_options.length;t++) {
				var parts = items_options[t].split('||');
				var tr = document.createElement("tr");
				for(var w=0;w<parts.length;w++) {
					var td = document.createElement("td");
					td.align = 'center';
					if(w>0) td.style.borderLeft = '1px solid #DDDDDD';
					if(w>1) td.innerHTML = parts[w]+'mm';
					else td.innerHTML = parts[w];
					tr.appendChild(td);
				}
				target.appendChild(tr);
			}
		}
		setTimeout("showDescription()",100);
	}
}

function showDescription() {
	var obj1 = $('boxCheck');
	var obj2 = $('changeBox');
	obj2.style.visibility = 'hidden';
	obj2.style.display = 'block';
	var div_width = parseInt(obj2.style.width);
    var div_height = parseInt(obj2.offsetHeight);
	var page_width = parseInt(document.body.offsetWidth);
	var page_height = parseInt(document.body.offsetHeight);
	obj1.style.height = page_height+'px';
	obj1.style.width = page_width+'px';

	if(div_height>(page_height-60)) {
		page_height = div_height + 60;
        document.body.style.height = page_height+'px';
		obj1.style.height = page_height+'px';
	}

    var leftOff = Math.round((page_width - div_width)/2);
    var topOff = Math.round((page_height - div_height)/2) + document.body.scrollTop;
	obj2.style.top = topOff+'px';
	obj2.style.left = leftOff+'px';
	obj1.style.display = 'block';
	obj1.fade(0.4);
	obj2.style.display = 'block';
	obj2.fade('in');
}

function print_PDF(itemID) {
	var url = 'print_item.php?id='+itemID;
	window.open(url, "item",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,modal=yes,close=no,resizable=1,width=800,height=600,left=100,top=100');
}
function loadGallery(id,width,height,comment,ext) {
	var page_width = parseInt(document.body.clientWidth);
	var page_length = parseInt(document.body.scrollHeight);
	var page_height = parseInt(document.body.clientHeight);
    if(page_length<page_height) page_length = page_height;

	var scroll = document.body.scrollTop;

	var obj = document.getElementById('image_div');
	var div_width = parseInt(obj.style.width);
	var div_height = parseInt(obj.style.height);

	var prevleftOff = Math.round((page_width - div_width)/2);
	var newleftOff = Math.round((page_width - (width))/2);
	var prevtopOff = Math.round((page_height - div_height)/2) + scroll;
	var newtopOff = Math.round((page_height - (height))/2) + scroll;

	$('image_div').style.display = 'block';
	$('blackout').style.display = 'block';
	$('blackout').style.width = page_width+'px';
	$('blackout').style.height = page_length+'px';
	$('image_div').style.top=prevtopOff+'px';
	$('image_div').style.left=prevleftOff+'px';
	$('actual_img').src = 'uploads/c'+id+'.'+ext;
	$('comment_img').innerHTML = comment;
	$('actual_img').fade('hide');
	$('comment_img').fade('hide');
	$('blackout').morph({'opacity': 0.85});
	$('image_div').morph({'opacity': 1});
	setTimeout("$('image_div').morph({'left': '"+newleftOff+"px','width': '"+width+"px'});",500);
	setTimeout("$('image_div').morph({'top': '"+newtopOff+"px','height': '"+(height + 45)+"px'});",1000);
	setTimeout("$('actual_img').morph({'opacity': 1});",1500);
	setTimeout("$('comment_img').morph({'opacity': 1});",1500);
}

function closeGallery(width,height) {
	$('blackout').morph({'opacity': 0,'moz-opacity': 0});
	$('image_div').morph({'opacity': 0});
	setTimeout("$('blackout').style.display = 'none';",500);
	setTimeout("$('image_div').style.display = 'none';",500);
	setTimeout("$('image_div').style.width = '"+width+"px';",500);
	setTimeout("$('image_div').style.height = '"+height+"px';",500);
	setTimeout("$('image_div').style.background = '#FFFFFF url(images/loading.gif) no-repeat scroll top left';",500);
	setTimeout("$('actual_img').src = '';",500);
	setTimeout("$('comment_img').innerHTML = '';",500);
}
function adjustGallery() {
	if($('image_div').style.display=='block') {
		var height = $('image_div').offsetHeight;
		var scroll = document.body.scrollTop;
		var page_height = parseInt(document.body.clientHeight);
		var newtopOff = Math.round((page_height - (height + 20))/2) + scroll;
    	$('image_div').morph({'top': newtopOff+'px'});
	}
}
function resizeGallery() {
	if($('image_div').style.display=='block') {
		var page_width = parseInt(document.body.clientWidth);
		var page_length = parseInt(document.body.scrollHeight);
		var page_height = parseInt(document.body.clientHeight);
	    if(page_length<page_height) page_length = page_height;
       	$('blackout').style.width = page_width+'px';
		$('blackout').style.height = page_length+'px';

		var height = $('image_div').offsetHeight;
		var width = $('image_div').offsetWidth;
		var scroll = document.body.scrollTop;
		var newtopOff = Math.round((page_height - (height + 20))/2) + scroll;
		var newleftOff = Math.round((page_width - (width + 20))/2);
    	$('image_div').morph({'top': newtopOff+'px','left': newleftOff+'px'});
	}
}

var hoverTimeout = 0;
function loadClickEvents() {
	var t = 1;
	while($('img'+t)) {
		$('img'+t).addEvent('mousemove', function(event) {
			clearTimeout(hoverTimeout);
			if(!$('tooltip')) {
				var div = document.createElement("div");
				div.id = 'tooltip';
				div.className = 'tooltip';
				$(document.body).appendChild(div);
				$('tooltip').setStyle('opacity', 0);
				$('tooltip').setStyle('display', 'block');
				$('tooltip').set('morph',{duration:200});
			}
			$('tooltip').setStyle('left',(event.page.x + 15));
			$('tooltip').setStyle('top',(event.page.y + 15));
			$('tooltip').innerHTML = this.alt.toUpperCase();
			$('tooltip').morph({opacity: 0.75});
		});
		$('img'+t).addEvent('mouseout', function() {
			hoverTimeout = setTimeout("$('tooltip').morph({opacity: 0});",500);
		});
		t++;
	}
}

window.onload = function() {
	loadClickEvents();
}
function submitenter(myform,a) {
	var e = window.event;
	var ie5 = (document.all && document.getElementById) ? true : false;
	if(ie5) var e = event;
	if (e.keyCode==13) {
	   	myform.action.value=a;
	   	myform.submit();
	   	return false;
	} else return true;
}
function DateFormat(vDateName, vDateValue, e, dateCheck) {
if (vDateValue == "~") {
alert("AppVersion = "+navigator.appVersion+" \nNav. 4 Version = "+isNav4+" \nNav. 5 Version = "+isNav5+" \nIE Version = "+isIE4+" \nYear Type = "+vYearType+" \nSeparator = "+strSeperator);
vDateName.value = "";
vDateName.focus();
return true;
}
var key = getkey(e);
// control keys
if((key==null||key==0||key==8||key==9||key==13||key==16||key==27)&&!dateCheck) return true;
if (vDateValue == "") return true;

if(vDateValue.length > 10) {
		vDateValue = vDateValue.substr(0, 10);
		}

var numCheck = "0123456789"+strSeperator;
for(c=0;c<vDateValue.length;c++) {
	if(numCheck.indexOf(vDateValue.substr(c,1)) == -1 || (c!=2&&c!=5&&vDateValue.substr(c,1)==strSeperator)) {
		vDateValue = vDateValue.substr(0, c) + vDateValue.substr((c+1), vDateValue.length);
		}
	if(numCheck.indexOf(vDateName.value.substr(c,1)) == -1) {
		vDateValue= vDateValue.substr(0, c) + vDateValue.substr((c+1), vDateValue.length);
		}
	}
if(vDateValue.length==2||vDateValue.length==5) {
		vDateValue = vDateValue.substr(0, vDateValue.length) + strSeperator;
		}
vDateName.value = vDateValue;

if(!dateCheck) return false;
if(vDateValue.length==8) {
	//Turn a two digit year into a 4 digit year
	mToday = new Date();
	//If the year is greater than 30 years from now use 19, otherwise use 20
	checkYear = mToday.getFullYear() + 30;
	mYear = vDateValue.substr(6)
	mCheckYear = '20' + mYear;
	if (mCheckYear >= checkYear)
		mYear = '19' + mYear;
	else
		mYear = '20' + mYear;
	vDateValue = vDateValue.substr(0, 6) + mYear;
	vDateName.value = vDateValue;
	}
//
if(vDateValue.length < 10) {
	alert("Invalid Date\nPlease Re-Enter");
	setTimeout(function(){vDateName.focus();vDateName.select();},10)
	return false;
	}
if (!dateValid(vDateValue)) {
	alert("Invalid Date\nPlease Re-Enter");
	setTimeout(function(){vDateName.focus();vDateName.select();},10)
	return false;
	}
}

function checkrqd(type) {
	var req_desc = '';
    var contact_us=["name","email","subject","message"];
    var gown_loan=["stockist","gown_1","size_or_colour_preference","date_required","brides_name","name","phone","email"];
    var gown_order=["stockist","order_number","style","size","bust","waist","length","colour","your_reference","brides_name","DOW","gown_required_by","name","phone","email"];
    var allfields = {
        0: contact_us,
        1: gown_loan,
        2: gown_order
    };
    for(var t=0;t<allfields[type].length;t++) {
        if(document.getElementById(allfields[type][t]).value=='') req_desc += '\n * '+allfields[type][t].replace(/_/g,' ').capitalize();
    }
    if(req_desc!='') {
		alert('The following information is required to proceed:\n'+req_desc);
		return false;
	} else return true;
}

