// Support Script (776)
function AddToValidateArray(strElementName)
{
    var strName = strElementName

    if (!document.ValidateArray) 
    {
        document.ValidateArray = new Array
    }

    document.ValidateArray[document.ValidateArray.length] = strName
}

// Support Script (655)
function ValidateNonBlank()
{
  var msg = "";
  var val = this.getText();  

  if (StripChars(" \n\t\r",val).length == 0)
  {
    if (Trim(this.ErrorMsg) != "")
      msg = "Required field. " + this.ErrorMsg
    else
      msg = "Required field. Please enter an appropriate value."
  }

  return msg;
}

// Support Script (800)
function StripChars(theFilter,theString)
{
	var strOut,i,curChar

	strOut = ""
	for (i=0;i < theString.length; i++)
	{		
		curChar = theString.charAt(i)
		if (theFilter.indexOf(curChar) < 0)	// if it's not in the filter, send it thru
			strOut += curChar		
	}	
	return strOut
}

function AllInRange(x,y,theString)
{
	var i, curChar
	
	for (i=0; i < theString.length; i++)
	{
		curChar = theString.charAt(i)
		if (curChar < x || curChar > y) //the char is not in range
			return false
	}
	return true
}


function reformat (s)
{
    var arg;
    var sPos = 0;
    var resultString = "";

    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) 
           resultString += arg;
       else 
       {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}

function Trim(theString)
{
 var i,firstNonWhite

 if (StripChars(" \n\r\t",theString).length == 0 ) return ""

	i = -1
	while (1)
	{
		i++
		if (theString.charAt(i) != " ")
			break	
	}
	firstNonWhite = i
	//Count the spaces at the end
	i = theString.length
	while (1)
	{
		i--
		if (theString.charAt(i) != " ")
			break	
	}	

	return theString.substring(firstNonWhite,i + 1)

}
// Support Script (650)
function ValidateEMail()
{
   var msg = "";
   var val = this.getText();
   var msgInvalid = "Please enter a valid e-mail address\n(a valid e-mail address contains the @ character)";

  	var theLen = StripChars(" ",val).length
	  if (theLen == 0)	
		  if (!this.Required) return ""		
		  else return "Required field.  " + msgInvalid

   if (val.indexOf("@",0) < 0) 
   {
      msg = msgInvalid 
   }
   return msg;
}

// Support Script (768)
function getForm()
{
	var f;
	if (document.forms && document.forms[0])
	{
		// This is the case where there are only hidden form elements in an
		// all 4.0 browser page and Comm is hitting the page - and all IE cases.
		f = document.forms[0];
	}

	if (f == null)
	{
		if ((document.layers) && (document.layers.length > 0))
		{
			var d;
			iLayer = 0;
			while (true)
			{
				d = document.layers[iLayer].document.layers[0].document;
				if (d.forms && d.forms[0]) // normal case
				{
					f = d.forms[0];
					break;
				}
				iLayer = iLayer + 2;
			
				if (document.layers[iLayer] == null)
					break;
			}
		}
	}

	if (f == null)
		alert("Form not submitted - internal error: Drumbeat can not find Form object in DOM. There may not be a form on this page.");

	return f;
}

// Support Script (810)
function subAwithBinC(a,b,c)
{

	var i = c.indexOf(a);
	var l = b.length;

	while (i != -1)	{
		c = c.substring(0,i) + b + c.substring(i + a.length,c.length);
  i += l
		i = c.indexOf(a,i);
	}
	return c;

}
// Support Script (770)
function Validate(stopOnFailure)
{
	var ErrorMsg = "";
	var i
	var msg
	var tofocus = true;
	var ErrorMsg = "";
	
	// Go through the Validate Array that may or may not exist
	// and call the Validate function for all elements that have one.
	if (document.ValidateArray)
	{
		for (i = 0; i < document.ValidateArray.length; i ++)
		{
			msg = eval( document.ValidateArray[i] + ".Validate()")
			if (msg != "")
			{
				ErrorMsg += "\n\n" + document.ValidateArray[i] + ":  " + msg;
				if (tofocus) 
				{
					eval(document.ValidateArray[i] + ".focus()")
					tofocus = false;
				}
				
				if (stopOnFailure == "1") return ErrorMsg;
			}
  	}
  }
	return ErrorMsg;
}

// Support Script (661)

function ValidateZipCode()
{
	var msg = ""
	var theString = this.getText()
 var msgInvalid = "Please enter a valid Zip Code.\nSuch as 92008 or 92008-1337"

	var theLen = StripChars(" \n\t\r",theString).length
	if (theLen == 0)	
		if (!this.Required) return ""		
		else return "Required field.  " + msgInvalid

	theString = StripChars("- \n\r",theString)		

	if (!AllInRange("0","9",theString))
	{
		msg = msgInvalid
	}
	else if (theString.length != 5 && theString.length != 9)
	{
		msg = msgInvalid
	}

	if (msg == "")
	{
		if (theString.length == 9)		
			this.setText(reformat(theString,"",5,"-",4))
		else
   this.setText(theString)
   
	}
	return msg	
}
// Support Script (642)
function PopulateWith(TheList, Items)
{
  var iCount = TheList.getCount();
  for (var i=0; i<iCount; i++)
    if (TheList.elementID)
      TheList.elementID.options[0] = null;
    else
      alert("bad or wrong DE for state population");

  i = 0;
  iCount = Items.length;
 	for (i=0; i<iCount; i++)
    TheList.addOption(Items[i], i, "");

  TheList.setSelectedByPosition(0);
}
// Support Script (654)

function ValidateInternationalPhone()
{
	var msg = ""
	var theString = this.getText()
	
	var theLen = StripChars(" \n\t\r",theString).length
	if (theLen == 0)	
		if (!this.Required) return ""		
		else return "Required field.  Please enter an appropriate value."

	theString = StripChars("+*() -./_\n\r",theString)		

	if (!AllInRange("0","9",theString) || theString.length < 6)
	{
		msg = "Please enter a valid phone number."
	}	

	return msg		
}
function document_onLoad() {
BuyerLastName.Validate=ValidateNonBlank;
BuyerLastName.ErrorMsg = "This field is required"
AddToValidateArray("BuyerLastName")
BuyerEmail.Validate = ValidateEMail;
BuyerEmail.Required = Number("1");
AddToValidateArray("BuyerEmail")
BuyerFirstName.Validate=ValidateNonBlank;
BuyerFirstName.ErrorMsg = "This field is required"
AddToValidateArray("BuyerFirstName")
Password.Validate=ValidateNonBlank;
Password.ErrorMsg = "This field is required"
AddToValidateArray("Password")
Confirm.Validate=ValidateNonBlank;
Confirm.ErrorMsg = "This field is required"
AddToValidateArray("Confirm")
ShipAddress.Validate=ValidateNonBlank;
ShipAddress.ErrorMsg = "This field is required"
AddToValidateArray("ShipAddress")
ShipCity.Validate=ValidateNonBlank;
ShipCity.ErrorMsg = "This field is required"
AddToValidateArray("ShipCity")
ShipCountry.Validate=ValidateNonBlank;
ShipCountry.ErrorMsg = "This field is required"
AddToValidateArray("ShipCountry")
ShipZip.Validate = ValidateZipCode;
ShipZip.Required = Number("1");
AddToValidateArray("ShipZip")
var i = 0;
s = new Array(51);
s[i++] = "--"
s[i++] = "AK"
s[i++] = "AL"
s[i++] = "AZ"
s[i++] = "AR"
s[i++] = "CA"
s[i++] = "CO"
s[i++] = "CT"
s[i++] = "DE"
s[i++] = "FL"
s[i++] = "GA"
s[i++] = "HI"
s[i++] = "ID"
s[i++] = "IL"
s[i++] = "IN"
s[i++] = "IA"
s[i++] = "KS"
s[i++] = "KY"
s[i++] = "LA"
s[i++] = "ME"
s[i++] = "MD"
s[i++] = "MA"
s[i++] = "MI"
s[i++] = "MN"
s[i++] = "MS"
s[i++] = "MO"
s[i++] = "MT"
s[i++] = "NE"
s[i++] = "NV"
s[i++] = "NH"
s[i++] = "NJ"
s[i++] = "NM"
s[i++] = "NY"
s[i++] = "NC"
s[i++] = "ND"
s[i++] = "OH"
s[i++] = "OK"
s[i++] = "OR"
s[i++] = "PA"
s[i++] = "RI"
s[i++] = "SC"
s[i++] = "SD"
s[i++] = "TN"
s[i++] = "TX"
s[i++] = "UT"
s[i++] = "VT"
s[i++] = "VA"
s[i++] = "WA"
s[i++] = "WV"
s[i++] = "WI"
s[i++] = "WY"

PopulateWith(ShipState, s);
BuyerPhone.Validate = ValidateInternationalPhone;
BuyerPhone.Required = Number("0");
AddToValidateArray("BuyerPhone")
 }
function Form1_onSubmit() {
errormessage=""
if ("1"=="1")  {
  if (Confirm.getText()!=Password.getText())
    errormessage="Password and Confirmation must match"
  }
else
  if (String(Confirm.getText().toUpperCase())!=String(Password.getText().toUpperCase()))
    errormessage="Password and Confirmation must match"
if ("1"=="0") {
  if (Confirm.getText()=="")
    errormessage=""
  }
if (errormessage!="")  {
  Confirm.clear()
  Password.clear()
  Confirm.focus()
  alert(errormessage)
  return false;
}
errorMsg = Validate("0"); // don't stop on first error

if (errorMsg != "")
    alert("The form could not be submitted:" + errorMsg);

return (errorMsg == ""); // false prevents form submission
 }
function _Form1_onSubmit() { if (Form1) return Form1.onSubmit(); }
function InsertNewUser_onClick() {
// We do not call onSubmit() with this contract

var formObj = getForm()
if (formObj != null)
{
var addChar = "?" 
var j
var Result
var okToSubmit = false

if ("".length > 1)
{
    formObj.action = subAwithBinC(" ", "%20", "")
}

// We have to
// put the source in the query string so the generic database contracts
// still work.

// NOTE: this only works if the method of the form is POST

act = formObj.action
if (act.indexOf("?") != -1)
{    
    addChar = "&"
}

act += addChar + "InsertNewUser=1"
formObj.action = act


if ("1" == "1")
{
    Result = Validate("0"); // don't stop on first error
    if (Result == "") okToSubmit = true;
    else alert("The form could not be submitted:" + Result);
}
else 
{
    okToSubmit = true
}


if (okToSubmit) formObj.submit();
}
var addChar = "?" 
var j
var okToSubmit = false;

if ("".length > 1)
{
    Form1.setAction(subAwithBinC(" ", "%20", ""));
}

// execute the onSubmit() event handler and try to 
// determine if it already validated the form
Result = Form1.onSubmit();

//   If there is no onSubmithander the return value is null
//   If there is a validation handler it returns true to submit
//   or false to not submit
if (Result==null)  // there is no validation already defined
{
    if ("0" == "1")
    {
        Result = Validate("0"); // don't stop on first error
        if (Result == "") okToSubmit = true;
        else alert("The form could not be submitted:" + Result);
    }
    else okToSubmit = true;
}
else // there is a validation already defined
{
    if (Result==true)
        okToSubmit = true;
}

if (okToSubmit) 
{
    // We have to
    // put the source in the query string so the generic database contracts
    // still work.

    // NOTE: this only works if the method of the form is POST


    act = Form1.getAction();
    if (act.indexOf("?") != -1)
    {    
        addChar = "&"
    }

    act += addChar + "InsertNewUser=1"
    Form1.setAction(act);


    Form1.submit();
}
 }
function _InsertNewUser_onClick() { if (InsertNewUser) return InsertNewUser.onClick(); }

