window.onload=function(){
	externalLinksByHref();
	if(isHomepage){
		initSwap();		
	}
	
	
}

function PopUp(id){
	if(id!=""){
		var strURL = "/photo.asp?id=" + id
		var objPhotoWindow = window.open(strURL,'photo','height=500,width=550,left=10,top=10,directories=0,fullscreen=0,location=0,menubar=0,scrollbars=1,resizable=0,status=0,toolbar=0');
	}

}


// the slides - relative or complete urls for any number of swappable images
var slides = new Array('/css/images/home_banner1.jpg','/css/images/home_banner2.jpg');
var slideIndex = 0; // which slide are we viewing?
var fadeTimer = 2; // time, in seconds, it takes to swap images
var swapTimer = 10; // time, in seconds, between image swaps

// gotta set it all up before we run it...
function initSwap() {
	// CAREFUL! we ASSUME there's a page element with id 'fadeBox' to 
	// contain the effected images
	//alert("HI")
	var fadeBox = document.getElementById('fadeBox');
	// set up the actual images
	for(var i=0;i<slides.length;i++) {
		// hooray for the DOM
		var slide = document.createElement('img'); // a new image
		slide.src = slides[i]; // should show this image
		slide.useMap ='#homemap' + i
		slide.style.display = 'none';
		slide.style.position = 'absolute'; // important so images stay on top of each other
		slide.style.opacity = '0'; // init to transparent (CSS2)
		slide.style.filter = 'alpha(opacity:0)'; // init to transparent (MSIE)
		fadeBox.appendChild(slide); // put the image in the box
		//alert(slide.useMap)
		slides[i] = slide; // reassign to same array for convenience
		// if it's the first image, let's show it now to avoid waiting
		if(i==0) { 
			fade(i,1); // fade it in!
			setTimeout(doSwap,swapTimer*1000); // start the swap timer!
		}
		// repeat for each slide
	}
}

function doSwap() {
	var s1 = slideIndex; // where *are* we?
	var s2 = s1+1==slides.length?0:s1+1; // either the next or the first
	// we just wrapped to the beginning if we hit the end of the array, so...
	slideIndex = s2; // update slide index
	fade(s1,0); // fade the old slide out!
	fade(s2,1); // fade the new slide in!
	setTimeout(doSwap,swapTimer*1000); // do it again in swapTimer seconds!
}

function fade(whoid,dir) {
	var slide = slides[whoid]; // get the slide element at index whoid
	var completed; // so we know when the fade is done
	var opac = parseFloat(slide.style.opacity,10); // get a reference value
	// increment if fading in, decrement if fading out
	if(dir > 0) { 
		slide.style.display = 'block';
		opac += .1; // more opaque
		if(opac >= 1) {
			// fade is at max! fade done!
			completed = true;
		}
	} else {
		opac -= .1; // less opaque
		if(opac <= 0) {
			// fade is at min! fade done!
			slide.style.display = 'none';
			completed = true;
		}
	}
	slide.style.opacity = opac; // set opacity (CSS2)
	slide.style.filter = 'alpha(opacity:'+(opac*100)+')'; // set opacity (MSIE)
	if(!completed) {
		// as long as the fade is not complete, keep doing this in 1/10 increments within fadeTimer seconds
		setTimeout("fade("+whoid+","+dir+")",parseInt(fadeTimer/10,10));
	}
}

// start it up when the page loads!
// ideally you want to place this in an onload appender or manager - if you use 
// a lot of JS you may end up overriding your onload


function strTrim(strText) {
		for (i=0; i<strText.length; ++i) {
			if (strText.charAt(i) != ' ') break;
		}
		if (i >= strText.length) return ('');
		if (i > 0) strText = strText.substring(i, strText.length);
		for (j=strText.length-1; j > i; --j) {
			if (strText.charAt(j) != ' ') break;
		}
		if (j < strText.length -1)
			strText = strText.substring(0, j+1);
		return strText;

}
function ValidEmail(s){
    var i = 1;
    var sLength = s.length;
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }
    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
} 


function MM_findObj(n, d) { //v4.01
		  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
		  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
		  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
		  if(!x && d.getElementById) x=d.getElementById(n); return x;
		}

		function MM_validateForm() { //v4.0
		  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
		  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
		    if (val) { nm=val.name; if ((val=val.value)!="") {
		      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
			if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
		      } else if (test!='R') { num = parseFloat(val);
			if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
			if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
			  min=test.substring(8,p); max=test.substring(p+1);
			  if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
		    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
		  } if (errors) alert('The following error(s) occurred:\n'+errors);
		  document.MM_returnValue = (errors == '');
		}

function checkSearch(query) {
	if(!query) {
		alert('Please enter a search term');
		return false;
	}
}

function DisplayPassword(){
	var obj = xDOM("forgotPassword",0);
	if(obj.style.height == "0px"){
		RevealPassword()
	}else{
		ClosePassword()
	}	

}
var intHeight = 0
var inTotal = 100
function RevealPassword(){
	var obj = xDOM("forgotPassword",0);
	if(intHeight == inTotal){
		setTimeout("setPassFocus()",10);
	}else{
		intHeight = intHeight + 10	
		obj.style.height = intHeight + "px";
		setTimeout("RevealPassword()",1);
	}			
}
function ClosePassword(){
	var obj = xDOM("forgotPassword",0);
	if(intHeight == 0){

	}else{
		intHeight = intHeight - 10	
		obj.style.height = intHeight + "px";
		setTimeout("ClosePassword()",1);
	}			
}

function CheckCountry(strCountry,objID){
	if(strCountry=="United States"){
		$(objID).slideDown();
	}else{
		$(objID).slideUp();
	}
}

function setPassFocus(){
	var objInput = xDOM("forgotForm",0);
	objInput.email.focus();	
}

function CheckPaymentType(objForm){
	if (strValue != ""){
		var strSelectValue = strValue
	}else{
		var strSelectValue = objForm.Agree[Agree.selectedIndex].value;
	}
	
	var objCC = xDOM("CCDetails",0);
	if(strSelectValue.substring(0,1) == '1'){
		objCC.style.display = "block";
	}else{
		objCC.style.display = "none";	
		
	}
}


// Cross Browser DOM Functions used to create objects and get top / left coords
// copyright Stephen Chapman, 4th Jan 2005
// you may copy this code but please keep the copyright notice as well
var aDOM = 0, ieDOM = 0, nsDOM = 0
var stdDOM = document.getElementById;
if (stdDOM) aDOM = 1; else {
  ieDOM = document.all;
  if (ieDOM) aDOM = 1; else {
    var nsDOM = (
      (navigator.appName.indexOf('Netscape') != -1)
      && (parseInt(navigator.appVersion) ==4));
    if (nsDOM) aDOM = 1;
  }
}
function xDOM(objectId, withStyle) {
var st = ''; if (withStyle) st = '.style';
if (stdDOM) return (eval('document.getElementById(objectId)'+st));
if (ieDOM) return (eval('document.all[objectId]'+st));
if (nsDOM) return (document.layers[objectId]);
}


function ClearPassword(value){
	if(strTrim(value)==''){	
		xDOM("PasswordText",1).display ="none";
		xDOM("LoginPassword",0).focus();
	}
}

function RestorePassword(value){
	if(strTrim(value)==''){
		xDOM("PasswordText",1).display ="block";
	}else{
		xDOM("PasswordText",1).display ="none";
	}
}
function ClearEmail(value){
	if(strTrim(value)=='Email'){
		xDOM("LoginEmail",0).value="";
		xDOM("LoginEmail",0).focus();
	}
}
function RestoreEmail(value){
	if(strTrim(value)==''){
		xDOM("LoginEmail",0).value="Email";
		RestorePassword(xDOM("LoginPassword",0).value)
	}
}

function Recaculate(objForm){
	objForm.frmaction.value = "recaculate";
	objForm.checkoutstep.value = "orderdetails";
	
}

function ValidateLogin(objForm){
	var strAlert = "";
	strAlert = "______________________________________\n\nPlease\n\n";
	var isAlert = false;
	var strEmail = strTrim(objForm.email.value);
	var strPassword = strTrim(objForm.password.value);
	if(strEmail == "") {
		strAlert += "- Enter your email address\n";
		isAlert = true;
	}else if(!ValidEmail(strEmail)){
		strAlert += "- Enter a valid email address\n";
		isAlert = true;
	}	
	if (strPassword==""){
		strAlert += "- Enter your password\n";
		isAlert = true;
	}
	if (isAlert == true){
		strAlert += "\n\n______________________________________";
		alert (strAlert);
		return false;
	}else{
		return true;
	}
}



function ValidateUpdateUser(objForm){
	
	var strAlert = "";
	strAlert = "______________________________________\n\nPlease\n\n";
	var isAlert = false;
	var strFirstName = strTrim(objForm.firstName.value);
	var strLastName = strTrim(objForm.lastName.value);
	var strEmail = strTrim(objForm.email.value);


	/** Unit password **/
	var strPassword = strTrim(objForm.password.value);
	var strConfirmPassword = strTrim(objForm.confirmpassword.value);
	
	if (strFirstName==""){
		strAlert += "- Enter your first name\n";
		isAlert = true;
	}
	if (strLastName==""){
		strAlert += "- Enter your last name\n";
		isAlert = true;
	}			
			
	if(strEmail == "") {
		strAlert += "- Enter your email address\n";
		isAlert = true;
	}else if(!ValidEmail(strEmail)){
		strAlert += "- Enter a valid email address\n";
		isAlert = true;
	}		
	
	if(strPassword!="!NULL!"){
		if(strPassword==""){
			strAlert += "- Enter your new password\n";
			isAlert = true;			
		}else if(strPassword.length < 6){
			strAlert += "- Ensure your new password is at least 6 characters\n";
			isAlert = true;
		}if(strPassword != strConfirmPassword){
			strAlert += "- Ensure your new password and confirm password match\n";
			isAlert = true;
		}					
	}
	
	
	if (isAlert == true){
		strAlert += "\n\n______________________________________";
		alert (strAlert);
		return false;
	}else{
		return true;
	}
	
	
	
}



function ValidateNewUser(objForm){
	var strAlert = "";
	strAlert = "______________________________________\n\nPlease\n\n";
	var isAlert = false;
	var strFirstName = strTrim(objForm.firstName.value);
	var strLastName = strTrim(objForm.lastName.value);
	var strEmail = strTrim(objForm.email.value);
	var strPassword = strTrim(objForm.password.value);
	var strConfirmPassword = strTrim(objForm.confirmpassword.value);
	
	
	if (strFirstName==""){
		strAlert += "- Enter your first name\n";
		isAlert = true;
	}
	if (strLastName==""){
		strAlert += "- Enter your last name\n";
		isAlert = true;
	}			
				
	if(strEmail == ""){
		strAlert += "- Enter your email address\n";
		isAlert = true;
	}else if(!ValidEmail(strEmail)){
		strAlert += "- Enter a valid email address\n";
		isAlert = true;
	}		
	if(strPassword == "") {
		strAlert += "- Enter a password\n";
		isAlert = true;
	}else if(strPassword.length < 6){
		strAlert += "- Ensure your password is at least 6 characters\n";
		isAlert = true;
	}else if(strPassword != strConfirmPassword){
		strAlert += "- Ensure your password and confirm password match\n";
		isAlert = true;
	}			
	
	
	if (isAlert == true){
		strAlert += "\n\n______________________________________";
		alert (strAlert);
		return false;
	}else{
		return true;
	}
}
function ValidateContact(objForm){
	var strAlert = "";
	strAlert = "______________________________________\n\nPlease\n\n";
	var isAlert = false;
	var strFirstName = strTrim(objForm.firstName.value);
	var strLastName = strTrim(objForm.lastName.value);
	var strEmail = strTrim(objForm.email.value);
	var strEnquiry = strTrim(objForm.Enquiry.value);
	
	
	if (strFirstName==""){
		strAlert += "- Enter your first name\n";
		isAlert = true;
	}
	if (strLastName==""){
		strAlert += "- Enter your last name\n";
		isAlert = true;
	}			
				
	if(strEmail == ""){
		strAlert += "- Enter your email address\n";
		isAlert = true;
	}else if(!ValidEmail(strEmail)){
		strAlert += "- Enter a valid email address\n";
		isAlert = true;
	}		
	if (strEnquiry.length < 5){
		strAlert += "- Enter your enquiry\n";
		isAlert = true;
	}			
	
	
	if (isAlert == true){
		strAlert += "\n\n______________________________________";
		alert (strAlert);
		return false;
	}else{
		return true;
	}
}

function ValidateCreateTestimonial(objForm){
	var strAlert = "";
	strAlert = "______________________________________\n\nPlease\n\n";
	var isAlert = false;
	var strFirstName = strTrim(objForm.firstName.value);	
	var strSuburbtown = strTrim(objForm.suburbtown.value);
	var strCountry = strTrim(objForm.country.value);
	var strTestimonial = strTrim(objForm.Testimonial.value);
	
	
	if (strFirstName==""){
		strAlert += "- Enter your first name\n";
		isAlert = true;
	}
	if (strSuburbtown==""){
		strAlert += "- Enter your surburb or town\n";
		isAlert = true;
	}			
	if (strCountry==""){
		strAlert += "- Enter your country\n";
		isAlert = true;
	}					
	
	if (strTestimonial.length < 5){
		strAlert += "- Enter your testimonial\n";
		isAlert = true;
	}			
	
	
	if (isAlert == true){
		strAlert += "\n\n______________________________________";
		alert (strAlert);
		return false;
	}else{
		return true;
	}
}

function CheckPayment(objForm){
	var strAlert = "";
	strAlert = "______________________________________\n\nPlease\n\n";
	var isAlert = false;
	var intBalance = Math.ceil(objForm.balance.value)
	var blnTC = objForm.Agree.checked

	
	if(intBalance>0){
		var paymentMethod = objForm.paymentType.options[objForm.paymentType.selectedIndex].value;	
		var intCreditCard = strTrim(objForm.ccnumber.value)
		var intCreditCardName = strTrim(objForm.nameoncard.value)
				
		if(paymentMethod==""){
			strAlert += "- Please select a payment method.\n";
			isAlert = true;		
		}else if(paymentMethod.substring(0,1) == '1'){ // Its a credit card
			if (intCreditCardName==""){
				strAlert += "- Please enter the name on your Credit Card\n";
				isAlert = true;
			}
			if (intCreditCard==""){
				strAlert += "- Please enter a valid Credit Card number\n";
				isAlert = true;
			}		
		}
	}
	if (!blnTC){
		strAlert += "- Please accept the terms and conditions.\n";
		isAlert = true;
	}		
	if (isAlert == true){
		strAlert += "\n\n______________________________________";
		alert (strAlert);
		return false;
	}else{
		return true;
	}	
	
}

function CheckLogin(objForm){
	var strAlert = "";
	strAlert = "______________________________________\n\nPlease\n\n";
	var isAlert = false;

	var username = strTrim(objForm.username.value)
	var password = strTrim(objForm.password.value)

	if (username==""){
		strAlert += "- Please enter your username (email address)\n";
		isAlert = true;
	}		

	if (password==""){
		strAlert += "- Please enter your password\n";
		isAlert = true;
	}			
	if (isAlert == true){
		strAlert += "\n\n______________________________________";
		alert (strAlert);
		return false;
	}else{
		return true;
	}
}


function ValidateReport(objForm,r_time,strReportType){
	var strAlert = "";
	var isAlert = false;
	var strFirstName = strTrim(objForm.firstName.value);
	var strLastName = strTrim(objForm.lastName.value);
	var strGender = objForm.gender.options[objForm.gender.selectedIndex].value;
	var strCity = strTrim(objForm.city.value);
	var strCountry = objForm.country.options[objForm.country.selectedIndex].value;	
	var strState = objForm.usState.options[objForm.usState.selectedIndex].value;		
	
	if (strFirstName==""){
		strAlert += "  - First name\n";
		isAlert = true;
	}
	if (strLastName==""){
		strAlert += "  - Last name\n";
		isAlert = true;
	}			
	if (strGender==""){
		strAlert += "  - Gender\n";
		isAlert = true;
	}
	if (strCity==""){
		strAlert += "  - City\n";
		isAlert = true;
	}		
	if (strCountry==""){
		strAlert += "  - Country\n";
		isAlert = true;
	}			
	if (strCountry=="United States" && strState==""){
		strAlert += "  - State\n";
		isAlert = true;
	}	
	
	if(strReportType=='LoveStars'){
		if (isAlert == true){
			strAlert = "Person 1\n" + strAlert;
		}	
		var strPerson2Alert = ""
		var strFirstName2 = strTrim(objForm.firstName2.value);
		var strLastName2 = strTrim(objForm.lastName2.value);
		var strGender2 = objForm.gender2.options[objForm.gender2.selectedIndex].value;
		var strCity2 = strTrim(objForm.city2.value);
		var strCountry2 = objForm.country2.options[objForm.country2.selectedIndex].value;	
		var strState2 = objForm.usState2.options[objForm.usState2.selectedIndex].value;		
		
		if (strFirstName2==""){
			strPerson2Alert += "  - First name\n";
			isAlert = true;
		}
		if (strLastName2==""){
			strPerson2Alert += "  - Last name\n";
			isAlert = true;
		}			
		if (strGender2==""){
			strPerson2Alert += "  - Gender\n";
			isAlert = true;
		}
		if (strCity2==""){
			strPerson2Alert += "  - City\n";
			isAlert = true;
		}		
		if (strCountry2==""){
			strPerson2Alert += "  - Country\n";
			isAlert = true;
		}	
		if (strCountry2=="United States" && strState2==""){
			strPerson2Alert += "  - State\n";
			isAlert = true;
		}			
		if (strPerson2Alert != ""){
			strAlert = strAlert + "\nPerson 2\n" + strPerson2Alert;
		}
	}
	
	
	
	
	if (isAlert == true){
		strAlert = "______________________________________\n\nThe following fields need to be completed\n" + strAlert + "\n\n______________________________________";
		alert (strAlert);
		return false;
	}else{
		return true;
	}
}

function ValidateOrderConf(objForm){
	var strAlert = "";
	
	var isAlert = false;

	if (objForm.frmaction.value == "recaculate"){
		strAlert = "______________________________________\n\n";	
		var quantity = parseFloat(objForm.quantity.value);
		//alert(quantity)
		if(quantity == 0 || isNaN(quantity)){
			if(confirm("Setting the quantity to 0 (or non-numeric) will delete your order. Are you sure? \n\nClick Ok to continue or cancel to return to your order")){
				return true;
			}else{
				return false;
			}
		}
	}else{

		var strName = strTrim(objForm.RecptName.value)
		var strAddress = strTrim(objForm.Address.value)
		var strCity = strTrim(objForm.City.value)
		var strState = strTrim(objForm.State.value)
		var strPostcode = strTrim(objForm.Postcode.value)
		var strCountry = strTrim(objForm.Country.value)

		strAlert = "______________________________________\n\nPlease\n\n";

		if (strName==""){
			strAlert += "- Enter the recipient's name\n";
			isAlert = true;
		}	
		if (strAddress==""){
			strAlert += "- Enter the recipient's address\n";
			isAlert = true;
		}	
		if (strCity==""){
			strAlert += "- Enter the recipient's suburb or town\n";
			isAlert = true;
		}	
		if (strState==""){
			strAlert += "- Enter the recipient's state\n";
			isAlert = true;
		}	
		if (strPostcode==""){
			strAlert += "- Enter the recipient's postcode\n";
			isAlert = true;
		}	
		/*
		if (strPhone==""){
			strAlert += "- Enter your phone number\n";
			isAlert = true;
		}	
		*/

		if (isAlert == true){
			strAlert += "\n\n______________________________________";
			alert (strAlert);
			return false;
		}else{
			return true;
		}

	}
}


// Credit Card Validation Javascript
// copyright 12th May 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function validateCreditCard(s) {
	var v = "0123456789";
	var w = "";
	for (var i=0; i < s.length; i++) {
	x = s.charAt(i);
	if (v.indexOf(x,0) != -1)
	w += x;
	}
	var j = w.length / 2;
	if (j < 6.5 || j > 8 || j == 7) return false;
	var k = Math.floor(j);
	var m = Math.ceil(j) - k;
	var c = 0;
	for (var i=0; i<k; i++) {
	a = w.charAt(i*2+m) * 2;
	c += a > 9 ? Math.floor(a/10 + a%10) : a;
	}
	for (var i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
	return (c%10 == 0);
}

function SetSelect(obj,value){
	for (var count = 0; count < obj.options.length; count++) {
		if(obj[count].value == value) {
			obj[count].selected = true;
		} else {
			obj[count].selected = false;
		}
	}	
}


function externalLinksByHref() {
	var anchors = document.getElementsByTagName("a");
	var i, href;
	for(i=0; i < anchors.length; i++){
		if(!anchors[i].href) continue;
		href = anchors[i].href;
		
		if(href.indexOf("stars") == -1){ // Href is not a file on my server
			if(href.indexOf("javascript:") == -1){ // Href is not a javascript call
				if(!anchors[i].onclick){ // Href does not have an onclick event
					if(href.indexOf("mailto:") ==-1){ // Href is not a mailto:
						if(href.indexOf("#") ==-1){ // Href is not an anchor					
							if(href.indexOf("http://") != -1){ // Href is not relative (for Safari)
							anchors[i].setAttribute("target","_blank");
							}
						}
					}
				}
			}
		}

	}
}

$(document).ready(function(){
						   
	$("#ToggleTestimonials").toggle(function(){
	  $("#MoreTestimonials").slideDown(200);
	  $("#ToggleTestimonials").html("Hide testimonials")
	},function(){
	  $("#MoreTestimonials").slideUp(200);
	  $("#ToggleTestimonials").html("See all testimonials")
	});				   
					   
});
	 



