/*Function to "break out" of framed pages*/
function brakeFrame(){
	if(top.location != self.location){
		top.location.href = document.location.href;
	}
}

/*Function to validate form on contactus*/
function contactValidate(){
    	//flag for error message
    	var flagError = 0;

    	//for error message
    	var errorText = "ERROR\nPlease enter\n";

	//for blank email
    	//var flagEmailBlankError = 0;

 	//for email error
    	var flagEmailError = 0;

	//error message for email
	var emailError = "";

	//for blank enquiryComment
	var flagEnquiryCommentBlankError = 0;

	//error message for enquiryComment
	var enquiryCommentError = "";

	//for blank form
	var formBlankError = "";
	
	//flag for blank form
	var flagBlankFormError = 0;

    	//error message for blanks in address block
    	var addressBlankError = "";

    	//for blanks in address block
    	var flagAddressBlankError = 0;

    	//for blank phone
    	var flagPhoneBlankError = 0;

    	//error message for phone
    	var phoneError = "";

	//flag for arrayAddress
	var addressCount = 0;

	//to make sure a name is entered in input field
	if (window.document.getElementById("contactForm").name.value == ""){ 
		errorText += "Name\n";
		flagError++;
		}

	//**********Testing for entries in Address block****************
	var arrayAddress = new Array(4);
	arrayAddress[0] = window.document.getElementById("contactForm").address.value;
	arrayAddress[1] = window.document.getElementById("contactForm").postCodeZipCode.value;
	arrayAddress[2] = window.document.getElementById("contactForm").state.value;
	arrayAddress[3] = window.document.getElementById("contactForm").country.value;

	for (var count = 0; count < arrayAddress.length; count++)
	{
		if (arrayAddress[count] != "")
		{
			addressCount++;
		}
	}

 	//window.alert(arrayAddress[0]);
	//window.alert(addressCount);

	//To validate for valid email address
	var x=window.document.getElementById("contactForm") ;
	var Temp=x.email.value ;
	var EmailOk  = true ;
	var AtSym    = Temp.indexOf('@') ;
	var Period   = Temp.lastIndexOf('.');
	var Space    = Temp.indexOf(' ') ;
	var Length   = Temp.length - 1;
	if (((AtSym < 1) ||                // '@' cannot be in first position
		(Period <= AtSym+1) ||         // Must be atleast one valid char btwn '@' and '.'
		(Period == Length ) ||         // Must be atleast one valid char after '.'
		(Space  != -1)) &&             // No empty spaces permitted
		!(window.document.getElementById("contactForm").email.value == ""))
		{
		window.document.getElementById("contactForm").email.value = "";
		flagEmailError++;
		flagError++;
		emailError = "Valid Email\n";
		}

	//To see if textarea has text in it
	if(
	(window.document.getElementById("contactForm").enquiryComment.value == ""))
	{
		flagEnquiryCommentBlankError++;
		flagError++;
		enquiryCommentError = "Enquiry/Comment\n";
	}

	//To see if anything has been entered on the form
	if((window.document.getElementById("contactForm").name.value == "") &&
	(addressCount < 1) &&	
	(window.document.getElementById("contactForm").phone.value == "") &&
	(window.document.getElementById("contactForm").email.value == "") &&
	(window.document.getElementById("contactForm").enquiryComment.value == ""))
	{
		//errorText = "ERROR\nNothing entered on form";
		flagBlankFormError++;
		flagError++;
		formBlankError = "ERROR\nNothing entered on form";
	}	 

	//To see if address or country or phone or email is on the form
	if((window.document.getElementById("contactForm").name.value != "") &&
	(addressCount < 1) &&
	(window.document.getElementById("contactForm").phone.value == "") &&
	(window.document.getElementById("contactForm").email.value == "") &&
	(window.document.getElementById("contactForm").enquiryComment.value != "") &&
	(flagEmailError < 1))
	{
		flagError++;
		errorText += "Address (with Post Code or Zip Code with State and Country) OR Phone OR Email\n";
	}

	//To see if address or country or phone or email is on the form
	if((window.document.getElementById("contactForm").name.value != "") &&
	(addressCount < 1) &&	
	(window.document.getElementById("contactForm").phone.value == "") &&
	(window.document.getElementById("contactForm").email.value == "") &&
	(window.document.getElementById("contactForm").enquiryComment.value == "") &&
	(flagEmailError < 1))
	{
		flagError++;
		errorText += "Address (with Post Code or Zip Code with State and Country) OR Phone OR Email\n";
	}

	//To see if address or country or phone or email is on the form
	if((window.document.getElementById("contactForm").name.value == "") &&
	(addressCount < 1) &&
	(window.document.getElementById("contactForm").phone.value == "") &&
	(window.document.getElementById("contactForm").email.value == "") &&
	(window.document.getElementById("contactForm").enquiryComment.value != "") &&
	(flagEmailError < 1))
	{
		flagError++;
		errorText += "Address (with Post Code or Zip Code with State and Country) OR Phone OR Email\n";
	}	 

	//Output if Address block not filled in completely
	if((addressCount > 0) && (addressCount < 4))
	{
		flagError++;
		errorText += "Address (with Post Code or Zip Code with State and Country)\n";
	}

	//**********Output block****************
	if(flagBlankFormError > 0){
		errorText = formBlankError;
	}

	if((flagEnquiryCommentBlankError > 0) && (flagBlankFormError < 1)){
		errorText += enquiryCommentError;
		flagError++;
	}

	if((flagEmailError > 0) && (flagBlankFormError < 1)){
         errorText += emailError;
        flagError++;
   }

   //Test to see if error message is shown
   if(flagError > 0){
        window.alert(errorText);
   }

    //Return statements
   if(flagError < 1)
         return true;
    return false;
}


/*Function to validate form on contactus*/
function donationValidate(){
    	//flag for error message
    	var flagError = 0;

    	//for error message
    	var errorText = "ERROR\nPlease enter\n";

	//for blank email
   // 	var flagEmailBlankError = 0;

 	//for email error
    	var flagEmailError = 0;

	//error message for email
	var emailError = "";

	//for blank enquiryComment
	var flagEnquiryCommentBlankError = 0;

	//error message for enquiryComment
	var enquiryCommentError = "";

	//for blank form
	var formBlankError = "";
	
	//flag for blank form
	var flagBlankFormError = 0;

    	//error message for blanks in address block
    	var addressBlankError = "";

    	//for blanks in address block
    	var flagAddressBlankError = 0;

    	//for blank phone
    	var flagPhoneBlankError = 0;

    	//error message for phone
    	var phoneError = "";

	//flag for arrayAddress
	var addressCount = 0;

	//to make sure a name is entered in input field
	if (window.document.getElementById("donationForm").name.value == ""){ 
		errorText += "Name\n";
		flagError++;
		}

	//**********Testing for entries in Address block****************
	var arrayAddress = new Array(4);
	arrayAddress[0] = window.document.getElementById("donationForm").address.value;
	arrayAddress[1] = window.document.getElementById("donationForm").postCodeZipCode.value;
	arrayAddress[2] = window.document.getElementById("donationForm").state.value;
	arrayAddress[3] = window.document.getElementById("donationForm").country.value;

	for (var count = 0; count < arrayAddress.length; count++)
	{
		if (arrayAddress[count] != "")
		{
			addressCount++;
		}
	}

	//To validate for valid email address
	var x=window.document.getElementById("donationForm") ;
	var Temp=x.email.value ;
	var EmailOk  = true ;
	var AtSym    = Temp.indexOf('@') ;
	var Period   = Temp.lastIndexOf('.');
	var Space    = Temp.indexOf(' ') ;
	var Length   = Temp.length - 1;
	if (((AtSym < 1) ||                // '@' cannot be in first position
		(Period <= AtSym+1) ||         // Must be atleast one valid char btwn '@' and '.'
		(Period == Length ) ||         // Must be atleast one valid char after '.'
		(Space  != -1)) &&             // No empty spaces permitted
		!(window.document.getElementById("donationForm").email.value == ""))
		{
		window.document.getElementById("donationForm").email.value = "";
		flagEmailError++;
		flagError++;
		emailError = "Valid Email\n";
		}

	//To see if anything has been entered on the form
	if((window.document.getElementById("donationForm").name.value == "") &&
	(addressCount < 1) &&
	(window.document.getElementById("donationForm").phone.value == "") &&
	(window.document.getElementById("donationForm").email.value == "") &&	
	(window.document.getElementById("donationForm").regularDonationAustralianDollars.value == "")&&
	(window.document.getElementById("donationForm").regularDonationUSDollars.value == "") &&
	(window.document.getElementById("donationForm").oneOffDonationAustralianDollars.value == "")&&
	(window.document.getElementById("donationForm").oneOffDonationUSDollars.value == "") &&
	(window.document.getElementById("donationForm").regularDonationMonths.value == ""))
	{
		//errorText = "ERROR\nNothing entered on form";
		flagBlankFormError++;
		flagError++;
		formBlankError = "ERROR\nNothing entered on form";
	}	 

	//To see if address or country or phone or email is on the form
	if((window.document.getElementById("donationForm").name.value != "") &&
	(addressCount < 1) &&
	(window.document.getElementById("donationForm").phone.value == "") &&
	(window.document.getElementById("donationForm").email.value == "") &&
	 //(window.document.getElementById("donationForm").enquiryComment.value != "") &&
	(flagEmailError < 1))
	{
		flagError++;
		errorText += "Address (with Post Code or Zip Code with State and Country) OR Phone OR Email\n";
	}

	//To see if address or country or phone or email is on the form
	if((window.document.getElementById("donationForm").name.value != "") &&
	(addressCount < 1) &&
	(window.document.getElementById("donationForm").phone.value == "") &&
	(window.document.getElementById("donationForm").email.value == "") &&
	(flagEmailError < 1))
	{
		flagError++;
		errorText += "Address (with Post Code or Zip Code with State and Country) OR Phone OR Email\n";
	}

	//To see if address or country or phone or email is on the form
	if((window.document.getElementById("donationForm").name.value == "") &&
	(addressCount < 1) &&
	(window.document.getElementById("donationForm").phone.value == "") &&
	(window.document.getElementById("donationForm").email.value == "") &&
	(flagEmailError < 1))
	{
		flagError++;
		errorText += "Address (with Post Code or Zip Code with State and Country) OR Phone OR Email\n";
	}	 

	//Output if Address block not filled in completely
	if((addressCount > 0) && (addressCount < 4))
	{
		flagError++;
		errorText += "Address (with Post Code or Zip Code with State and Country)\n";
	}

	//To see if Regular Donation or One Off Donation together with
	//dollar type has been entered on the form
	if(((window.document.getElementById("donationForm").name.value != "") ||
	(addressCount > 0) &&
	(window.document.getElementById("donationForm").phone.value != "") ||
	(window.document.getElementById("donationForm").email.value != "")) &&
	(window.document.getElementById("donationForm").regularDonationAustralianDollars.value == "") &&
	(window.document.getElementById("donationForm").regularDonationUSDollars.value == "") &&
	(window.document.getElementById("donationForm").oneOffDonationAustralianDollars.value == "") &&
	(window.document.getElementById("donationForm").oneOffDonationUSDollars.value == "") &&
	(window.document.getElementById("donationForm").regularDonationMonths.value == ""))
	{
		flagError++;
		errorText += "Amount for either Regular Donation or One Off Donation AUD or USD\n";
	}
	
	//To see if Number of months entered with Regular Donation
	if(((window.document.getElementById("donationForm").regularDonationAustralianDollars.value != "") ||
	(window.document.getElementById("donationForm").regularDonationUSDollars.value != "")) &&
	(window.document.getElementById("donationForm").regularDonationMonths.value == ""))
	{
		flagError++;
		errorText += "Number of months with your Regular Donation\n";
	}

	//To see if Number of months entered with Regular Donation
	if(((window.document.getElementById("donationForm").regularDonationAustralianDollars.value == "") &&
	(window.document.getElementById("donationForm").regularDonationUSDollars.value == "")) &&
	(window.document.getElementById("donationForm").regularDonationMonths.value != ""))
	{
		flagError++;
		errorText += "Amount of Regular Donation in AUD or USD with Number of Months\n";
	}

	//**********Output block****************
	if(flagBlankFormError > 0){
		errorText = formBlankError;
	}

	if((flagEnquiryCommentBlankError > 0) && (flagBlankFormError < 1)){
		errorText += enquiryCommentError;
		flagError++;
	}

	if((flagEmailError > 0) && (flagBlankFormError < 1)){
         errorText += emailError;
        flagError++;
   }

   //Test to see if error message is shown
   if(flagError > 0){
        window.alert(errorText);
   }

    //Return statements
   if(flagError < 1)
         return true;
    return false;
}