function bookmarkUs() 
{
	window.external.AddFavorite('http://www.giftday.co.za', 'Giftday.co.za - Experience Gift Vouchers in South Africa');
}

function recommendUs() 
{
	window.open('recommend.php', '', 'resizable=no,scrollbars=no,width=500,height=330');
}

function doGiftDelivery()
{
	window.open('index.php?page=delivery/delivery_details', '', 'resizable=yes,scrollbars=no,width=550,height=800');
}

function validateEnquiryForm()
{
	fs =  new formValidator();
	
	if (fs.isEmpty("your_name"))
		fs.raiseError("Your name must be specified.");
		
	if (fs.isEmpty("email"))
		fs.raiseError("An email address must be specified");
		
  	if (!fs.isEmailAddress("email"))
		fs.raiseError("A valid email address must be specified");
				
	if (fs.isEmpty("subject"))
		fs.raiseError("A subject must be specified");
		
	if (fs.isEmpty("comments"))
		fs.raiseError("Comment(s)/ Question(s) must be specified.");
		
	if (fs.numErrors() > 0)
	{
		fs.displayErrors();
		return false;
	}
	else
	  return true;
}


function validateRecommendForm()
{
	fs = new formValidator();
	
	if (fs.isEmpty("your_name"))
		fs.raiseError("Your name must be specified.");
	if (fs.isEmpty("your_email"))
		fs.raiseError("Your email address must be specified.");
	if (!fs.isEmailAddress("your_email"))
		fs.raiseError("Your email address must be a valid email address.");
	
	if (fs.isEmpty("friend_name"))
		fs.raiseError("Your friend's name must be specified.");
	if (fs.isEmpty("friend_email"))
		fs.raiseError("Your friend's email address must be specified.");
	if (!fs.isEmailAddress("friend_email"))
		fs.raiseError("Your friends email address must be a valid email address.");
	
	if (fs.numErrors() > 0)
	{
		fs.displayErrors();
		return false;
	}
	else
		return true;
}

function validateCorporateForm()
{
	fs =  new formValidator();
	
	if (fs.isEmpty("Product"))
		fs.raiseError("Please enter a product.");
	
	if (fs.isEmpty("company"))
		fs.raiseError("Your company must be specified.");
		
	if (fs.isEmpty("Nopersons"))
		fs.raiseError("Please enter no. persons");
		
	if (fs.isEmpty("date"))
		fs.raiseError("Please enter your preferred date.");		
	
	if (fs.isEmpty("your_name"))
		fs.raiseError("Your name must be specified.");
		
	if (fs.isEmpty("email"))
		fs.raiseError("An email address must be specified");
		
  	if (!fs.isEmailAddress("email"))
		fs.raiseError("A valid email address must be specified");
				
	if (fs.isEmpty("comments"))
		fs.raiseError("Comment(s)/ Question(s) must be specified.");
		
	if (fs.numErrors() > 0)
	{
		fs.displayErrors();
		return false;
	}
	else
	  return true;
}

function validateCCFields()
{
	fs =  new formValidator();
	if (!document.checkout.Ecom_Payment_Card_Name.disabled)
	{
		if (fs.isEmpty("Ecom_Payment_Card_Name"))
			fs.raiseError("You must specify the name on the Credit Card.");
		
		if (fs.isEmpty("Ecom_Payment_Card_Number"))
			fs.raiseError("You must specify the number of the Credit Card.");
		
		if (fs.isEmpty("Ecom_Payment_Card_ExpDate_Month"))
			fs.raiseError("You must specify the Credit Card expiry Month.");
			
		if (fs.isEmpty("Ecom_Payment_Card_ExpDate_Year"))
			fs.raiseError("You must specify the Credit Card expiry Year.");
			
		if (fs.isEmpty("Ecom_Payment_Card_Verification"))
			fs.raiseError("You must specify the CVV number.");
	}
		
	if (fs.numErrors() > 0)
	{
		fs.displayErrors();
		return false;
	}
	else
	  return true;
}

function validateCheckout()
{
	fs =  new formValidator();
	
/*	if (fs.isEmpty("shipto_first_name") || fs.isEmpty("shipto_last_name") || fs.isEmpty("shipto_user_email") || fs.isEmpty("shipto_phone_1") || (fs.isEmpty("shipto_address_1") && fs.isEmpty("shipto_address_2")))
			fs.raiseError("You have entered details into the recipient shipping information area. Please specify the first name, last name, address, telephone and email address of the recipient.");*/
	
	// validate the shipping address if necessary
	if 	((document.Checkout.shipto_company.value != "") || 
			(document.Checkout.shipto_first_name.value != "") ||
			(document.Checkout.shipto_address_1.value != "") || 
			(document.Checkout.shipto_phone_1.value != "") || 
			(document.Checkout.shipto_last_name.value != "") ||
			(document.Checkout.shipto_address_1.value != "") || 
			(document.Checkout.shipto_address_2.value != "") ||
			(document.Checkout.shipto_city.value != "") ||
			(document.Checkout.shipto_zip.value != "") ||
			(document.Checkout.shipto_state.value != "") ||
			(document.Checkout.shipto_phone_1.value != "") ||
			(document.Checkout.shipto_fax.value != "") ||
			(document.Checkout.shipto_user_email.value != ""))
	{
			if (fs.isEmpty("shipto_first_name") || fs.isEmpty("shipto_last_name"))
				fs.raiseError("You have entered details into the recipient shipping information area. Please specify the first name and surname of the recipient.");
	}
	
	function welcome(){
	window.alert ("IMPORTANT! Soonest delivery date: 15th January 2007");
	}
	
	
	// validate the delivery date
	// removed (requested by craig sandeman)

	/*
	var Year = document.Checkout.dYear.value;
	var Month = getMonthNum(document.Checkout.dMonth.value);
	var Day = document.Checkout.dDay.value;
	
  	aVal = Year+Month+Day; //alternate format
  
  	 //MM/DD/YYYY
   	if (formatDate("YYYYMMDD", document.Checkout.deliveryday.value, getMonthNum(document.Checkout.deliverymonth.value), document.Checkout.deliveryyear.value) < aVal) {
  	fs.raiseError("Bookings can only be made for the future.\n - Please check your Future Delivery Dates");
  }
  */
  
	// check how much text has been entered into the personalised message box.
	if (!fs.isEmpty("message"))
	{
		// count the number of characters.
		StrLen = fs.findObj("message").value.length;
		if (StrLen > Number(fs.findObj("maxChars").value))
			fs.raiseError("The message you have specified is too long - only 250 characters in a message is permitted.");
	}
	
	if (document.Checkout.deliveryType[1].checked)
	{
		specifiedMonth = getMonthNum(document.Checkout.deliverymonth.value);
  		specifiedDate = new Date(document.Checkout.deliveryyear.value, specifiedMonth-1, document.Checkout.deliveryday.value);
		if (specifiedDate.getUTCDay() == 6)
			fs.raiseError("Please note that Giftday does not deliver on Sundays. Please select another preferred delivery date.");
	}

	
	if (fs.numErrors() > 0)
	{
		fs.displayErrors();
		return false;
	}
	else
	  return true;
}

function validateCheckoutCCFields()
{
	fs =  new formValidator();
	
	if (fs.findObj("deliveryType").value == "Future Date Delivery")
	{
		if (fs.findObj("dayname").value == "Sunday")
			fs.raiseError("Please note that Giftday does not deliver on Sundays. Please select another preferred delivery date.");
	}
	
	switch (document.frmCheckout.payment_method_id.selectedIndex)
	{
		case 0:
			fs.raiseError("You must select a payment method.");
			break;
		case 1:
		case 2:
		case 3:
		case 4:
			if (fs.isEmpty("Ecom_Payment_Card_Name"))
				fs.raiseError("You must specify the name on the Credit Card.");
		
			if (fs.isEmpty("Ecom_Payment_Card_Number"))
				fs.raiseError("You must specify the number of the Credit Card.");
			
			if (fs.isEmpty("Ecom_Payment_Card_ExpDate_Month"))
				fs.raiseError("You must specify the Credit Card expiry Month.");
			
			if (fs.isEmpty("Ecom_Payment_Card_ExpDate_Year"))
				fs.raiseError("You must specify the Credit Card expiry Year.");
				
			if (fs.isEmpty("Ecom_Payment_Card_Verification"))
				fs.raiseError("You must specify the CVV number.");
			
			var d = new Date();
		  	Year = new String(d.getYear());
	
			Month = d.getMonth()+1;
  			if (Month < 10)
    			Month = "0"+Month;
			
			if (document.frmCheckout.Ecom_Payment_Card_ExpDate_Year[document.frmCheckout.Ecom_Payment_Card_ExpDate_Year.selectedIndex].value == Year)
			{
			 if (document.frmCheckout.Ecom_Payment_Card_ExpDate_Month[document.frmCheckout.Ecom_Payment_Card_ExpDate_Month.selectedIndex].value < Month)
				fs.raiseError("Please check the expiration date on your Credit Card");
			}
			
			break;
		default:
			break;
	}
	
	// check if anything has changed - if so, prompt the user to save the details
	hasChangedError = false;
	if (fs.findObj("haschanges").value == "1")
	{
		fs.raiseError("Changes have been made to the Your Billing, Recipient Shipping or Voucher Information. To ensure your details are saved correctly, please press the update button before pressing the Buy Button");
		hasChangedError = true;
	}
	
	if (!hasChangedError)
	{
		if ((fs.findObj("origcountry").value != fs.findObj("country").value) || (fs.findObj("origshiptocountry").value != fs.findObj("shipto_country").value))
		{
			fs.raiseError("Changes have been made to the Your Billing, Recipient Shipping or Voucher Information. To ensure your details are saved correctly, please press the update button before pressing the Buy Button");
			hasChangedError = true;
		}
	}
	
	
	if (fs.numErrors() > 0)
	{
		fs.displayErrors();
		return false;
	}
	else
	  return true;
}

function validateBookingRequest()
{
	fs =  new formValidator();
	
	fieldstoValidate = new Array(10);
	fieldstoValidate[0] = new fieldtoValidate("first_name", "first name");
	fieldstoValidate[1] = new fieldtoValidate("surname", "surname");
	fieldstoValidate[6] = new fieldtoValidate("daytime_number", "daytime_number");
	fieldstoValidate[9] = new fieldtoValidate("email_address", "email_address");
		
	for (i=0;i<fieldstoValidate.length;i++)
	{
		if (fs.isEmpty(fieldstoValidate[i].fieldname))
		  fs.raiseError("A "+fieldstoValidate[i].fieldalias+" must be specified.");
	}

	//date validation 
	var d = new Date();
	Year = new String(d.getFullYear());

	Month = d.getMonth()+1;
	if (Month < 10)
		Month = "0"+Month;
	
	Day = d.getDate();
	if (Day < 10)
 		Day = "0"+Day;

	aVal = Year+Month+Day; //alternate format
  
	//aVal = Day+"/"+Month+"/"+Year;
  	//MM/DD/YYYY
  	//check Date alternative 1
	
	// formatDate(day, month, year) < aVal
	if (formatDate("YYYYMMDD", fs.findObj("thedayname1").value, getMonthNum(fs.findObj("themonthname1").value), fs.findObj("theyearname1").value) < aVal)
	  	fs.raiseError("bookings can only be made for the future.\n --- Please check your 1st preference dates");
	// new processing using the fs.findObj()
	
	if (fs.numErrors() > 0)
	{
		fs.displayErrors();
		return false;
	}
	else
	  return true;
}

//returns the numeric value for the passed text values
function getMonthNum(val)
{
	switch(val)
    {
        case 'January': return '01'; break
        case 'February': return '02'; break
        case 'March': return '03'; break
		case 'April': return '04'; break
		case 'May': return '05'; break
		case 'June': return '06'; break
		case 'July': return '07'; break
		case 'August': return '08'; break
		case 'September': return '09'; break
		case 'October': return '10'; break
		case 'November': return '11'; break
		case 'December': return '12'; break
        default: alert("No Month Calculation ERROR.");
	}
}

function fieldtoValidate(fieldname, fieldalias)
{
	this.fieldname = fieldname;
	this.fieldalias = fieldalias;
}

//determines todays date
function determineDate(aDateStr) {
	theDate = new String(aDateStr);

	// format the date entered to the appropriate date format...		
	day = theDate.substring(0, 2);
	month = theDate.substring(3, 5);
	year = theDate.substring(6, 10);

	properDate = new Date(day+'/'+month+'/'+year);
	return properDate;
}

//trim whitespace on left
function ltrim ( s )
{
	return s.replace( /^\s*/, "" );
}
//trim whitespace on right
function rtrim ( s )
{
	return s.replace( /\s*$/, "" );
}
//trim white space on left & right
function trim ( s )
{
	return rtrim(ltrim(s));
}

/**
 * DHTML date validation script for dd/mm/yyyy. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

//returns a boolean if the passed variable
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}


//strips white space
function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

//returns days in feb
function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("The date entered is invalid - please check the month and day you have specified.")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("The date entered is invalid - please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}	

//simply formats the date
function formatDate(dateformat, day, month, year) {
	if (dateformat == "DD/MM/YYYY")
		return day+"/"+month+"/"+year;
	else
	if (dateformat == "YYYYMMDD")
	  	return year+month+day;
	else
	if (dateformat == "YYYYMM")
	  	return year+month;
	else  
		return month+"/"+day+"/"+year;
}

function goCalendar()
{
	window.open('modules/calendar/html/index.php', '', 'resizable=no,scrollbars=no,width=400,height=450');
}

function checkLength(form, controlName) 
{
	StrLen = controlName.value.length;
  	maxChar = form.maxChars.value
  
	/*if (StrLen > maxChar) 
	{
		//controlName.value = controlName.value.substring(0,maxChar);
		charsLeft = 0;
		if (controlName.value.length > maxChar) 
		{
			controlName.value = controlName.value.substring(0, maxChar-1);
			form.message2.value = controlName.value;
		}
		
	} 
	else 
	{*/
		charsLeft = maxChar - StrLen;
	//}

	form.charsLeft.value = charsLeft;
}

function findPostalCodes()
{
	window.open('post_codes.php', '', 'scrollbars=yes,resizable=yes,width=550,height=450');
}

function validateBillingDetails()
{
	fs =  new formValidator();
	
	if (fs.isEmpty("first_name"))
		fs.raiseError("Your first_name must be specified.");	
		
	if (fs.isEmpty("last_name"))
		fs.raiseError("Your surname must be specified.");	
		
	if (fs.isEmpty("user_email"))
		fs.raiseError("Your email must be specified.");	
	else
	{
		if (!fs.isEmailAddress("user_email"))
			fs.raiseError("Please specify a valid email address.");	
	}

		
/*	if (fs.isEmpty("address_1") && fs.isEmpty("address_2"))
		fs.raiseError("At least one of the address fields must be specified.");	
		
	if (fs.isEmpty("city"))
		fs.raiseError("A city must be specified.");	
		
	if (fs.isEmpty("zip"))
		fs.raiseError("A zip/postal code must be specified.");	
		
	if (fs.isEmpty("state"))
		fs.raiseError("A state/province must be specified.");	
		
	if (fs.isEmpty("Country"))
		fs.raiseError("A country must be specified.");	*/
		
	if (fs.numErrors() > 0)
	{
		fs.displayErrors();
		return false;
	}
	else
	  return true;
}

function validateRegistrationForm()
{
	fs = new formValidator();
	
	if (fs.isEmpty("user_email"))
		fs.raiseError("Please specify your email address");	
	else
	{
		if (!fs.isEmailAddress("user_email"))
			fs.raiseError("Please specify a valid email address.");	
	}
		
	if (fs.isEmpty("password_1"))
		fs.raiseError("Please specify your password.");
		
	password1 = fs.findObj("password_1").value;
	password2 = fs.findObj("password_2").value;
	
	if (password1 != password2)
		fs.raiseError("The passwords entered do not match");
				
	if (fs.isEmpty("first_name"))
		fs.raiseError("Please specify your first name.");
		
	if (fs.isEmpty("last_name"))
		fs.raiseError("Please specify your surname.");
				
	if (fs.numErrors() > 0)
	{
		fs.displayErrors();
		return false;
	}
	else
	  return true;
}

// Refer - A - Friend Validation
function validateReferrals()
{
	fv = new formValidator();
	
	if (fv.isEmpty("your_name"))
		fv.raiseError("Please specify your name.");

	if (fv.isEmpty("your_email"))
		fv.raiseError("Please specify an email address.");
	else
	{
		if (!fv.isEmailAddress("your_email"))
			fv.raiseError("Please enter a valid email address.");
	}
	
	friend1 = fv.isEmpty("friend_1");
	friend2 = fv.isEmpty("friend_2");
	friend3 = fv.isEmpty("friend_3");
	friend4 = fv.isEmpty("friend_4");
	friend5 = fv.isEmpty("friend_5");
	
	friendName1 = fv.isEmpty("friend_1_name");
	friendName2 = fv.isEmpty("friend_2_name");
	friendName3 = fv.isEmpty("friend_3_name");
	friendName4 = fv.isEmpty("friend_4_name");
	friendName5 = fv.isEmpty("friend_5_name");
	
	// Check that if user has entered a name for Friend and that friend name has an email address
	if (!friendName1)
	{
		if (friend1)
			fv.raiseError("Please enter an email address for the 1st friend.");
	}
	
	if (!friendName2)
	{
		if (friend2)
			fv.raiseError("Please enter an email address for the 2nd friend.");
	}
	
	if (!friendName3)
	{
		if (friend3)
			fv.raiseError("Please enter an email address for the 3rd friend.");
	}
	
	if (!friendName4)
	{
		if (friend4)
			fv.raiseError("Please enter an email address for the 4th friend.");
	}
	
	if (!friendName5)
	{
		if (friend5)
			fv.raiseError("Please enter an email address for the 5th friend.");
	}
	
	if (!friend1)
	{
		if (!fv.isEmailAddress("friend_1"))
			fv.raiseError("Please enter a valid email address for the 1st friend entered.");
	}
	
	if (!friend2)
	{
		if (!fv.isEmailAddress("friend_2"))
			fv.raiseError("Please enter a valid email address for the 2nd friend entered.");
	}

	if (!friend3)
	{
		if (!fv.isEmailAddress("friend_3"))
			fv.raiseError("Please enter a valid email address for the 3rd friend entered.");
	}

	if (!friend4)
	{
		if (!fv.isEmailAddress("friend_4"))
			fv.raiseError("Please enter a valid email address for the 4th friend entered.");
	}

	if (!friend5)
	{
		if (!fv.isEmailAddress("friend_5"))
			fv.raiseError("Please enter a valid email address for the 5th friend entered.");
	}
	
	if (friend1 && friend2 && friend3 && friend4 && friend5)
		fv.raiseError("At least one friend needs to referred in order to enter the competition.");
		
	if (fv.numErrors() > 0)
	{
		fv.displayErrors();
		return false;
	}
	else
		return true;
}