//--------------------------//
//Functions for Wedding List//
//--------------------------//

strListWindowParams = "scrollbars=yes,resizable=yes,status=yes"


function checkBAGDetails()
{
	
//Check Bride's Name Present
	if (isCompleted(document.frmBAGDetails.BrideFN.value) == false)
	{
		alert("Please complete the Bride's First Name")
		document.frmBAGDetails.BrideFN.focus()
		return false
	}
	if (isCompleted(document.frmBAGDetails.BrideSN.value) == false)
	{
		alert("Please complete the Bride's Surname")
		document.frmBAGDetails.BrideSN.focus()
		return false
	}
 
//Check Groom's Name Present
	if (isCompleted(document.frmBAGDetails.GroomFN.value) == false)
	{
		alert("Please complete the Groom's First Name")
		document.frmBAGDetails.GroomFN.focus()
		return false
	}
	if (isCompleted(document.frmBAGDetails.GroomSN.value) == false)
	{
		alert("Please complete the Groom's Surname")
		document.frmBAGDetails.GroomSN.focus()
		return false
	}
	
//Format Names
	document.frmBAGDetails.BrideFN.value = capitalise(document.frmBAGDetails.BrideFN.value)
	document.frmBAGDetails.BrideSN.value = capitalise(document.frmBAGDetails.BrideSN.value)
	document.frmBAGDetails.GroomFN.value = capitalise(document.frmBAGDetails.GroomFN.value)
	document.frmBAGDetails.GroomSN.value = capitalise(document.frmBAGDetails.GroomSN.value)
	
//Check Phone Number Present
	if (isCompleted(document.frmBAGDetails.phone.value) == false)
	{
		alert("Please enter a contact telephone number")
		document.frmBAGDetails.phone.focus()
		return false
	}

//E-Mail Address	
	if (isCompleted(document.frmBAGDetails.email.value) == false)
	{
		alert("Please complete your E-Mail Address")
		document.frmBAGDetails.email.focus()
		return false
	}
	if (document.frmBAGDetails.email.value.indexOf("@") == -1 | document.frmBAGDetails.email.value.indexOf(".") == -1 | document.frmBAGDetails.email.value.indexOf(" ") > -1)
	{
		alert("Please enter a VALID E-Mail Address without any spaces (eg nnnn@dddd.co.uk)")
		document.frmBAGDetails.email.focus()
		return false	
	}
	
//Check Date of Wedding is Present & is Valid
	if (isCompleted(document.frmBAGDetails.WedDate.value) == false)
	{
		alert("Please enter the date of your wedding in the format ddmmyyyy")
		document.frmBAGDetails.WedDate.focus()
		return false
	}
	if (isValidDate(document.frmBAGDetails.WedDate.value)==false)
	{
		alert("The date you have entered for your Wedding Date is not valid, please re-enter")
		document.frmBAGDetails.WedDate.focus()
		return false;
	}



//Check Delivery Date is Present & is Valid
	if (isCompleted(document.frmBAGDetails.DeliverOn.value) == false)
	{
		alert("Please enter the date that you would like your gifts to be delivered in the format ddmmyyyy")
		document.frmBAGDetails.DeliverOn.focus()
		return false
	}
	if (isValidDate(document.frmBAGDetails.DeliverOn.value)==false)
	{
		alert("The date you have entered for your Delivery Date is not valid, please re-enter")
		document.frmBAGDetails.DeliverOn.focus()
		return false;
	}

//Check Delivery Address Present
	if (isCompleted(document.frmBAGDetails.DeliverTo.value) == false)
	{
		alert("Please enter the address that you would like your gifts to be delivered to")
		document.frmBAGDetails.DeliverTo.focus()
		return false
	}
	
//Check Delivery Address Present
	if (isCompleted(document.frmBAGDetails.pWord.value) == false)
	{
		alert("Please enter a password that you can use to view your Wedding List or to amend these details")
		document.frmBAGDetails.pWord.focus()
		return false
	}
	if (isCompleted(document.frmBAGDetails.pWord2.value) == false)
	{
		alert("Please re-enter your password for confirmation")
		document.frmBAGDetails.pWord2.focus()
		return false
	}
	if (document.frmBAGDetails.pWord.value != document.frmBAGDetails.pWord2.value)
	{
		alert("Your password and the confirmation of your password do not match.  Please re-enter these.")
		document.frmBAGDetails.pWord.value=""
		document.frmBAGDetails.pWord2.value=""
		document.frmBAGDetails.pWord.focus()
		return false
	}

//Check Personal Message Completed
	if (document.frmBAGDetails.cboMessage.value == 1 & isCompleted(document.frmBAGDetails.txtMessage.value) == false)
	{
		alert("Please enter your own personal message (max 50 characters) or select an option from the list")
		document.frmBAGDetails.cboMessage.focus()
		return false
	}
	
//Check Number of Inserts Completed
	if (isCompleted(document.frmBAGDetails.Inserts.value) == false)
	{
		alert("Please enter the number of Wedding Invitation Inserts you require (or enter 0)")
		document.frmBAGDetails.Inserts.focus()
		return false
	}
	
//Check Number of Inserts is Numeric
	if (isNumeric(document.frmBAGDetails.Inserts.value) == false)
	{
		alert("Please enter the NUMBER of Wedding Invitation Inserts you require (or enter 0)")
		document.frmBAGDetails.Inserts.focus()
		return false
	}
	
	
//Complete Wedding List ID
	if (isCompleted(document.frmBAGDetails.WID.value) == false)
		{
		document.frmBAGDetails.WID.value = createWedListID()
		}

	return true;
}


function cboMessage_Change()
{
	if (document.frmBAGDetails.cboMessage.value == 0)
	{
		document.frmBAGDetails.txtMessage.value =""
	}
	else if (document.frmBAGDetails.cboMessage.value == 2)
	{
		document.frmBAGDetails.txtMessage.value ="Thank you for purchasing a gift from Designheaven"
	}
	else if (document.frmBAGDetails.cboMessage.value == 3)
	{
		document.frmBAGDetails.txtMessage.value ="We look forward to seeing you on our big day"
	}
	if (document.frmBAGDetails.cboMessage.value != 0)
	{
		document.frmBAGDetails.txtMessage.focus()
	}
}

function txtMessage_Change()
{
			document.frmBAGDetails.cboMessage.value =1	
}

function isCompleted(strString)
{
	if ((strString == "") || (strString == null))
	{
		return false;
	}
	else
	{
		return true;
	}
}

function isValidDate(strDate)
{
	if (isCompleted(strDate) == false)
		{
		return false;
		}
	
	if (strDate.length != 8)
		{
		//alert("wrong length")
		return false;
		}
	if (isNumeric(strDate) ==false)
		{
		//alert("not numeric")
		return false;
		}
	
	if(strDate.substring(2,4)>12)
		{
		//alert("Invalid Month")
		return false
		}

	var arrMonths = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
	var dteToday = new Date()
	
	intMonth = eval(strDate.substring(2,4))-1
	strMonth = arrMonths[intMonth]
	var dteDate = new Date(strDate.substring(0,2) + " " + strMonth + " " + strDate.substring(4,8))

	if(intMonth != dteDate.getMonth(dteDate))
		{
		//Date has been reformatted to accommodate data eg 31022003
		//alert("Invalid Date")
		return false
		}
	if (dteDate < dteToday)
		{
		//alert("Date in past")
		return false
		}
}


function isNumeric(strString)
{
   //  check for valid numeric strings
	var strValidChars = "0123456789.";
	var strChar;
	var blnResult1 = true;   
   
   if ((strString == "") || (strString == null))
   {   
		blnResult1 = false;
		return blnResult1;
	}	
   else
   {	
	for (i = 0; i < strString.length && blnResult1 == true; i++)
	   {
	   strChar = strString.charAt(i);
	   if (strValidChars.indexOf(strChar) == -1)
	      {
	      blnResult1 = false;
	      }
	   }
	return blnResult1;
   }
}

function createWedListID()
{
	strWedDate= document.frmBAGDetails.WedDate.value
	strGInits = document.frmBAGDetails.GroomFN.value.substring(0,1) + document.frmBAGDetails.GroomSN.value.substring(0,1)
	strBInits = document.frmBAGDetails.BrideFN.value.substring(0,1) + document.frmBAGDetails.BrideSN.value.substring(0,1)
	
	strWLID = strWedDate + strGInits + strBInits
	return strWLID
}

function imgSwap(btnName,imgTo,strStatusText)
{
	eval("document." + btnName).src = imgTo;
	if (arguments.length==3)
		{window.status=strStatusText};
}

function showWedList(strWID,strMode,intWedListStatus)
{
	if (intWedListStatus=="1") {
		alert("As your Wedding List has been closed this option is no longer available.\n\nTo view your Gift List, please use 'Manage your Gift List'  from within the Bride and Groom options in the Wedding List Registry.");
		return;
	}
	newWin=window.open("weddinglist/giftlist.php?WID=" + strWID + "&mode=" + strMode, "shop" , strListWindowParams)
	newWin.moveTo(50,50)
	newWin.focus()	
}


function addToWedList(strWID,strPID,strQuantityReqd,strPrice,strDescription,strStockLevel,strMode,intWedListStatus)
{
	if (intWedListStatus=="1") {
		alert("As your Wedding List has been closed this option is no longer available.");
		return;
	}
	var strOpenParams = "weddinglist/giftlist.php?opt=add&WID=" + strWID + "&PID=" + strPID + "&PDesc=" + strDescription + "&Q=" + strQuantityReqd + "&P=" +  strPrice  + "&mode=" + strMode
	newWin = window.open(strOpenParams,"shop",strListWindowParams)
	newWin.moveTo(50,50)
	newWin.focus()
}

function capitalise(strText)
{
	if (isCompleted(strText) == false)
		{return}

	strText = strText.toLowerCase()
	strText = strText.substring(0,1).toUpperCase() + strText.substring(1)
	return strText	
}