// JavaScript Document

// checks if the browser used is IE.
// @return 	TRUE if the browser is IE,
//			FALSE otherwise.
function isIE()
{
	if (document.all)
	{
		return true;
	}
	
	return false;
}

function isNE()
{
	if (document.layers) 
	{
		return true;
	}
	
	return false;
}


// Hides the 'Order' Div and shows the Check Mark and Cancel Link for a Photo to be ordered
// Also appends the picture's ID onto the hidden text field for storage.
function toggleOrder(strID)
{
	var strOrderID = 'order_' + strID;
	var strCancelID = 'cancel_' + strID;
	var strCheckID = 'check_' + strID;
	var strBlockID = 'block_' + strID;
	
	
	//if(isNE())
	//{
		//hide the Order div block;
		document. getElementById(strOrderID).style.display = 'none';
		
		//show the Cancel and Check Div Block
		document. getElementById(strCheckID).style.display = 'block';
		document. getElementById(strCancelID).style.display = 'block';
			
		//change the class style
		document. getElementById(strBlockID).className = 'siteDivResultBlockOrdered';

		
		// ex. appends [0] as ordered image ID. results to [2][0], meaning user ordered imgIDs 2, 0
		//strOldValue = document.frmShoppingCart.cartString.value;
		//strOldValue = window.top.name;
		//document.frmShoppingCart.cartString.value = strOldValue + '[' + strID + ']';
		//window.top.name = strOldValue + '[' + strID + ']';
		window.top.name = appendToCartInfo(strID);
		//alert(document.frmShoppingCart.cartString.value);		
	//}
}


// hides the 'Cancel' Div and Check Div and shows the Order Link for a Photo to be cancelled
// Also removes the ID of the image from the hidden text field to cancel the order.
function toggleCancel(strID)
{
	var strOrderID = 'order_' + strID;
	var strCancelID = 'cancel_' + strID;
	var strCheckID = 'check_' + strID;
	var strBlockID = 'block_' + strID;
	
	//if(isNE())
	//{
		//show the Order div block;
		document. getElementById(strOrderID).style.display = 'block';
		
		//hide the Cancel and Check Div Block
		document. getElementById(strCheckID).style.display = 'none';
		document. getElementById(strCancelID).style.display = 'none';
		
		//change the class style
		document. getElementById(strBlockID).className = 'siteDivResultBlock';
		
		// ex. removes [0] as ordered image ID. results to [2] from [2][0], meaning user canceled order for imgID 0
		//strOldValue = document.frmShoppingCart.cartString.value;
		//strOldValue = window.top.name;
		strOldValue = getCartSubstring();
		strToBeRemoved = new RegExp('\\['+strID+'\\]', 'gi');

		//document.frmShoppingCart.cartString.value = strOldValue.replace(strToBeRemoved, '');
		//window.top.name = strOldValue.replace(strToBeRemoved, "");
		window.top.name = setCartInfo(strOldValue.replace(strToBeRemoved, ""));
		//alert(document.frmShoppingCart.cartString.value);
	//}
}


// calls the submit function of the form to pass the user's selection of images
function submitCart()
{
	//strOldValue = document.frmShoppingCart.cartString.value;
	strOldValue = getCartSubstring();//window.top.name;
	strToBeRemoved = '\\';
	//document.frmShoppingCart.cartString.value = strOldValue.replace(strToBeRemoved, '');
	//window.top.name = strOldValue.replace(strToBeRemoved, '');
	window.top.name = setCartInfo(strOldValue.replace(strToBeRemoved, ''));
	document.frmShoppingCart.cartString.value = getCartSubstring();//window.top.name;
	document.frmShoppingCart.submit();
}

// submits the selected photos information from the search results to the gallery
function submitToGallery()
{
	//strOldValue = document.frmShoppingCart.cartString.value;
	strOldValue = getCartSubstring();//window.top.name;
	strToBeRemoved = '\\';
	document.frmShoppingCart.cartString.value = strOldValue.replace(strToBeRemoved, '');
	
	document.frmShoppingCart.action = './imageGallery.php';
	document.frmShoppingCart.submit();
}

// reloads the imageGallery page passing the contents of the selected photos from the current page to the next
function loadNewGalleryPage(strPageVars)
{
	//strOldValue = document.frmShoppingCart.cartString.value;
	strOldValue = getCartSubstring();//window.top.name;
	strToBeRemoved = '\\';
	document.frmShoppingCart.cartString.value = strOldValue.replace(strToBeRemoved, '');
	
	document.frmShoppingCart.action = './imageGallery.php?page=' + strPageVars;
	document.frmShoppingCart.submit();
}


function updateTotalOrderCost(fValue)
{
	var fCurrentAmount = parseFloat(document.getElementById('lblTotalOrderCost').innerHTML);
	
	if(isNaN(fCurrentAmount) || fCurrentAmount < 0)
	{
		fCurrentAmount = 0;
	}
	
	fCurrentAmount = fCurrentAmount + fValue;
	
	if(fCurrentAmount < 0)
	{
		fCurrentAmount = 0;
	}
	
	
	fCurrentAmount = fCurrentAmount.toFixed(2);
	document.getElementById('lblTotalOrderCost').innerHTML = '' + fCurrentAmount;
}


function updateCostLabel(nPicID)
{
	var iCtr = 0;
	var fTotalPicAmount = 0;
	var lblID = 'lblCost_' + nPicID;
	var fCost = new Array();
		fCost[0] = 95.00;
		//fCost[1] = 399.00;
		fCost[1] = 95.00;//fCost[2] = 95.00;
	
	
	//store original cost;
	var fOrigAmount = parseFloat(document.getElementById(lblID).innerHTML);
	
	for(iCtr = 1; iCtr <= 2; iCtr = iCtr + 1)//for(iCtr = 1; iCtr <= 3; iCtr = iCtr + 1)
	{	
		var strTypeSuffix = '' + iCtr + '_' + nPicID;
		var strIDName = 'txtQuantity' + strTypeSuffix;

		var fQuantity = parseFloat(document.shoppingCart[strIDName].value);
		if(isNaN(fQuantity) || fQuantity < 0)
		{
			fQuantity = 0;	
			document.shoppingCart[strIDName].value = '';
		}
		
		var fTotalCost = fQuantity * fCost[iCtr - 1];
	
		// add to total Pic Order Cost
		fTotalPicAmount = fTotalPicAmount + fTotalCost;
		
	}
	
	fTotalPicAmount = fTotalPicAmount.toFixed(2);
	document.getElementById(lblID).innerHTML = '' + fTotalPicAmount;
	
	updateTotalOrderCost(fTotalPicAmount - fOrigAmount);
}


function updateOrderType(nType, nPicID, fCost)
{
	strTypeSuffix = '' + nType + '_' + nPicID;
	strIDName = 'chkbox' + strTypeSuffix;
	//lblID = 'lblCost_' + nPicID;
//	
//	fTotalPicAmount = parseFloat(document.getElementById(lblID).innerHTML);
//	
//	
//	if(document.shoppingCart[strIDName].checked)
//	{
//		// get quantity, calculate cost;
//		strIDName = 'txtQuantity' + strTypeSuffix;
//
//		fQuantity = parseFloat(document.shoppingCart[strIDName].value);
//		if(isNaN(fQuantity))
//		{
//			fQuantity = 0;	
//		}
//		
//		fTotalCost = fQuantity * fCost;
//					
//		// add to total Pic Order Cost
//		fTotalPicAmount = fTotalPicAmount + fTotalCost;
//
//		//update the label
//		fTotalPicAmount = fTotalPicAmount.toFixed(2);
//
//		document.getElementById(lblID).innerHTML = '' + fTotalPicAmount;
//	}
//	else
//	{
//		// get quantity, calculate cost;
//		strIDName = 'txtQuantity' + strTypeSuffix;
//
//		fQuantity = parseFloat(document.shoppingCart[strIDName].value);
//		if(isNaN(fQuantity))
//		{
//			fQuantity = 0;	
//		}
//		
//		fTotalCost = fQuantity * fCost;
//		
//		// subtract from total Pic Order Cost
//		fTotalPicAmount = fTotalPicAmount - fTotalCost;
//		if(fTotalPicAmount < 0)
//			fTotalPicAmount = 0;
//			
//		//update the label
//		fTotalPicAmount = fTotalPicAmount.toFixed(2);
//		document.getElementById(lblID).innerHTML = '' + fTotalPicAmount;
//		
//		document.shoppingCart[strIDName].value = '';
//	}
	
	if(document.shoppingCart[strIDName].checked == false)
	{
		// get quantity, calculate cost;
		strIDName = 'txtQuantity' + strTypeSuffix;
		document.shoppingCart[strIDName].value = '';
	}
	else
	{
		// get quantity, calculate cost;
		strIDName = 'txtQuantity' + strTypeSuffix;
		if(!isNaN(document.shoppingCart[strIDName].value)) {
			if(parseInt(document.shoppingCart[strIDName].value) > 0)
				document.shoppingCart[strIDName].value = parseInt(document.shoppingCart[strIDName].value);
			else
				document.shoppingCart[strIDName].value = 1;
				
		}
		else
				document.shoppingCart[strIDName].value = 1;		
	}
	
	updateCostLabel(nPicID);
}


function setInitValue(nType, nPicID, fCost)
{
	strTypeSuffix = '' + nType + '_' + nPicID;
	strIDName = 'chkbox' + strTypeSuffix;
	lblID = 'lblCost_' + nPicID;
	
	if(document.shoppingCart[strIDName].checked == false)
	{
		// get quantity, calculate cost;
		strIDName = 'txtQuantity' + strTypeSuffix;
		document.shoppingCart[strIDName].value = '';
		updateOrderType(nType, nPicID, fCost);
	}
	else
	{
		// get quantity, calculate cost;
		strIDName = 'txtQuantity' + strTypeSuffix;
		document.shoppingCart[strIDName].value = '1';
		
		var fCost = new Array();
			fCost[0] = 95.00;
			//fCost[1] = 399.00;
			fCost[2] = 95.00;//fCost[2] = 99.00;
		
		//get original amount
		
		//add pic cost
		document.getElementById(lblID).innerHTML = '' + fCost[nType -1];
		updateTotalOrderCost(fCost[nType -1]);
	}
}


function setCheckboxSelected(nType, nPicID)
{
	strTypeSuffix = '' + nType + '_' + nPicID;
	strIDName = 'chkbox' + strTypeSuffix;
	
	document.shoppingCart[strIDName].checked = true;
}

// validate the contents of the shopping cart.
// check if the user selected an order type by checking the total cost of the selected orders.
function validateShoppingCart()
{
	var fCurrentAmount = parseFloat(document.getElementById('lblTotalOrderCost').innerHTML);
	
	if(isNaN(fCurrentAmount) || fCurrentAmount <= 0)
	{
		alert ('Please place your orders by specifying the quantity and type of photo.');
		return false;
	}
	
	if(validateCartQuantity() == false) {
		alert ('Your cart should at least have 3 items.');
		return false;	
	}
	
	
	return true;
}


// validate the contact information submitted.
function validateContactInfo()
{
	var strTemp = '';
	var bShowAlert = false;
	
	//check if user had entered values for FName and LName
	strTemp = document.frmContactDetails.contactFName.value;
	if(strTemp.length <= 0 && bShowAlert == false)
	{
		document.frmContactDetails.contactFName.focus();
		bShowAlert = true;
	}
	strTemp = document.frmContactDetails.contactLName.value;
	if(strTemp.length <= 0 && bShowAlert == false)
	{
		document.frmContactDetails.contactLName.focus();
		bShowAlert = true;
	}
	
	//check contact number
	strTemp = document.frmContactDetails.contactNum.value;
	if(strTemp.length <= 0 && bShowAlert == false)
	{
		document.frmContactDetails.contactNum.focus();
		bShowAlert = true;
	}
	
	//check email
	strTemp = document.frmContactDetails.contactEmail.value;
	if(strTemp.length <= 0 && bShowAlert == false)
	{
		document.frmContactDetails.contactEmail.focus();
		bShowAlert = true;
	}
	
	//check address
	strTemp = document.frmContactDetails.contactAddress.value;
	if(strTemp.length <= 0 && bShowAlert == false)
	{
		document.frmContactDetails.contactAddress.focus();
		bShowAlert = true;
	}
	
	
	//check agree
	if(document.frmContactDetails.agree.checked == false && bShowAlert == false) {
		document.frmContactDetails.agree.focus();
		bShowAlert = true;
	}
	
	//show alert if one field is missed
	if(bShowAlert)
	{
		alert ('Please completely fill out the form and agree to the terms provided.');
		return false;
	}
	
	return true;
}


//checks the selected payment method and proceeds to the correct confirmation page
function proceedToConfirmation()
{
	if(validateContactInfo() == false)
	{
		return;	
	}
	
	var nIndex = document.frmContactDetails.contactPaymentMethod.selectedIndex;
	
	if(nIndex == 0)
	{
		//bank Deposit
		//document.frmContactDetails.action = './bankOrderConfirmation.php';
		document.frmContactDetails.action = './addOrderEntry.php';
		document.frmContactDetails.submit();
	}
	else if(nIndex == 1)
	{
		//Paypal
		//document.frmContactDetails.action = './paypalOrderConfirmation.php';
		document.frmContactDetails.action = './addOrderEntry.php';
		document.frmContactDetails.submit();
	}
}


function proceedToBackToCart()
{
	document.frmContactDetails.action = './shoppingCart.php';
	document.frmContactDetails.submit();
}


// validate the email form on Contact us page.
function validateClientMessage()
{
	var strTemp = '';
	var bShowAlert = false;

	//check if user had entered values for Name
	strTemp = document.frmClientMessage.emailSender.value;
	if(strTemp.length <= 0 && bShowAlert == false)
	{
		document.frmClientMessage.emailSender.focus();
		bShowAlert = true;
	}
	
	//check email
	strTemp = document.frmClientMessage.emailSenderEmail.value;
	if(strTemp.length <= 0 && bShowAlert == false)
	{
		document.frmClientMessage.emailSenderEmail.focus();
		bShowAlert = true;
	}
	
	//check subject
	strTemp = document.frmClientMessage.emailSubject.value;
	if(strTemp.length <= 0 && bShowAlert == false)
	{
		document.frmClientMessage.emailSubject.focus();
		bShowAlert = true;
	}
	
	//check message
	strTemp = document.frmClientMessage.emailMessage.value;
	if(strTemp.length <= 0 && bShowAlert == false)
	{
		document.frmClientMessage.emailMessage.focus();
		bShowAlert = true;
	}
	
	//show alert if one field is missed
	if(bShowAlert)
	{
		alert ('Please fill out all the fields.');
		return false;
	}
	
	return true;
}

function checkObjectById(theVal)
{
	if(document.getElementById(theVal) != null)
		return true;
	else
		return false;
}

//toggle all ordered pictures in page
function toggleOrderedPictures() {
		var strImages = getCartSubstring();
		var strToBeRemoved = new RegExp('\\]\\[', 'gi');
		strImages = strImages.replace(strToBeRemoved, ",");
		strToBeRemoved = new RegExp('\\]', 'gi');
		strImages = strImages.replace(strToBeRemoved, "");
		strToBeRemoved = new RegExp('\\[', 'gi');
		strImages = strImages.replace(strToBeRemoved, "");
		var arrImageID = strImages.split(",");
		
		var i;
		
		
		for(i = 0; i < arrImageID.length; i++) {
		//alert(arrImageID[i]);
			if(arrImageID[i] != "") {
				var strOrderID = 'order_' + arrImageID[i];
				var strCancelID = 'cancel_' + arrImageID[i];;
				var strCheckID = 'check_' + arrImageID[i];;
				var strBlockID = 'block_' + arrImageID[i];;
			
				//hide the Order div block;
				if(checkObjectById(strOrderID)) document.getElementById(strOrderID).style.display = 'none';
				
				//show the Cancel and Check Div Block
				if(checkObjectById(strCheckID)) document.getElementById(strCheckID).style.display = 'block';
				if(checkObjectById(strCancelID)) document.getElementById(strCancelID).style.display = 'block';
					
				//change the class style
				if(checkObjectById(strBlockID)) document.getElementById(strBlockID).className = 'siteDivResultBlockOrdered';
			}
		}
}

function updateAllCostLabels() {
		var strImages = getCartSubstring();//window.top.name;
		var strToBeRemoved = new RegExp('\\]\\[', 'gi');
		strImages = strImages.replace(strToBeRemoved, ",");
		strToBeRemoved = new RegExp('\\]', 'gi');
		strImages = strImages.replace(strToBeRemoved, "");
		strToBeRemoved = new RegExp('\\[', 'gi');
		strImages = strImages.replace(strToBeRemoved, "");
		var arrImageID = strImages.split(",");
		
		var i;
		
		
		for(i = 0; i < arrImageID.length; i++) {
			updateCostLabel(arrImageID[i]);
		}
}

function setCartInfo(strCart) {
	strNew = "[PV]"+strCart+"[PV]";
	return strNew;
}

function appendToCartInfo(strToAppend) {
	str = window.top.name;
	start = str.indexOf("[PV]") + 4;
	end = str.lastIndexOf("[PV]");
	strNew = "[PV]"+str.substring(start, end)+"["+strToAppend+"][PV]";
	return strNew;
}

function getCartSubstring() {
	str = window.top.name;
	start = str.indexOf("[PV]") + 4;
	end = str.lastIndexOf("[PV]");
	return str.substring(start, end);
}

function splitCartString() {
	var strImages = getCartSubstring();//window.top.name;
	var strToBeRemoved = new RegExp('\\]\\[', 'gi');
	strImages = strImages.replace(strToBeRemoved, ",");
	strToBeRemoved = new RegExp('\\]', 'gi');
	strImages = strImages.replace(strToBeRemoved, "");
	strToBeRemoved = new RegExp('\\[', 'gi');
	strImages = strImages.replace(strToBeRemoved, "");
	var arrImageID = strImages.split(",");

	return arrImageID;
}

function validateCartQuantity() {
	var minItems = 3;
	//var fCost = new Array();
		//fCost[0] = 95.00;
		//fCost[1] = 95.00;
	var arrImage = splitCartString();
	var totalQuantity = 0;
	var i, j;
	var numItemTypes = 2;
	
	for(i = 1; i <= arrImage.length; i++) {
		for(j = 1; j <= numItemTypes; j++) {
			strIdName = 'txtQuantity'+j+'_'+arrImage[i-1];
			var currQty = parseInt(document.shoppingCart[strIdName].value);
			if(isNaN(currQty))
				currQty=0;
			totalQuantity += currQty;
		}
	}
	
	if(totalQuantity >= 3)
		return true;
		
	return false;
}