// JavaScript Document
function toggle_display(id) {
	var disp = document.getElementById(id).style.display;
	if (disp == 'none') {
		disp = 'block';	
	} else {
		disp = 'none';
	}
}
function validate_required(field)
{
	with (field)
	{
		if (value==null||value=="")
		{
			return false;
		} else {
			field.focus();
			return true;
		}
	}
}
function validate_expression(field, expr)
{
	var ex = '';
	switch (expr) {
		case 'alpha':
			ex = /^[a-zA-Z ]+$/;
			break;
		case 'number':
			ex  = /^[0-9]+$/;
			break;
		case 'alphanum':		
			ex = /^[0-9a-zA-Z ]+$/;
			break;
		case 'email':
			ex = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-Z0-9]{2,4}$/;	
			break;
		default: // alpha
			ex = /^[a-zA-Z ]+$/;
			break;
	}

	var val = field.value.replace(/ /,'');
	if (val.match(ex)) {
		return true;	
	} else {
		field.focus();
		return false;
	}
}

function compare_values(field_a, field_b) {
	if (field_a.value == field_b.value) {
		return true;
	} else {
		field_a.focus();
		return false;
	}
}

function confirm_delete(obj_name, id, page) {
	if (confirm("Are you sure you want to delete " + obj_name + "?")) {
		window.location.href = page + "?id=" + id + "&action=remove";
	}
}

/* BEGIN DROP-DOWN MENU */
function loadNav(list) {
	var ids = new Array(list);
	for(var i = 0; i < ids.length; i++)
	{
		var sfEls = document.getElementById(ids[i]).getElementsByTagName("LI");
		for (var i = 0; i < sfEls.length; i++) {
			sfEls[i].onmouseover = function() {
				if(!this.oldClassName)
					this.oldClassName = this.className.replace(new RegExp("\\bsfhover\\b"), "");
				this.className += " sfhover";
			}
			sfEls[i].onmouseout = function() {
				this.className = this.oldClassName;
			}

			var sfEls2 = sfEls[i].getElementsByTagName("A");
			for (var j = 0; j < sfEls2.length; j++)
			{
				sfEls2[j].onclick = function() {
					if(this.href.substr(this.href.length-1) == "#")
						return false;
					else
						this.parentNode.parentNode.parentNode.className = this.parentNode.parentNode.parentNode.oldClassName;
					if(this.className == "subnewwindow")
					{
						window.open(this.href);
						return false;
					}
				}
			}
		}
	}
}
/* END DROP-DOWN MENU */

function checkContact(form) {
	var msg = '';
	if(!validate_required(form.cName)) {
		msg = msg + '  Your Name\n';
		form.cName.focus();
	}
	if(!validate_required(form.cEmailAddress) || !validate_expression(form.cEmailAddress, 'email')) {
		msg = msg + '  Your Email Address\n';
		form.cEmailAddress.focus();
	}
	
	if(msg.length > 0) {
		alert('Please complete or correct the following:\n' + msg);
		return false;
	}
	return true;
}
/* BEGIN GIFT CARD FUNCTIONS */
function checkGiftCard(form) {
	// require gcAmount, gcPurchEmailAddress, ...
	var msg = '';
	if(!validate_required(form.gcAmount) && !validate_expression(form.gcAmount, 'number')) {
		msg = msg + '  The Amount\n';
		form.gcAmount.focus();
	}
	if(!validate_required(form.gcPurchEmailAddress) || !validate_expression(form.gcPurchEmailAddress, 'email')) {
		msg = msg + '  Your Email Address\n';
		form.gcPurchEmailAddress.focus();
	}
	if(!validate_required(form.gcPurchFirstName)) {
		msg += '  Your First Name\n';
		form.gcPurchFirstName.focus();
	}
	if(!validate_required(form.gcPurchLastName)) {
		msg += '  Your Last Name\n';
		form.gcPurchLastName.focus();
	}
	if(!validate_required(form.gcPurchPhone)) {
		msg += '  Your Phone Number\n';
		form.gcPurchPhone.focus();
	}
	if(!validate_required(form.gcPurchAddress)) {
		msg += '  Your Address\n';
		form.gcPurchAddress.focus();
	}
	if(!validate_required(form.gcPurchCity)) {
		msg += '  Your City\n';
		form.gcPurchCity.focus();
	}
	if(!validate_required(form.gcPurchState)) {
		msg += '  Your State\n';
		form.gcPurchState.focus();
	}
	if(!validate_required(form.gcPurchZip)) {
		msg += '  Your Zip Code\n';
		form.gcPurchZip.focus();
	}	
	
	if(msg.length > 0) {
		alert('Please complete or correct the following:\n' + msg);
		return false;
	}
	return true;
}
function updateOrderAmount() {
	var gcAmount = document.getElementById('gcAmount').options[document.getElementById('gcAmount').selectedIndex].value;
	var gcQty = document.getElementById('gcQty').options[document.getElementById('gcQty').selectedIndex].value;
	amount = parseInt(gcAmount) * parseInt(gcQty);

	document.getElementById('orderAmount').removeChild(document.getElementById('orderAmount').firstChild);
	document.getElementById('orderAmount').appendChild(document.createTextNode(amount));
	updateRecipients();
	
	return false;
}
function updateRecipients() {
	var eQty = document.getElementById('gcQty').value;
	var rQty = document.getElementById('recipientCount').value;
	for(r=rQty; r>0; r--) {
		if(r > eQty) // remove entry
			removeRecipient(r);
	}
	for(e=1; e<=eQty; e++) {
		if(e > rQty) // add entry
			addRecipient(e);
	}
	if(eQty > 0)
		document.getElementById('recipients').style.display = 'block';
	else 
		document.getElementById('recipients').style.display = 'none';
	document.getElementById('recipientCount').value = eQty;
	//gotoRecipient(eQty);
}
function removeRecipient(r) {
	var rec = document.getElementById("recipientWrapper_"  + r);
	while(rec.hasChildNodes()) {
		rec.removeChild(rec.lastChild);
	}
	var recs = document.getElementById("recipients");
	recs.removeChild(rec);
}
function addRecipient(r) {
	if(document.getElementById("recipientWrapper_"  + r))
		return;
			
	var newWrapper = document.createElement("div");
	newWrapper.id = "recipientWrapper_"  + r;
	//newWrapper.className = "giftrows clearing";
	var newTitle = document.createElement("h4");
	newTitle.id = "recipientTitle_" + r;
	newTitle.appendChild(document.createTextNode("RECIPIENT " + r + " INFORMATION"));
	newWrapper.appendChild(newTitle);
	
	var newWrap0 = document.createElement("div");
	newWrap0.className = "giftrows clearing";
	var newWrap1 = document.createElement("div");
	newWrap1.className = "giftrows clearing";
	var newWrap2 = document.createElement("div");
	newWrap2.className = "giftrows clearing";
	var newWrap3 = document.createElement("div");
	newWrap3.className = "giftrows clearing";
	var newWrap4 = document.createElement("div");
	newWrap4.className = "giftrows clearing";
	var newWrap5 = document.createElement("div");
	newWrap5.className = "giftrows clearing";
	var newWrap6 = document.createElement("div");
	newWrap6.className = "giftrows clearing";
	
	var newP = document.createElement("p");
	var newSpan = document.createElement("span");
	newSpan.className = "small";
	newSpan.appendChild(document.createTextNode("(If not checked or no address is provided, gift cards will be sent to purchaser)"));
	var newField0 = document.createElement("input");
	newField0.type = "checkbox";
	newField0.id = "gcSendToRecipient_" + r;
	newField0.name = newField0.id;
	newP.appendChild(newField0);
	newP.appendChild(document.createTextNode(" PLEASE SEND THIS TO RECIPIENT"));
	newP.appendChild(document.createElement("br"));
	newP.appendChild(newSpan);
	newWrapper.appendChild(newP);
	
	var newLabel1 = document.createElement("label");
	newLabel1.className = "gift";
	newLabel1.id = "lblTo_" + r;
	var newLabel2 = document.createElement("label");
	newLabel2.className = "gift";
	newLabel1.id = "lblAddress_" + r;
	var newLabel3 = document.createElement("label");
	newLabel3.className = "gift";
	newLabel1.id = "lblCity_" + r;
	var newLabel4 = document.createElement("label");
	newLabel4.className = "gift";
	newLabel1.id = "lblState_" + r;
	var newLabel5 = document.createElement("label");
	newLabel5.className = "gift";
	newLabel1.id = "lblZip_" + r;
	var newLabel6 = document.createElement("label");
	newLabel6.className = "gift";
	newLabel1.id = "lblNote_" + r;
	
	newLabel1.appendChild(document.createTextNode("HOW WOULD YOU LIKE THE CARD ADDRESSED?"));
	newLabel2.appendChild(document.createTextNode("ADDRESS:"));
	newLabel3.appendChild(document.createTextNode("CITY:"));
	newLabel4.appendChild(document.createTextNode("STATE:"));
	newLabel5.appendChild(document.createTextNode("ZIP:"));
	newLabel6.appendChild(document.createTextNode("NOTE:"));
	
	newWrap1.appendChild(newLabel1);
	newWrap2.appendChild(newLabel2);
	newWrap3.appendChild(newLabel3);
	newWrap4.appendChild(newLabel4);
	newWrap5.appendChild(newLabel5);
	newWrap6.appendChild(newLabel6);
	
	var newField1 = document.createElement("input");
	newField1.id = "gcTo_" + r;
	newField1.name = newField1.id;
	newField1.className = "gift";
	newWrap1.appendChild(newField1);
	
	var newField2 = document.createElement("input");
	newField2.id = "gcAddress_" + r;
	newField2.name = newField2.id;
	newField2.className = "gift";
	newWrap2.appendChild(newField2);
	
	var newField3 = document.createElement("input");
	newField3.id = "gcCity_" + r;
	newField3.name = newField3.id;
	newField3.className = "gift";
	newWrap3.appendChild(newField3);
	
	var newField4 = document.createElement("input");
	newField4.id = "gcState_" + r;
	newField4.name = newField4.id;
	newField4.className = "gift";
	newWrap4.appendChild(newField4);
	
	var newField5 = document.createElement("input");
	newField5.id = "gcZip_" + r;
	newField5.name = newField5.id;
	newField5.className = "gift";
	newWrap5.appendChild(newField5);
	
	var newField6 = document.createElement("textarea");
	newField6.id = "gcNote_" + r;
	newField6.name = newField6.id;
	newField6.className = "gift";
	newWrap6.appendChild(newField6);
		
	newWrapper.appendChild(newWrap1);
	newWrapper.appendChild(newWrap2);
	newWrapper.appendChild(newWrap3);
	newWrapper.appendChild(newWrap4);
	newWrapper.appendChild(newWrap5);
	newWrapper.appendChild(newWrap6);
	var myRecipients = document.getElementById('recipients');
	myRecipients.appendChild(newWrapper);
}

function gotoEntry(id, qty) {
	var myIn;
	for(i=1; i<=qty; i++) {
		myIn = document.getElementById("recipient_" + i);
		if(myIn.value == "") {
			myIn.focus();
			return;
		}
	}
}
/* END GIFT CARD FUNCTIONS */

/* BEGIN GALLERY FUNCTIONS */
function showPhoto(img, altText) {
	var tmp = document.createElement("img");
	tmp.id = 'galleryPhoto';
	tmp.src = './photos-std/' + img;
	tmp.alt = altText;
	document.getElementById('photoWindow').appendChild(tmp);
	document.getElementById('photo-wrapper').className = 'photoVis';
	document.getElementById('photo-container').className = 'containerVis';
}
function showPhoto2(photoId) {
	currPhoto = photoId;
	var img = eval('photos' + currGallery + '[' + photoId + ']');
	var tmp = document.createElement("img");
	tmp.id = 'galleryPhoto';
	tmp.src = './photos-std/' + img + '.jpg';
	tmp.alt = eval('gallery' + currGallery + '[1]');

	document.getElementById('photoWindow').appendChild(tmp);
	document.getElementById('photo-wrapper').className = 'photoVis';
	document.getElementById('photo-container').className = 'containerVis';
}
function updatePhoto(thumbId) {
	photoId = eval('item' + thumbId);
	currPhoto = photoId;	
	document.getElementById('galleryPhoto').src = './photos-std/' + eval('photos' + currGallery + '[' + photoId + ']') + '.jpg';
}
function hidePhoto() {
	oldImg = document.getElementById('galleryPhoto');
	document.getElementById('photoWindow').removeChild(oldImg);
	document.getElementById('photo-wrapper').className = 'photoNoVis';
	document.getElementById('photo-container').className = 'containerNoVis';
}

function showGallery(galleryId) {
	hideItems();
	// set current gallery
	currGallery = galleryId;
	//  populate gallery text
	var tmpP = document.createElement("p");
	tmpP.id = 'galleryTxt-in';
	//var txt = eval(galleryId + '[1]');
	var tmpTxt = document.createTextNode(eval('gallery' + galleryId + '[1]'));
	tmpP.appendChild(tmpTxt);
	document.getElementById('galleryTxt').appendChild(tmpP);
	// populate gallery thumbs
	gallLen = 5;
	gall = eval('photos' + galleryId);
	if(gall.length < 5) { // less than 5 photos
		gallLen = gall.length;
		document.getElementById('prevItem').style.display = 'none';
		document.getElementById('nextItem').style.display = 'none';
	} else {
		document.getElementById('prevItem').style.display = 'inline';
		document.getElementById('nextItem').style.display = 'inline';
	}
	for(i=0; i<gallLen; i++) {
		updateThumb(i, i);
	}
	// set prev & next Items
	prevItem = gallLen-1;
	nextItem = gallLen+1;
	
	// populate gallery main image on first round
	showPhoto2(0);
}
function hideGallery() {
	oldGal = document.getElementById('galleryPhoto');
	document.getElementById('photoWindow').removeChild(oldGal);
	oldTxt = document.getElementById('galleryTxt-in');
	document.getElementById('galleryTxt').removeChild(oldTxt);
	hideItems();
	document.getElementById('photo-wrapper').className = 'photoNoVis';
	document.getElementById('photo-container').className = 'containerNoVis';
}
function hideItems() {
	document.getElementById('item0').className = 'std-off';
	document.getElementById('item1').className = 'std-off';
	document.getElementById('item2').className = 'std-off';
	document.getElementById('item3').className = 'std-off';
	document.getElementById('item4').className = 'std-off';
}
function prevThumb() {
	// check length of array
	pArr = eval('photos' + currGallery);
	last = (pArr.length - 1);
	photoId = prevItem;
	for(thumbId=0; thumbId<5; thumbId++) {
		photoId++;
		if(photoId > last)
			photoId = 0;
		updateThumb(thumbId, photoId);
		if(thumbId == 0) {
			item0 = photoId;
		} else if(thumbId == 1) {
			item1 = photoId;
		} else if(thumbId == 2) {
			item2 = photoId;
		} else if(thumbId == 3) {
			item3 = photoId;
		} else if(thumbId == 4) {
			item4 = photoId;
		}
	}
	prevItem = photoId;
	nextItem--;
	if(nextItem < 0)
		nextItem = last;
}
function nextThumb() {
	// check length of array
	pArr = eval('photos' + currGallery);
	last = (pArr.length - 1);
	photoId = nextItem;
	
	for(thumbId=4; thumbId>-1; thumbId--) {
		photoId--;
		if(photoId < 0)
			photoId = last;
		updateThumb(thumbId, photoId);
		if(thumbId == 0) {
			item0 = photoId;
		} else if(thumbId == 1) {
			item1 = photoId;
		} else if(thumbId == 2) {
			item2 = photoId;
		} else if(thumbId == 3) {
			item3 = photoId;
		} else if(thumbId == 4) {
			item4 = photoId;
		}
	}
	nextItem = photoId;
	prevItem++;
	if(prevItem > last)
		prevItem = 0;
}
function updateThumb(thumbId, photoId) {
	document.getElementById('photo' + thumbId).src = './photos-th/' + eval('photos' + currGallery + '[' + photoId + ']') + '.jpg';
	document.getElementById('item' + thumbId).className = 'std';
}
/* END GALLERY FUNCTIONS */



window.onload = function () {
	loadNav('nav');
}
