// Confirm
function really(url) {
    if (confirm("Are you sure?")) location.href = url;
}
// Close window and re-open liveyoungernow.info
function leaveWellnessProfile() {
	self.close();
}
// FORM VALIDATION FUNCTION
// http://www.leigeber.com/2008/04/dynamic-inline-javascript-form-validation/
function validateWellness() {
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  // page 1
  if(document.wellness.firstname.value == "") {
    inlineMsg('firstname','Please enter your first name.',2);
    return false;
  }
  if(!document.wellness.firstname.value.match(nameRegex)) {
    inlineMsg('firstname','You have entered an invalid first name.',2);
    return false;
  }
  if(document.wellness.lastname.value == "") {
    inlineMsg('lastname','Please enter your last name.',2);
    return false;
  }
  if(!document.wellness.lastname.value.match(nameRegex)) {
    inlineMsg('lastname','You have entered an invalid last name.',2);
    return false;
  }
  if(document.wellness.address.value == "") {
    inlineMsg('address','Please enter an address.',2);
    return false;
  }
  if(document.wellness.city.value == "") {
    inlineMsg('city','Please enter a city',2);
    return false;
  }
  if(document.wellness.state.value == "") {
    inlineMsg('state','Please enter a state.',2);
    return false;
  }
  if(document.wellness.zip.value == "") {
    inlineMsg('zip','Please enter a zip code.',2);
    return false;
  }
  if(document.wellness.phone.value == "") {
    inlineMsg('phone','Please enter a zip code.',2);
    return false;
  }
  if(document.wellness.email.value == "") {
    inlineMsg('email','<strong>Error!</strong> Please enter an email.',2);
    return false;
  }
  if(!document.wellness.email.value.match(emailRegex)) {
    inlineMsg('email','<strong>Error!</strong> You have entered an invalid email.',2);
    return false;
  }
  if(document.wellness.age.value == "") {
    inlineMsg('age','Please enter you age.',2);
    return false;
  }
  if(document.wellness.sex.value == "") {
    inlineMsg('sex','Please select your sex.',2);
    return false;
  }
  /*if(document.wellness.systolic.value == "") {
    inlineMsg('systolic','Please enter a systolic number.',2);
    return false;
  }
  if(document.wellness.diastolic.value == "") {
    inlineMsg('diastolic','Please enter a diastolic number.',2);
    return false;
  }
  if(document.wellness.hdl.value == "") {
    inlineMsg('hdl','Please enter a HDL number.',2);
    return false;
  }
  if(document.wellness.ldl.value == "") {
    inlineMsg('ldl','Please enter a LDL number.',2);
    return false;
  }*/
  if(document.wellness.ft.value == "") {
    inlineMsg('ft','Please enter you feet.',2);
    return false;
  }
  if(document.wellness.inch.value == "") {
    inlineMsg('inch','Please enter you inches.',2);
    return false;
  }
  if(document.wellness.weight.value == "") {
    inlineMsg('weight','Please enter you weight.',2);
    return false;
  }
  if(document.wellness.medications.value == "") {
    inlineMsg('medications','Please enter any medications or NA if none.');
    return false;
  }
  // END
  return true;
}

// function test for radio buttons

function validateWellnessAllRadio(rbform, rbnum) {
	// check all rb radio buttons
	for (var i = 1; i <= rbnum; i++) {
		if (! getCheckedRadioValue(rbform["rb"+i])) {
			inlineMsg('allhidden','<strong>Error!</strong> You must answer ALL questions.',2);
			return false;
		}
	}
	// add other checks here...
	return true;
}
function getCheckedRadioValue(radiob) {
	for (var i=0; i < radiob.length; i++) {
		if (radiob[i].checked) return radiob[i].value
	}
	return false;
}

// Validate ADD COMPANY

function validateAddCompany() {
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  var spaceRegex = /\s/g;
  // page 1
  if(document.add_co_form.co_id_add.value == "") {
    inlineMsg('co_id_add','Please enter a WEB ID.',2);
    return false;
  }
  if(spaceRegex.test(document.add_co_form.co_id_add.value)) {
    inlineMsg('co_id_add','<strong>Error!</strong> WEB ID cannot contain spaces.',2);
    return false;
  }
  if(document.add_co_form.form_co_name.value == "") {
    inlineMsg('form_co_name','Please enter a company name.',2);
    return false;
  }
  if(document.add_co_form.form_co_addr.value == "") {
    inlineMsg('form_co_addr','Please enter an address.',2);
    return false;
  }
  if(document.add_co_form.form_co_city.value == "") {
    inlineMsg('form_co_city','Please enter a city',2);
    return false;
  }
  if(document.add_co_form.form_co_state.value == "") {
    inlineMsg('form_co_state','Please enter a state.',2);
    return false;
  }
  if(document.add_co_form.form_co_zip.value == "") {
    inlineMsg('form_co_zip','Please enter a zip code.',2);
    return false;
  }
  if(document.add_co_form.form_co_contact_name.value == "") {
    inlineMsg('form_co_contact_name','Please enter a contact name.',2);
    return false;
  }
  if(document.add_co_form.form_co_phone.value == "") {
    inlineMsg('form_co_phone','Please enter a contact phone number.',2);
    return false;
  }
  if(document.add_co_form.form_co_email.value == "") {
    inlineMsg('form_co_email','<strong>Error!</strong> Please enter an email.',2);
    return false;
  }
  if(!document.add_co_form.form_co_email.value.match(emailRegex)) {
    inlineMsg('form_co_email','<strong>Error!</strong> You have entered an invalid email.',2);
    return false;
  }
  if(document.add_co_form.form_co_www.value == "") {
    inlineMsg('form_co_www','Please enter a website address.',2);
    return false;
  }
  /*if(document.add_co_form.form_co_logo_upload.value == "") {
    inlineMsg('form_co_logo_upload','Please select a logo to upload.',2);
    return false;
  }*/
  if(document.add_co_form.form_co_product_link.value == "") {
    inlineMsg('form_co_product_link','Please enter the Shaklee product link',2);
    return false;
  }
  if(document.add_co_form.form_co_product_link.value == "EXAMPLE: http://XXXXXX.myshaklee.com/us/en/") {
    inlineMsg('form_co_product_link','Please enter the Shaklee product link',2);
    return false;
  }
  if(document.add_co_form.form_co_admin_email.value == "") {
    inlineMsg('form_co_admin_email','<strong>Error!</strong> Please enter an email.',2);
    return false;
  }
  if(!document.add_co_form.form_co_admin_email.value.match(emailRegex)) {
    inlineMsg('form_co_admin_email','<strong>Error!</strong> You have entered an invalid email.',2);
    return false;
  }
  if(document.add_co_form.form_co_admin_password.value == "") {
    inlineMsg('form_co_admin_password','Please enter a password.');
    return false;
  }
  if(document.add_co_form.form_co_admin_password_v.value == "") {
    inlineMsg('form_co_admin_password_v','Please verify your password.');
    return false;
  }
  if(document.add_co_form.form_co_admin_password_v.value != document.add_co_form.form_co_admin_password.value) {
	inlineMsg('form_co_admin_password_v','Passwords do not match! Please re-enter your passwords.');
    return false;
  }
  // END
  return true;
}

// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 5;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "http://www.liveyoungernow.info/global/images/msg_arrow.gif"; 
}

// Validate WEB ID
var xmlhttp;

function validateCompanyID(str)
{
	// check for spaces in id
	var spaceRegex = /\s/g;
	if(spaceRegex.test(document.add_co_form.co_id_add.value)) {
		inlineMsg('co_id_add','<strong>Error!</strong> WEB ID cannot contain spaces.',2);
		return false;
  	}
	
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="global/validateCompanyId.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}