
var xmlhttp = false;

try {
	xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");
}
catch(e) {
	try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(E) {
		xmlhttp = false;
	}
}

if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
	xmlhttp = new XMLHttpRequest();
}


function duplicateEmail(div, element, resignup) {
	serverPage = 'validateEmail.asp?email='+element+'&return='+resignup;

	var obj = document.getElementById(div);
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
}


function editVendorPmt(element, page, id, purchases_vendor, date_paid, check_number, amount) {
	serverPage = page + '?id='+id+'&purchases_vendor='+purchases_vendor+'&date_paid='+date_paid+'&check_number='+check_number+'&amount='+amount;

	var obj = document.getElementById(element);
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
	this.location.reload();
}


function editMemberPmt(element, page, id, amount) {
	serverPage = page + '?id='+id+'&amount='+amount;

	var obj = document.getElementById(element);
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
	this.location.reload();
}

function getRebatePercent(element, page, vendor) {
	serverPage = page + '?vendor='+vendor;
	
	var obj_percent = document.getElementById(element);
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj_percent.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);	
}

function calculateRebates(monthly, annual, amount, percent, total_amount, total_monthly, total_annual) {
	if(amount != '')
	{
		var obj_monthly = document.getElementById(monthly);
		var obj_annual = document.getElementById(annual);
		var obj_total_purchases = document.getElementById('total_member_purchases');
		var obj_total_monthly = document.getElementById('total_monthly_rebates');
		var obj_total_annual = document.getElementById('total_annual_rebates');
		
		var monthly_rebate = roundNumber( (parseFloat(amount) * (parseFloat(percent)/100)), 2 );
		var annual_rebate = roundNumber( (parseFloat(monthly_rebate) * 12), 2 );
		var total_purchases = roundNumber( (parseFloat(total_amount.substr(1)) + parseFloat(amount)), 2 );
		var total_monthly = roundNumber( (parseFloat(total_monthly.substr(1)) + monthly_rebate), 2 );
		var total_annual = roundNumber( (parseFloat(total_annual.substr(1)) + annual_rebate), 2 );
		
		obj_monthly.innerHTML = '$' + monthly_rebate;
		obj_annual.innerHTML = '$' + annual_rebate;
		obj_total_purchases.innerHTML = '$' + total_purchases;
		obj_total_monthly.innerHTML = '$' + total_monthly;
		obj_total_annual.innerHTML = '$' + total_annual;
	}
}

function getContentImage(element, page, url) {
	serverPage = page + '?url='+url;
	
	var obj = document.getElementById(element);
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);	
}

function roundNumber(num, dec) 
{
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}


// This script and many more are available free online at 
// The JavaScript Source!! http://javascript.internet.com
function formatCurrency(num) 
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function checkInputDetails(form) 
{
   if(form.fname.value.length==0) {
      alert('You must provide your first name');
      form.fname.focus();
      return false;
   }
   if(form.lname.value.length==0) {
      alert('You must provide your last name');
      form.lname.focus();
      return false;
   }
   if(!check_email(form.email.value)) {
      alert('You must provide a valid email address in the form someone@somewhere.ext');
      form.email.focus();
      return false;
   }
   if(form.password.value.length < 5) {
      alert('You must provide a password of at least 5 characters');
      form.password.focus();
      return false;
   }
   if(form.password.value!=form.confirmpassword.value) {
      alert('The password and it\'s confirmation do not match');
      form.password.focus();
      return false;
   }
   /*
   if((form.Industry1.checked==false)&&(form.Industry2.checked==false)) {
		alert('You must check at least one Industry');
		form.Industry1.focus();
		return false;
	}
	*/
   return true;
}

function check_email(e) 
{
   if(e.length==0) 
      return false; 
   else 
      return true;
   ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
   for(i=0; i<e.length ;i++) {
      if(ok.indexOf(e.charAt(i))<0) { 
         return (false);
      }
   }
   if(document.images) {
      re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
      re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
      if (!e.match(re) && e.match(re_two)) {
         return (-1);		
      } 
   }
}

function chooseMailingDetails(element, item) {
	serverPage = 'fulfillment_details.asp?item='+item;
	var obj = document.getElementById(element);
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
}
