var xmlhttp
var progress_bar = new Image();
	progress_bar.src = 'http://www.avidrc.com/images/load.gif';

function notifyBegin(form) {
	if (document.getElementById("product[id]").value != '') {
		document.getElementById("beginNotify").innerHTML = '<div class="notifyBlock"><input type="hidden" name="productid2" id="productid2" value="'+document.getElementById("product[id]").value+'" /><h2>Text / Email notification</h2>By adding your phone # (USA only) or email address below, we will send a friendly text and/or email update only once when the stock levels of this product change.<div class="product_row">Email: <input type="input" name="email2" id="email2" class="input_notify" value="'+document.getElementById("email").value+'" /></div><div class="product_row">Phone: <input type="input" name="text2" id="text2" class="input_notify" value="'+document.getElementById("text").value+'" /></div><div class="orange_button_big" style="_margin-top: 5px;"><input name="notifySubmit" type="button" onclick="notifyAdd(document.notify);" value="Notify Me" style="padding-bottom: 2px;"></div></div>';
	}
}

			
			



function validatePhoneNumber(elementValue){
	var phoneNumberPattern = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
	return phoneNumberPattern.test(elementValue);
}

// Just need a super simple email check
function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}


function notifyAdd(form) {
	var selectResults = '';
	var errorResults = '';

	var reg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9]+([.-]?[a-zA-Z0-9]+)?([\.]{1}[a-zA-Z]{2,4}){1,4}$/;
	var address = document.getElementById("email2").value;
	var sms = document.getElementById("text2").value;
	if(address != '') {
		if(isValidEmail(address) == false) {
			errorResults += 'Please enter a valid email address.  ';
		}
	}
	if(sms != '') {
		if(validatePhoneNumber(sms) == false) {
			errorResults += 'Please enter a valid phone number (USA Only).  ';
		}
	}
	if((errorResults != '' || ((sms == '') && (address == '')))) {
		if(errorResults == '') {
			 errorResults += 'Stop messing around and enter something!';
		}
		document.getElementById("beginNotify").innerHTML = '<div class="notifyBlock"><input type="hidden" name="productid2" id="productid2" value="'+document.getElementById("productid2").value+'" /><h2>Text / Email notification error</h2><font color="#FF0000">'+errorResults+'</font>.<div class="product_row">Email: <input type="input" name="email2" id="email2" class="input_notify" value="'+document.getElementById("email2").value+'" /></div><div class="product_row">Phone: <input type="input" name="text2" id="text2" class="input_notify" value="'+document.getElementById("text2").value+'" /></div><div class="orange_button_big" style="_margin-top: 5px;"><input name="notifySubmit" type="button" onclick="notifyAdd(document.notify);" value="Notify Me" style="padding-bottom: 2px;"></div></div>';
	} else {
		selectResults = document.getElementById("productid2").value+'|'+document.getElementById("email2").value+'|'+document.getElementById("text2").value;
		prodNotify(selectResults);
	}
}


function prodNotify(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }

var url="/ajax/notify/notify.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChangedNotified;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}




function stateChangedNotified()
{
	if(xmlhttp.readyState == 4){
	  document.getElementById("txtNotify").innerHTML = '<div class="notifyBlock"><h2>You are all set!</h2>'+xmlhttp.responseText;
	} else {
	  document.getElementById("txtNotify").innerHTML= '<img style="margin-left:5px" src="http://www.avidrc.com/images/load.gif">';
	}
}



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;
}
