// User account script
// Includes zipcode checking, form checking

var ZipValid = false;
var PrevZip = "";

function CheckZip(e) {
  e = false;
  if (document.rewardsform) {
    if (document.rewardsform.zip) {
      e = document.rewardsform.zip;
    }
  }
  if (e == false) {
    PrevZip = "";
    return;
  }
  if (e.value != PrevZip) {
    ZipValid = false;
    PrevZip = e.value;
    if (e.value.match(/^\d\d\d\d\d$/)) {
      SetZipText("Verifying...");
      frames['zipIF'].document.location.replace('verifyzip.php?zip='+e.value);
    } else {
      SetZipText("");
    }
  }
}

function VerifiedZip(r) {
  if (r == false) {
    SetZipText("Not on file");
  } else {
    ZipValid = true;
    SetZipText(r);
  }
}

PrevZipText = "";
function SetZipText(t) {
  e = document.getElementById("zipdesc");
  if (e) {e.innerHTML = t;}
  PrevZipText = t;
}

function CheckAvail(e, NeedsZip) {
  if (e.checked) {
    EmailValid = document.forms[0].email.value.match(/.\@.+\..+/);
    if (EmailValid) {
      if (NeedsZip) {
	if (ZipValid == false) {
	  e.checked = false;
	  alert("A valud US Zip Code is required.");
	}
      }
    } else {
      e.checked = false;
      alert("A valid email address is required.");
    }
  }
}

window.setInterval("CheckZip(false)", 500);


// Info window

var InfoBox = false;

function ExpandInfo(text, element) {
  InfoBox = document.getElementById("infobox");
  if (!(InfoBox)) {return;}

  x = element.offsetLeft;
  y = element.offsetTop;
  while (element.offsetParent) {
    element = element.offsetParent;
    x += element.offsetLeft;
    y += element.offsetTop;
  }
  gap = 25
  if (x<150) {x=150;}
  InfoBox.style.width = ""+(x-gap)+"px";
  InfoBox.style.left = gap+"px";
  InfoBox.style.top = ""+(y-10)+"px";
  InfoBox.style.visibility = "visible";
  InfoBox.innerHTML = "<div class=\"segment\">"+text+"</div>";
}

function CollapseInfo() {
  if (InfoBox) {
    InfoBox.style.visibility = "hidden";
    InfoBox.innerHTML = "";
  }
}


//Collapsable form sections

var Values = new Object;
var FoundSections = false;

window.setTimeout("CollapseInit()",500);

function CollapseInit() {
  if (!(document)) {
    window.setTimeout("CollapseInit()",500);
    return;
  }
  CheckCollapse();
  RefreshAll();
}

function CheckCollapse(recursion) {
  f = false;
  if (document) {
    if (document.rewardsform) {
      f = document.rewardsform;
    }
  }
  if (f == false) {return;}

  es = document.getElementsByTagName("TD");

  if (FoundSections == false) {
    FoundSections = true;

    for (id in Expands) {
      Expands[id].Sections = new Object();
      Expands[id].regex = new RegExp("^"+id);

      for (n=0; n<es.length; n++) {
	e = es[n];
	name = ""+e.id;
	if (name.match(Expands[id].regex)) {
	  Expands[id].Sections[name] = e.innerHTML;
	}
      }
    }
  }

  for (n=0; n<f.elements.length; n++) {
    name = f.elements[n].name;
    if (f[name]) {
      Values[name] = {value: f[name].value, checked: f[name].checked, selectedIndex: f[name].selectedIndex};
    }
  }
  if (f.HomeRange) {
    Values.rangeid = f.HomeRange.selectedIndex;
  }

  region = "";
  if (f.region) {
    region = f.region.options[f.region.selectedIndex].value;
  }
  for (id in Expands) {
    for (n=0; n<es.length; n++) {
      e = es[n];
      name = ""+e.id;
      if (name.match(Expands[id].regex)) {
	e.innerHTML = Expands[id].check()?Expands[id].Sections[name]:"";
	e.style.paddingTop = Expands[id].check()?"":"0";
	e.style.paddingBottom = Expands[id].check()?"":"0";
      }
    }
    for (n in Values) {
      if (f[n]) {
	for (arg in Values[n]) {
	  f[n][arg] = Values[n][arg];
	}
      }
    }
  }

  SetZipText(PrevZipText);
  RefreshAll();

  if (recursion > 1) {
    CheckCollapse(recursion-1)
  }
}

var optionals = /^(ShipName|ShipAddr|zip|Phone)$/;
var mandatories = /^(user|pw|cpw|referral|email)$/;

function RefreshAll() {
  var es = document.getElementsByTagName("INPUT");
  for (var n=0; n<es.length; n++) {
    var e = es[n];
    if (e == document.rewardsform[e.name]) {
      RefreshElement(e);
    }
  }
}

function RefreshElement(e) {
  if (e) {
    if (!(e.style)) {
      if (e.target) {e = e.target;}
    }
  } else if (event) {
    if (event.srcElement) {e = event.srcElement;}
  }

  var m = 0;
  if (e.name.match(optionals)) {m=1;}
  if (e.name.match(mandatories)) {m=2;}

  if (m) {
    e.onfocus = ClearElement;
    e.onblur = RefreshElement;
    if (e.value == "") {
      //e.style.backgroundImage = (m==1)?"url(img/form-optional.jpg)":"url(img/form-mandatory.jpg)";
      e.style.backgroundImage = (m==1)?"":"url(img/form-mandatory.jpg)";
      e.style.backgroundPosition = "bottom right";
    } else {
      e.style.backgroundImage = "";
    }
  }
}

function ClearElement(e) {
  if (e) {
    if (!(e.style)) {
      if (e.target) {e = e.target;}
    }
  } else if (event) {
    if (event.srcElement) {e = event.srcElement;}
  }

  e.style.backgroundImage = "";
}

var invalids = /[<>\"\n\t\r]/;
var userinvalids = /[^a-zA-Z0-9\-\!\_ ]/;

function CheckUserForm(f) {
  if (f.user) {
    if (f.user.value == "") {
      alert("Please provide a user name.");
      f.user.focus();
      return false;
    }
    if (f.user.value.match(userinvalids)) {
      alert("This user name include invalid characters.\nPlease use only letters, numbers and spaces.");
      f.user.focus();
      return false;
    }
    if (f.pw.value == "") {
      alert("Please provide a password.");
      f.pw.focus();
      return false;
    }
    if (f.cpw.value == "") {
      alert("Please confirm your password.");
      f.cpw.focus();
      return false;
    }
    if (f.pw.value != f.cpw.value) {
      alert("Password and Confirmed Password do not match.");
      f.pw.focus();
      return false;
    }
  }

  if (f.DOBmonth) {
    if ((f.DOBmonth.selectedIndex == 0) && (f.DOBday.selectedIndex == 0) && (f.DOByear.selectedIndex == 0)) {
      alert("Please provide the year of birth.");
      f.DOByear.focus();
      return false;
    }
  } else if (f.DOByear) {
    if (f.DOByear.selectedIndex == 0) {
      alert("Please provide your year of birth.  This is only used to determine your age for contest eligibility.");
      f.DOByear.focus();
      return false;
    }
  }

  if (f.wasreferred) {
    if (f.wasreferred.checked) {
      if (f.referral.value == "") {
	alert("Please provide the name or ID of the user who referred you, or un-check the Referral section.");
	f.referral.focus();
	return false;
      }
    }
  }

  if (f.region) {
    region = f.region.options[f.region.selectedIndex].value;
    if (region == "") {
      alert("Please choose a region or country from the pulldown.");
      f.region.focus();
      return false;
    } else if (region == "USA") {
      if (f.ShipName.value == "") {
	alert("Please provide your full name for shipping.");
	f.ShipName.focus();
	return false;
      }
      if (f.ShipAddr.value == "") {
	alert("Please provide your street address for shipping.");
	f.ShipAddr.focus();
	return false;
      }
      if (!(f.zip.value.match(/^\d\d\d\d\d$/))) {
	alert("Please provide a valid US Zip Code for shipping.");
	f.zip.focus();
	return false;
      }
    }
  }

  if (f.Phone) {
    if (f.Phone.value == "") {
      alert("Please provide a phone number you can be reached at.");
      f.Phone.focus();
      return false;
    }
  }

  if (!(f.email.value.match(/^.*\@.*\..*$/))) {
    alert("Please provide a valid email address.");
    f.email.focus();
    return false;
  }

  return true;
}


function CheckCheckbox(id) {
  if (document.rewardsform[id]) {
    return document.rewardsform[id].checked;
  }
  return false;
}
