

// Do not mess in here

// Function to enable/disable a particular tour when the check box is clicked

function toggleTour(obj)
{
	// Which check box?
	var objNum =obj.value;
//alert(objNum)
	// Which list box?
	var lstBox=document.getElementById("lngPersons"+objNum);
	
	// Which date box
	var dateBox = document.getElementById("strDateBox"+objNum);
	
	// Which cuurency box?
	var amtBox = document.getElementById("curAmt"+objNum);
	
	// Which calendar link?
	var calIcon = document.getElementById("callink"+objNum);
	
	var idSelected=document.frmData.strIdList.value;
	var tourSelected=document.frmData.strTourList.value;
	var tName=tourCode[objNum]+" "+tourName[objNum];
	if(obj.checked==true)
	{
		var intPos=0;
		intPos=tourSelected.indexOf(tName);
		if(intPos==-1)
		{
			idSelected=idSelected+","+objNum;
			tourSelected=tourSelected+"^"+tName;
		}
	}
	else
	{
		var idTxt=","+objNum;
		idSelected=idSelected.replace(idTxt,"");

		var txt="^"+tName;
		tourSelected=tourSelected.replace(txt,"");
	}

	document.frmData.strIdList.value=idSelected;
	document.frmData.strTourList.value=tourSelected;

	// Enable/disable the listbox and date box depending on the check box action
	if (obj.checked)
	{
		lstBox.disabled=false;
		dateBox.disabled=false;
		amtBox.innerHTML="0.00";
		var pers=lstBox.value;
		var XtraPers=pers-2;
		var amt=0;
		amt=(tourBaseNum[objNum]/1+(tourExtraNum[objNum]*XtraPers))/1;
		amtBox.innerHTML=amt;

		document.frmData.strSubTotList.value=document.frmData.strSubTotList.value+"^|"+objNum+"|"+amt;
//alert("sub tot = "+document.frmData.strSubTotList.value);
	  
		calIcon.innerHTML="<a href='javascript:showCal("+objNum+")' title=' Click to select date '><img name='myPic"+objNum+"' id='myPic"+objNum+"' border='0' src='images/calicon.gif' align='top' hspace='4'/></a>";
		moneyDisplay();
	}
	else
	{
		lstBox.disabled=true;
		dateBox.disabled=true;
		amtBox.innerHTML="0";

		var subTotLst=document.frmData.strSubTotList.value;
		var subTxt="^|"+objNum+"|";
//alert(subTxt)
		var intPos=subTotLst.indexOf(subTxt);
		var newSubTxt="";
		
		newSubTxt=subTotLst.substr(intPos,subTotLst.length);
		
//alert("1st = "+newSubTxt);

		var intCommaPos=newSubTxt.indexOf("^");
//alert(intCommaPos)

		if(intCommaPos==0)
		{
			newSubTxt=newSubTxt.substr(1,newSubTxt.length);
			intCommaPos=newSubTxt.indexOf("^");
			if(intCommaPos==-1)
			{
				newSubTxt=newSubTxt.substr(0,newSubTxt.length);
			}
			else
			{
				newSubTxt=newSubTxt.substr(0,intCommaPos);
			}
			newSubTxt="^"+newSubTxt;
		}
		else
		{
			newSubTxt=newSubTxt.substr(intPos,newSubTxt.indexOf("^")-1);
		}

//alert("new sub tot = "+newSubTxt)
		document.frmData.strSubTotList.value=subTotLst.replace(newSubTxt,"");
		
//alert("final= "+document.frmData.strSubTotList.value)

		calIcon.innerHTML="<img name='myPic"+objNum+"' id='myPic"+objNum+"' border='0' src='images/calicongray.gif' align='top' hspace='4'/>";
		moneyDisplay();
	}
}

function calcTour(lstObj)
{
	//extract the array number
	var str=lstObj.id;
	whichNum=str.replace(/lngPersons/,"");

	// Which list box?
	//we know as it is the lstObj
	var lstBox=lstObj

	// Which date box
	var dateBox = document.getElementById("strDateBox"+whichNum);

	// Which cuurency box?
	var amtBox = document.getElementById("curAmt"+whichNum);
	
	var pers=lstBox.value;
	var XtraPers=pers-2;
	var amt=0;
	amt=(tourBaseNum[whichNum]/1+(tourExtraNum[whichNum]*XtraPers))/1;
	amtBox.innerHTML=amt;

	var subTotLst=document.frmData.strSubTotList.value;
	var subTxt="^|"+whichNum+"|";
	var intPos=subTotLst.indexOf(subTxt);
	var newSubTxt="";
	var repTxt="";
//alert(subTotLst+"\npos="+intPos+"\n"+subTxt);
	
	newSubTxt=subTotLst.substr(intPos,subTotLst.length);
	
//alert("1st = "+newSubTxt);

	var intCommaPos=newSubTxt.indexOf("^");
//alert(intCommaPos)

	if(intCommaPos==0)
	{
		newSubTxt=newSubTxt.substr(1,newSubTxt.length);
		intCommaPos=newSubTxt.indexOf("^");
		if(intCommaPos==-1)
		{
			newSubTxt=newSubTxt.substr(0,newSubTxt.length);
		}
		else
		{
			newSubTxt=newSubTxt.substr(0,intCommaPos);
		}
		newSubTxt="^"+newSubTxt;
	}
	else
	{
		newSubTxt=newSubTxt.substr(intPos,newSubTxt.indexOf("^")-1);
	}

//alert("new sub tot = "+newSubTxt)
	repTxt="^|"+whichNum+"|"+amt;
//alert("replace with = "+repTxt)
	document.frmData.strSubTotList.value=subTotLst.replace(newSubTxt,repTxt);

//alert("final= "+document.frmData.strSubTotList.value)

	moneyDisplay();
}

function moneyDisplay()
{
	var curTot=0;
	for (a=0;a<tourName.length;a++)
	{
		// Which cuurency box?
		var amtBox = document.getElementById("curAmt"+a);
		amt=amtBox.innerHTML.replace(/,/,"");
		curTot=curTot+amt/1;
		amtBox.innerHTML= FormatNumber(amt,2,true,false,true);
	}
	var totBox=document.getElementById("curTotal");
	curTot=curTot/1-(curTot*bookingPercentage/100)
	totBox.innerHTML=FormatNumber(curTot,2,true,false,true);
	document.frmData.strTotal.value=totBox.innerHTML;
}


function FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
	
numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.
 
	RETVAL:
		The formatted number!
 **********************************************************************/
{ 
	if (isNaN(parseInt(num))) return "NaN";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign
	
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" +
tmpNumStr.substring(2,tmpNumStr.length);
		
	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," +
tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	var intPos=0;
	var intDec=0;
	var intLoop=0;
	var intNum=0;
	intPos=tmpNumStr.indexOf(".");
	if(intPos!=-1)
	{
		intDec=tmpNumStr.substr(intPos+1,tmpNumStr.length).length;
		if(intDec!=decimalNum)
		{
			intLoop=decimalNum-intDec;
			while (intNum<intLoop)
			{
				tmpNumStr=tmpNumStr+"0";
				intNum=intNum+1;
			}
		}
	}
	
	return tmpNumStr;		// Return our formatted string!
}

function ignoreIt(obj)
{
	obj.blur();
}

function checkValidDate(obj)
{
	if(obj.value!=" Click icon to select =>")
	{
		var millisecond=1;
		var second=millisecond*1000;
		var minute=second*60;
		var hour=minute*60;
		var day=hour*24;
		var year=day*365;
		var dateObj;
		
		dateObj=Date.parse(Date())
		var newDate;
		var dVal=dateObj.valueOf();
		
		newDate=new Date(dVal+day*1);
		
		if(Date.parse(obj.value)<Date.parse(newDate))
		{
			alert("A minimum of 2 days is required to make a booking");
			obj.value=" Click icon to select =>";
			return false;
		}
	}
}

function checkEmailValid(emailStr,msg)
{
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var errMsg="";   
	var matchArray=emailStr.match(emailPat)

	if (matchArray==null) 
	{
	   errMsg=errMsg+msg+" email address seems incorrect";
	   return errMsg;
	}
	else
	{
	  var user=matchArray[1]
	  var domain=matchArray[2]
	  if (user.match(userPat)==null) 
	  {
		errMsg=errMsg+msg+" email address seems incorrect";
		return errMsg;       
	  }
	  var IPArray=domain.match(ipDomainPat)
	  if (IPArray!=null) 
	  {
		  for (var i=1;i<=4;i++) 
			  {
			if (IPArray[i]>255) 
				{
				errMsg=errMsg+msg+" email address seems incorrect";
				return errMsg;
				}//IF
			  }//for
	   }//IP Array
	   var domainArray=domain.match(domainPat)
	   if (domainArray==null) 
	   {
		 errMsg=errMsg+msg+" email address seems incorrect";
		 return errMsg;
	   }
	   var atomPat=new RegExp(atom,"g")
	   var domArr=domain.match(atomPat)
	   var len=domArr.length
	   if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) 
	   {
		 errMsg=errMsg+msg+" email address seems incorrect";
		 return errMsg;	
	   } 
	   if (len<2) 
	   {
		 errMsg=errMsg+msg+" email address seems incorrect";
		 return errMsg;	 
	   } 
	 }//End  Match
	return "0";
}

function checkDataEntry(obj)
{
	var mycode
	var count=0;
	if(obj.length!=0)
	{
		for(i=0; i<obj.length; i++)
		{
			mycode = obj.charCodeAt(i);
			if((mycode>47 && mycode<58) || (mycode>64 && mycode<91) || (mycode>96 && mycode<123) || (mycode>3584 && mycode<3631) || (mycode>3647 && mycode<3653))
			{
				count=count+1;
			}
		}
	}
	return count;
}

function checkBookingForm()
{
	if(checkDataEntry(document.frmData.strContactName.value)==0)
	{
		alert("Please enter a contact name");
		return false;
	}
	var e=document.frmData.strEmail.value;
	if(checkDataEntry(e)==0)
	{
		alert("Please enter a valid email address");
		return false;
	}
	else
	{
		if (checkEmailValid(e)!="0") 
		{
			alert(checkEmailValid(e,'The'));
			return false;	
		}
	}
	
	var i=0;
	var ctrl=document.frmData.tourName;
	var id=0;
	var xName="";
	var tSel=0;
	for(i=0; i<tourName.length; i++)
	{
		xName="chkTour"+i
		if(document.getElementById(xName).checked==true)
		{
			tSel=i;
			id=xName.value;
		}
	}
	if(id==0)
	{
		alert("Please select at least one tour");
		return false;
	}
	
	xName="strDateBox"+tSel;
	if(document.getElementById(xName).value==" Click icon to select =>")
	{
		alert("The requested tour date is missing for "+tourCode[tSel]+" "+tourName[tSel]);
		return false;
	}

	if(document.frmData.optPayment[0].checked==false && document.frmData.optPayment[1].checked==false)
	{
		alert("Please select a payment method");
		return false;
	}
	
	if((checkDataEntry(document.frmData.strHotelName.value)==0) && (checkDataEntry(document.frmData.strAddress.value)==0))
	{
		alert("Please enter the pickup details");
		return false;
	}
	return true;
}