var HAS_SUBMIT = 0;

function gender(g)
{
	if(document.create1.male.checked)
	{
		if(g == 'male')
		{
			document.create1.female.checked = 0;
			document.create1.female.value = 0;
			document.create1.male.value = 1;
		}
	} else {
		document.create1.male.value = 0;
	}
	if(g == 'female') {
		document.create1.male.checked = 0;
		document.create1.male.value = 0;
		if(document.create1.female.checked){ document.create1.female.value = 1; }
		else{ document.create1.female.value = 0; }
	}
}
function hasspace(word)
{
	//alert("in hasspace = "+word);
	w = new String(word);
	if(w.match(/ /)){ return 1; }
	else { return 0; }

}

function radioSelected(r)
{
	var chk = false;
	if( r.length > 0 )
	{
		for( var i=0, chk=0; i<r.length && !chk; i++ )
			chk = r[i].checked;
	}
	else
	{
		chk = r.checked;
	}
	return chk;
}

function checkZip(zipVal, fldName, countryVal)
{
	var err = '';
	var bogus_zip_fmt = validate_postal_code( zipVal, countryVal );
	if( bogus_zip_fmt < 0 )
	{
		switch( bogus_zip_fmt)
		{
			case -10: //# blank - covered above
			  break;

			case -20: err = "- "+fldName+" contains invalid characters.\n";
 			  break;

			case -30:
			// fall thru
			default:
	  			err += "- Invalid "+fldName+" format.\n";
				break;
		}
	}
	return err;
}

function checkPhone(phoneVal, fldName, countryVal)
{
	var err = '';
	var bogus_phone_fmt = validate_phone_number( phoneVal, countryVal );
	if( bogus_phone_fmt < 0 )
	{
		switch( bogus_phone_fmt)
		{
			case -10: //# blank - covered above
			  break;

			case -20: err = "- "+fldName+" contains invalid characters.\n";
 			  break;

			case -30:
			// fall thru
			default:
	  			err += "- Invalid "+fldName+" format.\n";
				break;
		}
	}
	return err;
}


function checkLogin( fldVal, fldName )
{
	var err = '';
	if( fldVal.length < 3 || fldVal.length > 33 )
	{
		err += "- "+fldName+" needs to be between 3 and 32 characters\n";
	}
	if( validate_password(fldVal) == 20)
	{
		err +="- Character Prohibited in "+fldName+" \' \\\" \n";
	}
	return err;
}

function validatestep1(f)
{
	var msg;
	var empty_fields = "";
	var errors = "";
	var bogus_email_fmt = "";
	var tmpStr;

	for (var i = 0; i < f.elements.length; i++)
	{
		var e = f.elements[i];

		if( (e.type != "hidden")  && (e.optional == false) )
		{
			//alert(e.name+":  "+e.value+".  optional = "+e.optional);

			if( ((e.type == "text") || (e.type == "textarea") || (e.type == "password")) )
			{
				// Make sure something was entered in the field and that it contained something more than
				// just spaces.
				tmpStr = e.value;
				tmpStr = tmpStr.replace (/ /g, "");  // strip spaces

				//check if field is empty
				if ((tmpStr == null) || (tmpStr == "") || (tmpStr == "\n") || (tmpStr == "\t"))
				{
					empty_fields += "\n        " + e.pretty_name;
					continue;
				}
			}
			else if( e.type == "select-one" )
			{
				if( e.selectedIndex <= 0 )
				{
					empty_fields += "\n        " + e.pretty_name;
					continue;
				}
			}
		}
	}

	// CHECK THAT BILLING PLAN HAS BEEN SELECTED
	if( !radioSelected( f.billingoption ) )
		empty_fields += "\n        Payment Plan";

	// Validate domain name
	if(f.hostname.value != '')
	{
		if( f.hostname.value.match(/[^a-zA-Z0-9-\.]/)  //ONLY ALLOW 0-9 A-Z - .
			|| f.hostname.value.match(/\.{2,}/) //DONT ALLOW 2 or more ..
			|| f.hostname.value.match(/\.$/) //DONT ALLOW ABC.
			|| f.hostname.value.match(/^\./) //DONT ALLOW .ABC
			|| f.hostname.value.match(/^-/) //DONT ALLOW -ABC
			|| f.hostname.value.match(/-$/)) //DONT ALLOW ABC-
		{
			errors += "- URL contains invalid characters.\n";
		} else if( f.hostname.value == 'www') {
			errors += "- URL can not be www.\n";
		}
        } else {
		empty_fields += "\n        Company URL";
	}

	// Validate e-mail if it's there
	if( f.email )
	{
		bogus_email_fmt = validate_email_address (f.email.value);
		if( bogus_email_fmt )
		{
			switch( bogus_email_fmt )
			{
				case 10:  //# blank e-mail addresses are covered above
 				  break

				case 20: errors += "- Invalid e-mail address format.\n";
				  break

				case 30: errors += "- E-mail address contains invalid characters.\n";
				  break

				default: errors += "- Invalid e-mail address format.\n";
					  break;
			}
		}
	}

	var country_value = f.country.options[f.country.selectedIndex].value;

	// validate state 
	if (f.state)
	{
		// validate and get the cleaned up state code
		var state_check = validate_state_address( f.state.value, country_value, 1 );
		if( state_check < 0 )
		{
			switch( state_check)
			{
				case -10: //# blank - covered above
				  break;
	
				case -20: errors += "- "+f.state.pretty_name+" should be two-letter state abbreviation.\n";
	 			  break;
	
				case -30: errors += "- "+f.state.pretty_name+" should be two-letter province abbreviation.\n";
	 			  break;
	
				// fall thru
				default:
		  			errors += "- Invalid "+f.state.pretty_name+".\n";
					break;
			}
		}
		// Otherwise use the returned clean state value
		else if ( state_check != 0 )
		{
			f.state.value = state_check;
		}
	}

	// validate zip code 
	errors += checkZip( f.zip.value, f.zip.pretty_name, country_value );

	// validate phone number 
	errors += checkPhone( f.phone.value, f.phone.pretty_name, country_value );

	if(f.login.value == '')
	{
		empty_fields += "\n        User Name";
	}
	if(f.password.value == '')
	{
		empty_fields += "\n        Password";
	}
	if(f.password1.value == '')
	{
		empty_fields += "\n        Verify Password";
	}

	// CHECK FOR VALID LOGIN AND PASSWORD
	if( empty_fields == '' )
	{
		if(f.login.value.length < 3 || f.login.value.length > 33 )
		{
			errors += "- User Name needs to be between 3 and 32 characters\n";
		}
		if(f.password.value != f.password1.value)
		{
			errors +="- Passwords do not match";
		}
		var haschar2 = containsNonDigits(f.password.value);
		if(haschar2 == -1 )
		{
			errors +="- Password needs to contain a letter";
		}
	}
	// END OF CHECK PASSWORD

	if( f.ccinf.value != 0 )
	{
		if( f.cardnumber.value && f.cardtype.value ) {
			//Netscape 4.7
			if(!f.expmo.value || !f.expyr.value)
			{
				ind = f.expmo.selectedIndex;
				expmo = f.expmo[ind].value;
				ind = f.expyr.selectedIndex;
				expyr = f.expyr[ind].value;
			}
			else
			{
				expmo = f.expmo.value;
				expyr = f.expyr.value;
			}
			if( validate_credit_card( f.cardnumber.value, f.cardtype.value, expmo, expyr ) != 0 )
				errors += "- Invalid Credit Card Number";
		}
	}

	//# No Lic Agreement Selected
	if(f.acceptlic[0].checked){ f.acceptlic.value = "n"; }
	else if(f.acceptlic[1].checked){ f.acceptlic.value = "y"; }
	else
	{
		empty_fields += "\n        License Agreement";
	}

	if( f.acceptlic.value == "n" )
	{
		if( confirm("You do not accept the license agreement and would like to back out?\n\nBy selecting cancel you are accepting the license agreement.") )
		{
			window.history.back();
		} else {
			f.acceptlic.value = "y";
		}
	}

	//display errors and return false to prevent submission of form
	if( empty_fields == "" && errors == "" )
	{
		if(HAS_SUBMIT == 0)
		{
			HAS_SUBMIT = 1;
			return true;
		} else {
			return false;
		}
	}
	else
	{
		msg = "____________________________________________\n\n";
		msg += "The form was not submitted because of the following error(s):\n";
		msg += "Please correct these error(s) and resubmit.\n";
		msg += "___________________________________________\n\n";

		if( empty_fields != "" )
		{
			msg += "- The following required fields are empty:"	+ empty_fields + "\n";
		}
		if( errors != "" )
		{
			msg += "\n";
			msg += errors;
		}
		alert(msg);
		return false;
	}
}
