function getXmlHttpRequestObject(){
  var xhr;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xhr = false;
      }
    }
  @else
  xhr = false;
  @end @*/
  if (!xhr && typeof XMLHttpRequest != 'undefined') {
    try {
      xhr = new XMLHttpRequest();
    } catch (e) {
      xhr = false;
    }
  }
  return xhr;
}

function display_email(){
	
	var url = document.location.href;
	
	var section1 = '<div id="email_header"><div id="email_header_left">Email-A-Friend</div><div id="email_header_right" onclick="close_email()"></div></div><div id="email_contents"><center>Complete the details below to send a link to this page.</center><br>';
	
	var section2 = '<form name="tellafriend" action="" method="post" onsubmit="return false;" style="margin-top:0px">&nbsp;<table align="center"><tr><td>*Your name:</td><td><input name="name" value="Your Name" maxlength="45" id="djcustom_your_name" class="djcustom_input" onkeyup="update_message()"></td></tr><tr><td>*Your email:</td><td><input name="email" id="djcustom_your_email" value="your@email.com" maxlength="45" class="djcustom_input" onkeyup="update_message()"></td></tr><tr><td colspan="2"><p align="center" style="margin-top:10px">Enter email addresses:</p></td></tr><tr><td>*Email 1:</td><td><input name="djcustom_ema1" id="djcustom_ema1" maxlength="50" class="djcustom_input"></td></tr><tr><td>Email 2:</td><td><input name="djcustom_ema2" id="djcustom_ema2" maxlength="50" class="djcustom_input"></td></tr><tr><td>Email 3:</td><td><input name="ema3" id="djcustom_ema3" maxlength="50" class="djcustom_input"></td></tr><tr><td colspan="2"><p align="center">The following message will be sent to your recipients.<br /><textarea name="send_email" readonly="readonly" id="djcustom_message" >Your Name, whose email address is email@email.com thought you may be interested in visiting the following page of our website.\n\n';
	
	var section3 = '\n\nYour Name has used our Tell-a-Friend form to send you this email.\n\nWe look forward to your visit.</textarea><br /><br /><input type="button" onclick="close_email()" value="Cancel" style="width:120px; margin-right:15px;" /><input onclick="djcustom_send()" type="button" value="Send Email" style="width:120px;" /></td></tr></table></form></div>';

	document.getElementById('email_form').innerHTML = section1 + url + section2 + url + section3;
	document.getElementById('djcustom_shadow').style.display = 'block';
	document.getElementById('email_form').style.display = 'block';
}

function djcustom_send(){
	if (document.getElementById('djcustom_your_name').value=="" || document.getElementById('djcustom_your_name').value=="Your Name"){
		alert('Please enter your name before submitting the form');
		return false;
	}
	if (document.getElementById('djcustom_your_email').value=="" || document.getElementById('djcustom_your_email').value=="your@email.com"){
		alert('Please enter your email address before submitting the form');
		return false;
	}
	if (document.getElementById('djcustom_ema1').value==""){
		alert('Please enter atleast one email address before submitting the form');
		return false;
	}
	var djcustom_your_name = document.getElementById('djcustom_your_name').value;
	var djcustom_your_email = document.getElementById('djcustom_your_email').value;
	var djcustom_ema1 = document.getElementById('djcustom_ema1').value;
	var djcustom_ema2 = document.getElementById('djcustom_ema2').value;
	var djcustom_ema3 = document.getElementById('djcustom_ema3').value;
	document.getElementById('email_form').innerHTML = '<br><br><br><br><br><br><br><br><br><br><img src="http://www.djcustomdesign.com/images/loading.gif"><br />Loading';

	var get_obj = getXmlHttpRequestObject();
	get_obj.open('POST', 'http://www.valleyofthestarz.com/tell_a_friend.php', true);
	get_obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	get_obj.onreadystatechange = function(){
		if(get_obj.readyState == 4){
			var resp = get_obj.responseText;
			if(resp.charAt(0) == 0){
				document.getElementById('email_form').innerHTML = '<h3 align="center">An Error Has Occured</h3> You have either entered a false email address or you are attempting to hack our system.  We do not except forms being submitted falsly or attempts against our site.<br /><br /> If this was by error please close the window and click the button again.<br /><br /><input type="button" value="Close Window" onclick="close_email()">';	
			}
			else{
				document.getElementById('email_form').innerHTML = '<br><br><br><br><br><h3 align="center">Emails Sent</h3>Thank You for suggesting our site!<br>Please tell the world.<br><br><center><input type="button" onclick="close_email()" value="Close Window" /></center>';	
			}
		}
	}
	var post_params = 'name=' + djcustom_your_name;
	post_params += '&email=' + djcustom_your_email;
	post_params += '&ema1=' + djcustom_ema1;
	post_params += '&ema2=' + djcustom_ema2;
	post_params += '&ema3=' + djcustom_ema3;
	post_params += '&refurl=' + document.location.href;
	get_obj.send(post_params);
}

function close_email(){
	document.getElementById('email_form').style.display = 'none';
	document.getElementById('djcustom_shadow').style.display = 'none';
}

function update_message(){
	var x = document.getElementById('djcustom_message');
	var url = document.location.href;
	var y = ', whose email address is ';
	var w = ' thought you may be interested in visiting the following page of our website.\n\n';
	var z = ' has used our Tell-a-Friend form to send you this note.\n\nWe look forward to your visit.';
	var name = document.getElementById('djcustom_your_name').value;
	var email = document.getElementById('djcustom_your_email').value;
	
	x.value = name + y + email + w + url + '\n\n' + name + z;
}