var zone_count = 0;
var image_username_warnings = true;

function TickZone(bln) {
	if (bln && zone_count < 2) {
		zone_count++;
	}
	else if (bln && zone_count == 2) {
		alert('You may only select TWO zones.');
		return false;
	}
	else if (!bln) {
		zone_count--;
	}
	return true;
}

function ValidateSubscribeForm() {
		var email1 = new String(document.getElementById('Email').value).trim();
		var email2 = new String(document.getElementById('Email Retype').value).trim();
	
		if (email1 != email2) {
			alert('Please make sure you have typed your correct email in the confirmation box.');
			return false;
		}
		else {
			document.getElementById('Email').value = email1;
			document.getElementById('Email Retype').value = email2;
		}
			
			
		if (document.getElementById('username_ok').value != '1') {
			alert('Please make sure you have entered a valid username.');
			return false;
		}
		//if (document.getElementById('cmbPlan').selectedIndex <= 0) {
		//	alert('Please make sure you have selected a valid membership level.');
		//	return false;
		//}
		/*if (zone_count <= 0) {
			alert('Please make sure you have selected at least one port zone.');
			return false;
		}*/
		//if (document.getElementById('cmbPlan').selectedIndex == 1 && document.getElementById('txtGrowerNumber').value == '') {
		//	alert('Please enter your grower number.');
		//	return false;
		//}
		
		var sPass = document.getElementById('Password').value;
		if (sPass.length > 0) {
			if (testPassword(sPass) <= 12) {
				alert('Your select password must contain at least one capital letter, one number and be 5 characters or more. Please try again.');
				return false;
			}
		}


	return true;
}

function ValidateTrialSubscribeForm() {
		if (document.getElementById('Email').value != document.getElementById('Email Retype').value) {
			alert('Please make sure you have typed your correct email in the confirmation box.');
			return false;
		}
		if (document.getElementById('username_ok').value != '1') {
			alert('Please make sure you have entered a valid username.');
			return false;
		}

	return true;
}

function ValidateKondininSubscribeForm() {
		if (document.getElementById('Email').value != document.getElementById('Email Retyped').value) {
			alert('Please make sure you have typed your correct email in the confirmation box.');
			return false;
		}

	return true;
}

function CheckUsername(blnImageWarnings) {
	var objMessage = document.getElementById('spanUserMessage');
	objMessage.value = '';
	var objUser = document.getElementById('Username');
	document.getElementById('username_ok').value = '0';
	image_username_warnings = blnImageWarnings;

	if (objUser.value != ''){
		if (objUser.value.length > 4) {
			http.open("GET", '/member_username_check.asp?username=' + objUser.value, true);
			http.onreadystatechange = handleResponseUserCheck;
			http.send(null);
		}
		else {
			objUser.value = '';
			if (blnImageWarnings)
				objMessage.innerHTML = '<img src="images/buttons/username_morethan4.gif">';
			else
				alert("Please enter more than 4 characters.");
		}
	}
}

function handleResponseUserCheck() {
	var exists = false;
	if (http.readyState == 4) {
		if (http.responseText.indexOf('true') != -1)
			exists = true;
	}
	
	var objMessage = document.getElementById('spanUserMessage');
	if (exists) {
		var objUser = document.getElementById('Username');
		objUser.value = '';
		objMessage.innerHTML = '<img src="images/buttons/username_taken.gif" style="border: 0px;">';
	}
	else {
		objMessage.innerHTML = '<img src="images/buttons/username_ok.gif" style="border: 0px;">';
		document.getElementById('username_ok').value = '1';
	}		
}

function CheckConsultantCode() {
	var objMessage = document.getElementById('spanConsCodeMessage');
	var objConCode = document.getElementById('txtConCode');
	var objPlan = document.getElementById('cmbPlan');

	objMessage.innerHTML = '';
	objPlan.disabled = false;

	if (objConCode.value != ''){
		if (objConCode.value.length > 4) {
			http.open("GET", '/marea_action.asp?cmd=conspasscheck&concode=' + objConCode.value + '&nocache=' + new Date().getTime(), true);
			http.onreadystatechange = handleResponseConsultantCodeCheck;
			http.send(null);
		}
		else {
			objConCode.value = '';
		}
	}
}

function handleResponseConsultantCodeCheck() {
	if (http.readyState == 4) {
		var objMessage = document.getElementById('spanConsCodeMessage');
		var objPlan = document.getElementById('cmbPlan');
		
		var arrSplit = http.responseText.split('==');
		
		if (arrSplit[0] == "AVAILABLE") {
			for (var i = 0; i < objPlan.options.length; i++) {
				if (parseInt(objPlan.options[i].value) == parseInt(arrSplit[2])) {
					
					objPlan.selectedIndex = i;
				}
			}

			objPlan.disabled = true;
			objMessage.innerHTML = '<font color="#009900">Valid code.</font>';
		}
		else {
			var objConCode = document.getElementById('txtConCode');
			objConCode.value = '';
			objMessage.innerHTML = '<font color="#FF0000">Invalid consultant code.</font>';		
		}
	}
}

function activateTailoredSMS(bln) {
		document.getElementById('cmbSMSBarley').disabled = bln;
		document.getElementById('chkSMSIncludeEndUser').disabled = bln;
		document.getElementById('trTailorSMS').style.display = (bln ? 'none' : '');
}


function activatePostalAddr(bln) {
		document.getElementById('txtPostStreet').disabled = bln;
		document.getElementById('txtPostSuburb').disabled = bln;
		document.getElementById('cmbPostState').disabled = bln;
		document.getElementById('txtPostPostcode').disabled = bln;
}



/* ************************************************************
Created: 20060120
Author:  Steve Moitozo <god at zilla dot us> -- geekwisdom.com
Description: This is a quick and dirty password quality meter 
		 written in JavaScript so that the password does 
		 not pass over the network.
License: MIT License (see below)
Modified: 20060620 - added MIT License
Modified: 20061111 - corrected regex for letters and numbers
                     Thanks to Zack Smith -- zacksmithdesign.com
Modified: 20100616 - changed scoring system
---------------------------------------------------------------
Copyright (c) 2006 Steve Moitozo <god at zilla dot us>
 
Permission is hereby granted, free of charge, to any person 
obtaining a copy of this software and associated documentation 
files (the "Software"), to deal in the Software without 
restriction, including without limitation the rights to use, 
copy, modify, merge, publish, distribute, sublicense, and/or 
sell copies of the Software, and to permit persons to whom the 
Software is furnished to do so, subject to the following 
conditions:
 
   The above copyright notice and this permission notice shall 
be included in all copies or substantial portions of the 
Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY 
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 
OR OTHER DEALINGS IN THE SOFTWARE. 
---------------------------------------------------------------
 
 
Password Strength Factors and Weightings
 
password length:
level 0 (3 point): less than 4 characters
level 1 (6 points): between 5 and 7 characters
level 2 (12 points): between 8 and 15 characters
level 3 (18 points): 16 or more characters
 
letters:
level 0 (0 points): no letters
level 1 (5 points): all letters are lower case
level 2 (7 points): letters are mixed case
 
numbers:
level 0 (0 points): no numbers exist
level 1 (5 points): one number exists
level 1 (7 points): 3 or more numbers exists
 
special characters:
level 0 (0 points): no special characters
level 1 (5 points): one special character exists
level 2 (10 points): more than one special character exists
 
combinatons:
level 0 (1 points): letters and numbers exist
level 1 (1 points): mixed case letters
level 1 (2 points): letters, numbers and special characters 
					exist
level 1 (2 points): mixed case letters, numbers and special 
					characters exist
 
 
NOTE: Because I suck at regex the code might need work
	  
NOTE: Instead of putting out all the logging information,
	  the score, and the verdict it would be nicer to stretch
	  a graphic as a method of presenting a visual strength
	  guage.
 
************************************************************ */
function testPassword(passwd)
{
		var intScore   = 0
		var strVerdict = "weak"
		var strLog     = ""
		
		// PASSWORD LENGTH
		if (passwd.length>4 && passwd.length<8) // length between 5 and 7
		{
			//intScore = (intScore+6)
			intScore = (10)
 
			//strLog   = strLog + "6 points for length (" + passwd.length + ")\n"
		}
		else if (passwd.length>7/* && passwd.length<16*/)// length between 8 and 15
		{
			//intScore = (intScore+12)
			intScore = (intScore+10)
			//strLog   = strLog + "12 points for length (" + passwd.length + ")\n"
		}
		/*else if (passwd.length>15)                    // length 16 or more
		{
			//intScore = (intScore+18)			
			//strLog   = strLog + "18 point for length (" + passwd.length + ")\n"
		}*/
		
				
		// NUMBERS
		if (passwd.match(/\d+/))                                 // [verified] at least one number
		{
			//intScore = (intScore+5)
			intScore = (intScore+1)
			//strLog   = strLog + "5 points for at least one number\n"
		}
		
		if (passwd.match(/(.*[0-9].*[0-9].*[0-9])/))             // [verified] at least three numbers
		{
			//intScore = (intScore+5)
			intScore = (intScore+1)
			//strLog   = strLog + "5 points for at least three numbers\n"
		}
		
		
		// SPECIAL CHAR
		if (passwd.match(/.[!,@,#,$,%,^,&,*,?,_,~]/))            // [verified] at least one special character
		{
			//intScore = (intScore+5)
			intScore = (intScore+1)
			//strLog   = strLog + "5 points for at least one special char\n"
		}
		
									 // [verified] at least two special characters
		/*if (passwd.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/))
		{
			//intScore = (intScore+5)
			intScore = (intScore+1)
			//strLog   = strLog + "5 points for at least two special chars\n"
		}*/
	
		
		// COMBOS
		if (passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))        // [verified] both upper and lower case
		{
			//intScore = (intScore+2)
			intScore = (intScore+1)
			//strLog   = strLog + "2 combo points for upper and lower letters\n"
		}
 
		if (passwd.match(/([a-zA-Z])/) && passwd.match(/([0-9])/)) // [verified] both letters and numbers
		{
			//intScore = (intScore+2)
			intScore = (intScore+1)
 
			//strLog   = strLog + "2 combo points for letters and numbers\n"
		}
 
									// [verified] letters, numbers, and special characters
		if (passwd.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/))
		{
			//intScore = (intScore+2)
			intScore = (intScore+1)
			//strLog   = strLog + "2 combo points for letters, numbers and special chars\n"
		}
	
	
		/*if(intScore < 16)
		{
		   strVerdict = "very weak"
		}
		else if (intScore > 15 && intScore < 25)
		{
		   strVerdict = "weak"
		}
		else if (intScore > 24 && intScore < 35)
		{
		   strVerdict = "mediocre"
		}
		else if (intScore > 34 && intScore < 45)
		{
		   strVerdict = "strong"
		}
		else
		{
		   strVerdict = "stronger"
		}*/
	
	//document.forms.passwordForm.score.value = (intScore)
	//document.forms.passwordForm.verdict.value = (strVerdict)
	//document.forms.passwordForm.matchlog.value = (strLog)
	return intScore;
}


function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}


var http = getHTTPObject(); // We create the HTTP Object
