function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){request_type = new ActiveXObject("Microsoft.XMLHTTP");}
else{request_type = new XMLHttpRequest();}
return request_type;
}

var http = createObject();
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;

//function leads_update 
function leads_update(lead_id) {

 	if(!document.getElementById('update_chkbx_'+lead_id).checked){
		var reason = "";
		reason += validateDate(document.getElementById('appt_date_'+lead_id));
		if (document.getElementById('appt_date_'+lead_id).value != "" && document.getElementById('appt_date_'+lead_id).value != "0000-00-00")
		reason += validateEmpty(document.getElementById('appt_time_'+lead_id),"Please enter Appt Time");
		reason += validateDate(document.getElementById('contact_date_'+lead_id));
		reason += validateDate(document.getElementById('sale_date_'+lead_id));
	
		if (reason != "") {
			alert("Some fields need correction:\n" + reason);
			}
		else {
			leads_update_basic(lead_id);
			}
		}	
	else {
		var reason = "";
		reason += validateEmpty(document.getElementById('fname_'+lead_id),"Please enter First Name");
		reason += validateEmpty(document.getElementById('lname_'+lead_id),"Please enter Last Name");
		
		if (document.getElementById('email_'+lead_id).value == "" && document.getElementById('phone_'+lead_id).value == "")
		  {
		  reason += "You must enter either a Phone Number or an Email Address.\n";
		  document.getElementById('phone_'+lead_id).style.background = 'Yellow'; 
		  document.getElementById('email_'+lead_id).style.background = 'Yellow';
		  }
		  else
		  {
			  if (document.getElementById('email_'+lead_id).value != "")
			  {
			  reason += validateEmail(document.getElementById('email_'+lead_id));
			  }
			  else
			  {
			  document.getElementById('email_'+lead_id).style.background = 'White';
			  }
			  
			  if (document.getElementById('phone_'+lead_id).value != "")
			  {
			  reason += validatePhone(document.getElementById('phone_'+lead_id));
			  }
			  else
			  {
			  document.getElementById('phone_'+lead_id).style.background = 'White';
			  }
		  }
		
		//reason += validateEmail(document.getElementById('email_'+lead_id));
		//reason += validatePhone(document.getElementById('phone_'+lead_id));
		
		reason += validateDate(document.getElementById('appt_date_'+lead_id));
		if (document.getElementById('appt_date_'+lead_id).value != "" && document.getElementById('appt_date_'+lead_id).value != "0000-00-00")
		reason += validateEmpty(document.getElementById('appt_time_'+lead_id),"Please enter Appt Time");
		reason += validateDate(document.getElementById('contact_date_'+lead_id));
		reason += validateDate(document.getElementById('sale_date_'+lead_id));

	
		if (reason != "") {
			alert("Some fields need correction:\n" + reason);
			}
		else {
			leads_update_detail(lead_id);
			}
		}
}

function leads_update_basic(lead_id) {

var client_id = encodeURI(document.getElementById('client_id_'+lead_id).value);
var status = encodeURI(document.getElementById('status_'+lead_id).value);
var sale_date = encodeURI(document.getElementById('sale_date_'+lead_id).value);
var appt_date = encodeURI(document.getElementById('appt_date_'+lead_id).value);
var appt_time = encodeURI(document.getElementById('appt_time_'+lead_id).value);
var contact_date = encodeURI(document.getElementById('contact_date_'+lead_id).value);
var contact_time = encodeURI(document.getElementById('contact_time_'+lead_id).value);
var comments = encodeURI(document.getElementById('comments_'+lead_id).value);

// Set the random number to add to URL request
nocache = Math.random();

var params = 'lead_id='+lead_id+'&client_id='+client_id+'&status='+status+'&sale_date='+sale_date+'&appt_date='+appt_date+'&appt_time='+appt_time+'&contact_date='+contact_date+'&contact_time='+contact_time+'&comments='+comments+'&nocache='+nocache;

document.getElementById('form_feedback_'+lead_id).innerHTML = '&nbsp;';

if (confirm('Are you sure you want to update this lead?')){


	// Pass the login variables like URL variable
	//http.open('get', 'sql/sql_leads_update_basic.php?'+params, false);
	http.open('post', 'sql/sql_leads_update_basic.php', false);
	
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	
	//http.onreadystatechange = insertReply;
	http.send(params);
	
	if(http.readyState == 4){
	
		var d0 = new Date();
		var d1 = d0.getYear()+''+(d0.getMonth()+1)+''+d0.getDate();
		var d2 = appt_date.replace(/-0/g,'');
		var d3 = d2.replace(/-/g,'');
			
		document.getElementById('form_feedback_'+lead_id).innerHTML = "Your lead has been updated.";
		if (d3 >= d1)
		document.getElementById('appt_remind_'+lead_id).innerHTML = "<input type='button' value='Remind' onclick='javascript: send_remind("+lead_id+")'>";
		else document.getElementById('appt_remind_'+lead_id).innerHTML = "";
		document.getElementById('last_update_td_'+lead_id).innerHTML = http.responseText;
		
		}
	setTimeout ("document.getElementById('form_feedback_"+lead_id+"').innerHTML = ''", 2000 );	
	}
	
}

function leads_update_detail(lead_id) {

var client_id = encodeURI(document.getElementById('client_id_'+lead_id).value);
var status = encodeURI(document.getElementById('status_'+lead_id).value);
var sale_date = encodeURI(document.getElementById('sale_date_'+lead_id).value);
var appt_date = encodeURI(document.getElementById('appt_date_'+lead_id).value);
var appt_time = encodeURI(document.getElementById('appt_time_'+lead_id).value);
var contact_date = encodeURI(document.getElementById('contact_date_'+lead_id).value);
var contact_time = encodeURI(document.getElementById('contact_time_'+lead_id).value);
var comments = encodeURI(document.getElementById('comments_'+lead_id).value);

var fname = encodeURI(document.getElementById('fname_'+lead_id).value);
var lname = encodeURI(document.getElementById('lname_'+lead_id).value);
var company_name = encodeURI(document.getElementById('company_name_'+lead_id).value);
var company_url = encodeURI(document.getElementById('company_url_'+lead_id).value);
var phone = encodeURI(document.getElementById('phone_'+lead_id).value);
var email = encodeURI(document.getElementById('email_'+lead_id).value);
var email_ok = encodeURI(document.getElementById('email_ok_'+lead_id).value);
var source = encodeURI(document.getElementById('source_'+lead_id).value);
var promo_desc = encodeURI(document.getElementById('promo_desc_'+lead_id).value);

// Set te random number to add to URL request
nocache = Math.random();

var params = 'lead_id='+lead_id+'&client_id='+client_id+'&status='+status+'&sale_date='+sale_date+'&appt_date='+appt_date+'&appt_time='+appt_time+'&contact_date='+contact_date+'&contact_time='+contact_time+'&comments='+comments+'&fname='+fname+'&lname='+lname+'&company_name='+company_name+'&company_url='+company_url+'&phone='+phone+'&email='+email+'&email_ok='+email_ok+'&source='+source+'&promo_desc='+promo_desc+'&nocache='+nocache;

document.getElementById('form_feedback_'+lead_id).innerHTML = '';

if (confirm('Are you sure you want to update this lead?')){


	// Pass the login variables like URL variable
	//http.open('get', 'sql/sql_leads_update_detail.php?'+params, false);
	http.open('post', 'sql/sql_leads_update_detail.php', false);
	
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	
	//http.onreadystatechange = insertReply;
	http.send(params);
	
	if(http.readyState == 4){

		
		document.getElementById('fname_h_'+lead_id).value = decodeURI(fname);
		document.getElementById('lname_h_'+lead_id).value = decodeURI(lname);
		document.getElementById('company_name_h_'+lead_id).value = decodeURI(company_name);
		document.getElementById('company_url_h_'+lead_id).value = decodeURI(company_url);
		document.getElementById('phone_h_'+lead_id).value = decodeURI(phone);
		document.getElementById('email_h_'+lead_id).value = decodeURI(email);
		document.getElementById('email_ok_h_'+lead_id).value = decodeURI(email_ok);
		document.getElementById('source_h_'+lead_id).value = decodeURI(source);
		document.getElementById('promo_desc_h_'+lead_id).value = decodeURI(promo_desc);
		
		var d0 = new Date();
		var d1 = d0.getYear()+''+(d0.getMonth()+1)+''+d0.getDate();
		var d2 = appt_date.replace(/-0/g,'');
		var d3 = d2.replace(/-/g,'');
			
		document.getElementById('form_feedback_'+lead_id).innerHTML = "Your lead has been updated.";
		if (d3 >= d1)
		document.getElementById('appt_remind_'+lead_id).innerHTML = "<input type='button' value='Remind' onclick='javascript: send_remind("+lead_id+")'>";
		else document.getElementById('appt_remind_'+lead_id).innerHTML = "";
		document.getElementById('last_update_td_'+lead_id).innerHTML = http.responseText;

		}
	setTimeout ("document.getElementById('form_feedback_"+lead_id+"').innerHTML = ''", 2000 );
	}

	
}

function hide_lead(lead_id){

var client_id = encodeURI(document.getElementById('client_id_'+lead_id).value);

// Set te random number to add to URL request
nocache = Math.random();

var params = 'lead_id='+lead_id+'&client_id='+client_id+'&nocache='+nocache;

document.getElementById('form_feedback_'+lead_id).innerHTML = '';

if (confirm('Are you sure you want to delete this lead?')){

	// Pass the login variables like URL variable
	//http.open('get', 'sql/sql_leads_hide.php?'+params, false);
	http.open('post', 'sql/sql_leads_hide.php', false);
	
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	
	//http.onreadystatechange = insertReply;
	http.send(params);
	//http.send(null);
	
	if(http.readyState == 4){
	
		document.getElementById('form_feedback_'+lead_id).innerHTML = "Lead has been DELETED. No longer available.<br>Refresh browser to remove from view.";		
		document.getElementById('updates_'+lead_id).innerHTML = "";
		
		}

	}
}

function chkbx(lead_id){

var fname = document.getElementById('fname_h_'+lead_id).value;
var lname = document.getElementById('lname_h_'+lead_id).value;
var company_name = document.getElementById('company_name_h_'+lead_id).value;
var company_url = document.getElementById('company_url_h_'+lead_id).value;
var phone = document.getElementById('phone_h_'+lead_id).value;
var email = document.getElementById('email_h_'+lead_id).value;
var email_ok = document.getElementById('email_ok_h_'+lead_id).value;
var source = document.getElementById('source_h_'+lead_id).value;
var promo_desc = document.getElementById('promo_desc_h_'+lead_id).value;

var sel_N = '';
if (email_ok == "N") {sel_N = "selected";} else {sel_N = "";}

if(document.getElementById('update_chkbx_'+lead_id).checked){
	document.getElementById('fname_td_'+lead_id).innerHTML = "<input type='text' size='20' id='fname_"+lead_id+"' value='"+fname+"'>";
	document.getElementById('lname_td_'+lead_id).innerHTML = "<input type='text' size='20' id='lname_"+lead_id+"' value='"+lname+"'>";
	document.getElementById('company_name_td_'+lead_id).innerHTML = "<input type='text' size='20' id='company_name_"+lead_id+"' value='"+company_name+"'>";
	document.getElementById('company_url_td_'+lead_id).innerHTML = "<input type='text' size='20' id='company_url_"+lead_id+"' value='"+company_url+"'>";
	document.getElementById('phone_td_'+lead_id).innerHTML = "<input type='text' size='20' id='phone_"+lead_id+"' value='"+phone+"'>";
	document.getElementById('email_td_'+lead_id).innerHTML = "<input type='text' size='20' id='email_"+lead_id+"' value='"+email+"'>";
	document.getElementById('email_ok_td_'+lead_id).innerHTML ="<select id='email_ok_"+lead_id+"'><option value='Y'>Y</option><option value='N' "+sel_N+">N</option></select>";     ;//"<input type='text' size='20' id='email_ok_"+lead_id+"' value='"+email_ok+"'>";
	document.getElementById('source_td_'+lead_id).innerHTML = "<input type='text' size='20' id='source_"+lead_id+"' value='"+source+"'>";
	document.getElementById('promo_desc_td_'+lead_id).innerHTML = "<input type='text' size='20' id='promo_desc_"+lead_id+"' value='"+promo_desc+"'>";
	}
else {
	document.getElementById('fname_td_'+lead_id).innerHTML = document.getElementById('fname_h_'+lead_id).value;
	document.getElementById('lname_td_'+lead_id).innerHTML = document.getElementById('lname_h_'+lead_id).value;
	document.getElementById('company_name_td_'+lead_id).innerHTML = document.getElementById('company_name_h_'+lead_id).value;
	document.getElementById('company_url_td_'+lead_id).innerHTML = document.getElementById('company_url_h_'+lead_id).value;
	document.getElementById('phone_td_'+lead_id).innerHTML = document.getElementById('phone_h_'+lead_id).value;
	document.getElementById('email_ok_td_'+lead_id).innerHTML = document.getElementById('email_ok_h_'+lead_id).value;
	document.getElementById('source_td_'+lead_id).innerHTML = document.getElementById('source_h_'+lead_id).value;
	document.getElementById('promo_desc_td_'+lead_id).innerHTML = document.getElementById('promo_desc_h_'+lead_id).value;	
	
	if(document.getElementById('email_ok_h_'+lead_id).value == "Y"){
		document.getElementById('email_td_'+lead_id).innerHTML = "<a href='mailto:"+document.getElementById('email_h_'+lead_id).value+"?subject=&body='>"+document.getElementById('email_h_'+lead_id).value+"</a>";
		}
		else {
		document.getElementById('email_td_'+lead_id).innerHTML = document.getElementById('email_h_'+lead_id).value;
		}

	}

}

function login() {

	var reason = "";
	reason += validateEmpty(document.getElementById('username'),"Please enter your Username (this is your email address).");
	reason += validateEmpty(document.getElementById('password'),"Please enter your Password");
	  
	if (reason != "") {
	alert("Some fields need correction:\n" + reason);
	}
	
	else {

	var username = encodeURI(document.getElementById('username').value);
	var password = encodeURI(document.getElementById('password').value);

	// Set te random number to add to URL request
	nocache = Math.random();
	
	var params = 'username='+username+'&password='+password+'&nocache = '+nocache;


	http.open('post', 'sql/sql_login.php', false);

	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	//http.onreadystatechange = insertReply;
	http.send(params);
	
	//http.onreadystatechange = insertReply;
	//http.send(null);
	
	if(http.readyState == 4){
		if (trim(http.responseText) == 'success') {window.location="dashboard.php";}
		else {document.getElementById('login_response').innerHTML = http.responseText;}
		}
	}
}



function login_form(){

	var html = '';
	html += '<table>\n';
	html += '<form action="javascript: login()">\n';
	html += '<tr><th colspan="2" id="login_response">Login</th></tr>\n';
	html += '<tr><td><table>\n';
	html += '<tr><td>Username:</td><td><input type="text" name="username" id="username" size=20 maxlength=30 value=""> (your email address)</td></tr>\n';	
	html += '<tr><td>Password:</td><td><input type="password" name="password" id="password" size=20 maxlength=30 value=""></td></tr>\n';	
	html += '<tr><td></td><td><input type="submit" value="Login"></td></tr>\n';
	html += '</table></td></tr>\n';
	html += '</form>\n';
	html += '</table>\n';
	
	//figure out FireFox <table> issue
	/*
	var html = '';
	html += '<form onsubmit="return validateLogin(this)" name="myForm" action="javascript:login()" method="get">\n';
	html += '<div id="login_response"></div>\n';
	html += '<div>Username:&nbsp;<input type="text" name="username" id="username" size=20 maxlength=30 value=""> (your email address)</div>\n';
	html += '<div>Password:&nbsp;<input type="password" name="password" id="password" size=20 maxlength=30 value=""></div>\n';
	html += '<div><input type="submit" value="Submit"></div>\n';
	html += '</form>\n';
	*/
	
	document.getElementById('login_out').innerHTML= html;

}


function lead_entry_insert(){

var client_id = encodeURI(document.getElementById('client_id').value);
var fname = document.getElementById('fname').value;
var lname = document.getElementById('lname').value;
var company_name = document.getElementById('company_name').value;
var company_url = document.getElementById('company_url').value;
var phone = document.getElementById('phone').value;
var email = document.getElementById('email').value;
var email_ok = document.getElementById('email_ok').value;
var source = document.getElementById('source').value;
var promo_desc = document.getElementById('promo_desc').value;
var status = document.getElementById('status').value;
var sale_date = document.getElementById('sale_date').value;
var lead_date = document.getElementById('lead_date').value;
var appt_date = document.getElementById('appt_date').value;
var appt_time = document.getElementById('appt_time').value;
var contact_date = document.getElementById('contact_date').value;
var contact_time = document.getElementById('contact_time').value;
var comments = document.getElementById('comments').value;


// Set te random number to add to URL request
nocache = Math.random();

var params = 'client_id='+client_id+'&fname='+fname+'&lname='+lname+'&company_name='+company_name+'&company_url='+company_url+'&phone='+phone+'&email='+email+'&email_ok='+email_ok+'&source='+source+'&promo_desc='+promo_desc+'&status='+status+'&sale_date='+sale_date+'&lead_date='+lead_date+'&appt_date='+appt_date+'&appt_time='+appt_time+'&contact_date='+contact_date+'&contact_time='+contact_time+'&comments='+comments+'&nocache='+nocache;

document.getElementById('insert_response').innerHTML = '';

	// Pass the login variables like URL variable
	//http.open('get', 'sql/sql_leads_hide.php?'+params, false);
	http.open('post', 'sql/sql_lead_entry_insert.php', false);
	
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	
	//http.onreadystatechange = insertReply;
	http.send(params);
	//http.send(null);
	
	if(http.readyState == 4){
	
		if (http.responseText.substring(0,1) == 's')
		{
		document.getElementById('lead_entry_form').innerHTML = "Lead has been entered.<br><br><a href='lead_entry.php'>Enter Another Lead</a>";
		}		
		else document.getElementById('insert_response').innerHTML = http.responseText;

		}

}

function email_message_html_insert(){
	var client_id = document.getElementById('client_id').value;
	//var recipients = document.getElementById('status').value;
	var subject_line = document.getElementById('subject_line').value;
	var header_img_url = document.getElementById('header_img_url').value;
	var header_img_align = '';
	if (document.getElementById('header_img_align_l').checked)
	header_img_align = 'left';
	if (document.getElementById('header_img_align_c').checked)
	header_img_align = 'center';
	if (document.getElementById('header_img_align_r').checked)
	header_img_align = 'right';
	var message = document.getElementById('message').value;
	
	var m = Math.round(message.length / 6);
	
	var m1 = message.substring(0,m);
	var m2 = message.substring(m,2*m);
	var m3 = message.substring(2*m,3*m);
	var m4 = message.substring(3*m,4*m);
	var m5 = message.substring(4*m,5*m);
	var m6 = message.substring(5*m);
	
	var reason = "";
	reason += validateEmpty(document.getElementById('subject_line'),"Please enter the E-Mail subject Line.");
	reason += validateEmpty(document.getElementById('message'),"Please enter a Message.");
	  
	if (reason != "") {
	alert("Some fields need correction:\n" + reason);
	}
	else
	{
	// Set te random number to add to URL request
	nocache = Math.random();
	
	var params = 'client_id='+client_id+'&subject_line='+subject_line+'&header_img_url='+header_img_url+'&header_img_align='+header_img_align+'&m1='+m1+'&m2='+m2+'&m3='+m3+'&m4='+m4+'&m5='+m5+'&m6='+m6+'&nocache='+nocache;
	
		if (confirm('Are you sure you want to save this email blast?')){
		document.getElementById('last_update').innerHTML = '';
	
		// Pass the login variables like URL variable
		//http.open('get', 'sql/sql_leads_hide.php?'+params, false);
		http.open('post', 'sql/sql_email_message_html_insert.php', false);
		
		//Send the proper header information along with the request
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");
		
		//http.onreadystatechange = insertReply;
		http.send(params);
		//http.send(null);
	
			if(http.readyState == 4){
				document.getElementById('last_update').innerHTML = http.responseText.substring(0,19);
			}
		}
	}
}

function email_promo_html_insert(){
	var client_id = document.getElementById('client_id').value;
	var cp_type = document.getElementById('cp_type').value;
	var cp_promo_desc = document.getElementById('cp_promo_desc').value;
	var cp_email_subject = document.getElementById('cp_email_subject').value;
	var cp_width = document.getElementById('cp_width').value;
	var cp_border = document.getElementById('cp_border').value;
	var cp_border_color = document.getElementById('cp_border_color').value;
		if (cp_border_color == '') cp_border_color ='black';
	var cp_background_color = document.getElementById('cp_background_color').value;
		if (cp_background_color == '') cp_background_color ='white';
	
	var cp_image_source = document.getElementById('cp_image_source').value;
	var cp_promo_desc1 = document.getElementById('cp_promo_desc1').value;
	var cp_promo_desc2 = document.getElementById('cp_promo_desc2').value;
	var cp_promo_desc3 = document.getElementById('cp_promo_desc3').value;
	
	var cp_promo_font_size1 = document.getElementById('cp_promo_font_size1').value;
		if (cp_promo_font_size1 == '') cp_promo_font_size1 ='14';
	var cp_promo_font_size2 = document.getElementById('cp_promo_font_size2').value;
		if (cp_promo_font_size2 == '') cp_promo_font_size2 ='14';
	var cp_promo_font_size3 = document.getElementById('cp_promo_font_size3' ).value;
		if (cp_promo_font_size3 == '') cp_promo_font_size3 ='14';
		
	var cp_promo_font_type1 = document.getElementById('cp_promo_font_type1').value;
		if (cp_promo_font_type1 == '') cp_promo_font_type1 ='arial';
	var cp_promo_font_type2 = document.getElementById('cp_promo_font_type2').value;
		if (cp_promo_font_type2 == '') cp_promo_font_type2 ='arial';
	var cp_promo_font_type3 = document.getElementById('cp_promo_font_type3').value;
		if (cp_promo_font_type3 == '') cp_promo_font_type3 ='arial';
		
	var cp_promo_font_color1 = document.getElementById('cp_promo_font_color1').value;
		if (cp_promo_font_color1 == '') cp_promo_font_color1 ='black';
	var cp_promo_font_color2 = document.getElementById('cp_promo_font_color2').value;
		if (cp_promo_font_color2 == '') cp_promo_font_color2 ='black';
	var cp_promo_font_color3 = document.getElementById('cp_promo_font_color3').value;
		if (cp_promo_font_color3 == '') cp_promo_font_color3 ='black';
		
	var cp_promo_font_style1 = document.getElementById('cp_promo_font_style1').value;
	var cp_promo_font_style2 = document.getElementById('cp_promo_font_style2').value;
	var cp_promo_font_style3 = document.getElementById('cp_promo_font_style3').value;
	
	var cp_exp = document.getElementById('cp_exp').value;
	var cp_exp_font_size = document.getElementById('cp_exp_font_size').value;
		if (cp_exp_font_size == '') cp_exp_font_size ='14';
	var cp_exp_font_type = document.getElementById('cp_exp_font_type').value;
		if (cp_exp_font_type == '') cp_exp_font_type ='arial';
	var cp_exp_font_color = document.getElementById('cp_exp_font_color').value;
		if (cp_exp_font_color == '') cp_exp_font_color ='black';
	var cp_exp_font_style = document.getElementById('cp_exp_font_style').value;
	
	var cp_fprint = document.getElementById('cp_fprint').value.replace(/\r\n/g,"<br>");
	var cp_fprint_font_size = document.getElementById('cp_fprint_font_size').value;
		if (cp_fprint_font_size == '') cp_fprint_font_size ='8';
	var cp_fprint_font_type = document.getElementById('cp_fprint_font_type').value;
		if (cp_fprint_font_type == '') cp_fprint_font_type ='arial';
	var cp_fprint_font_color = document.getElementById('cp_fprint_font_color').value;
		if (cp_fprint_font_color == '') cp_fprint_font_color ='black';
	var cp_fprint_font_style = document.getElementById('cp_fprint_font_style').value;
	
	var cp_foot = document.getElementById('cp_foot').value.replace(/\r\n/g,"<br>");
	var cp_foot_font_size = document.getElementById('cp_foot_font_size').value;
		if (cp_foot_font_size == '') cp_foot_font_size ='10';
	var cp_foot_font_type = document.getElementById('cp_foot_font_type').value;
		if (cp_foot_font_type == '') cp_foot_font_type ='arial';
	var cp_foot_font_color = document.getElementById('cp_foot_font_color').value;
		if (cp_foot_font_color == '') cp_foot_font_color ='black';
	var cp_foot_font_style = document.getElementById('cp_foot_font_style').value;		



	var html = '';
	html +='<table width="'+cp_width+'" cellspacing="5" style="padding:0px; border:'+cp_border+' '+cp_border_color+'; background-color:'+cp_background_color+';">';
	
	if (document.getElementById('cp_image_source').value != 'http://' && document.getElementById('cp_image_source').value != '')
	html +='<tr><td width="'+cp_width+'" style="border:0px; text-align:center; background-color:'+cp_background_color+';"><img src="'+cp_image_source+'"></td></tr>';
	
	html +='<tr><td width="'+cp_width+'" style="border:0px; font:'+cp_promo_font_style1+' '+cp_promo_font_size1+' '+cp_promo_font_type1+'; color:'+cp_promo_font_color1+'; text-align:center; background-color: '+cp_background_color+';">'+cp_promo_desc1+'</td></tr>';
	html +='<tr><td width="'+cp_width+'" style="border:0px; font:'+cp_promo_font_style2+' '+cp_promo_font_size2+' '+cp_promo_font_type2+'; color:'+cp_promo_font_color2+'; text-align:center; background-color: '+cp_background_color+';">'+cp_promo_desc2+'</td></tr>';
	html +='<tr><td width="'+cp_width+'" style="border:0px; font:'+cp_promo_font_style3+' '+cp_promo_font_size3+' '+cp_promo_font_type3+'; color:'+cp_promo_font_color3+'; text-align:center; background-color: '+cp_background_color+';">'+cp_promo_desc3+'</td></tr>';
	
	if (document.getElementById('cp_exp').value != 'none' && document.getElementById('cp_exp').value != '')
	html +='<tr><td width="'+cp_width+'" style="border:0px; font:'+cp_exp_font_style+' '+cp_exp_font_size+' '+cp_exp_font_type+'; color:'+cp_exp_font_color+'; text-align:center; background-color: '+cp_background_color+';">Redeem Before: [cp_exp]</td></tr>';	
	
	html +='<tr><td width="'+cp_width+'" style="border:0px; font:'+cp_fprint_font_style+' '+cp_fprint_font_size+' '+cp_fprint_font_type+'; color:'+cp_fprint_font_color+'; text-align:center; background-color: '+cp_background_color+';">'+cp_fprint+'</td></tr>';
	html +='<tr><td width="'+cp_width+'" style="border:0px; font:'+cp_foot_font_style+' '+cp_foot_font_size+' '+cp_foot_font_type+'; color:'+cp_foot_font_color+'; text-align:center; background-color: '+cp_background_color+';">'+cp_foot+'</td></tr>';
	html +='</table>';	


	var reason = "";
	reason += validateEmpty(document.getElementById('cp_promo_desc'),"Please enter a Promotion Description.");
	reason += validateEmpty(document.getElementById('cp_email_subject'),"Please enter the E-Mail subject Line.");
	  
	if (reason != "") {
	alert("Some fields need correction:\n" + reason);
	}
	else
	{
	// Set te random number to add to URL request
	nocache = Math.random();
	
	var params = 'client_id='+client_id;
	params += '&cp_type='+cp_type;
	params += '&cp_promo_desc='+cp_promo_desc;
	params += '&cp_email_subject='+cp_email_subject;
	params += '&cp_width='+cp_width;
	params += '&cp_border='+cp_border;
	params += '&cp_border_color='+cp_border_color;
	params += '&cp_background_color='+cp_background_color;
	params += '&cp_image_source='+cp_image_source;
	params += '&cp_promo_desc1='+cp_promo_desc1;
	params += '&cp_promo_desc2='+cp_promo_desc2;
	params += '&cp_promo_desc3='+cp_promo_desc3;
	params += '&cp_promo_font_size1='+cp_promo_font_size1;
	params += '&cp_promo_font_size2='+cp_promo_font_size2;
	params += '&cp_promo_font_size3='+cp_promo_font_size3;
	params += '&cp_promo_font_type1='+cp_promo_font_type1;
	params += '&cp_promo_font_type2='+cp_promo_font_type2;
	params += '&cp_promo_font_type3='+cp_promo_font_type3;
	params += '&cp_promo_font_color1='+cp_promo_font_color1;
	params += '&cp_promo_font_color2='+cp_promo_font_color2;
	params += '&cp_promo_font_color3='+cp_promo_font_color3;
	params += '&cp_promo_font_style1='+cp_promo_font_style1;
	params += '&cp_promo_font_style2='+cp_promo_font_style2;
	params += '&cp_promo_font_style3='+cp_promo_font_style3;
	params += '&cp_exp='+cp_exp;
	params += '&cp_exp_font_size='+cp_exp_font_size;
	params += '&cp_exp_font_type='+cp_exp_font_type;
	params += '&cp_exp_font_color='+cp_exp_font_color;
	params += '&cp_exp_font_style='+cp_exp_font_style;
	params += '&cp_fprint='+cp_fprint;
	params += '&cp_fprint_font_size='+cp_fprint_font_size;
	params += '&cp_fprint_font_type='+cp_fprint_font_type;
	params += '&cp_fprint_font_color='+cp_fprint_font_color;
	params += '&cp_fprint_font_style='+cp_fprint_font_style;
	params += '&cp_foot='+cp_foot;
	params += '&cp_foot_font_size='+cp_foot_font_size;
	params += '&cp_foot_font_type='+cp_foot_font_type;
	params += '&cp_foot_font_color='+cp_foot_font_color;
	params += '&cp_foot_font_style='+cp_foot_font_style;
	params += '&cp_html='+html;
	params += '&nocache='+nocache;
	
		if (confirm('Are you sure you want to save this email promo?')){
		
		document.getElementById('last_update').innerHTML = '';
	
		// Pass the login variables like URL variable
		//http.open('get', 'sql/sql_leads_hide.php?'+params, false);
		http.open('post', 'sql/sql_email_promo_html_insert.php', false);
		
		//Send the proper header information along with the request
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");
		
		//http.onreadystatechange = insertReply;
		http.send(params);
		//http.send(null);
		
			if(http.readyState == 4){
				document.getElementById('last_update').innerHTML = http.responseText.substring(0,19);
			}
		}		
	}	
}



function email_send_1(){

var client_id = document.getElementById('client_id').value;
var blast_content = '';
if (document.getElementById('promo').checked)
blast_content = 'promo';
if (document.getElementById('message').checked)
blast_content = 'message';

nocache = Math.random();

var params = 'client_id='+client_id+'&blast_content='+blast_content+'&nocache='+nocache;

// Pass the login variables like URL variable
http.open('post', 'sql/sql_email_preview.php', false);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

//http.onreadystatechange = insertReply;
http.send(params);
//http.send(null);

	if(http.readyState == 4){
		 var html = http.responseText;
	}
	
html += '<div style="width:60%; text-align:center;"><input type="submit" value="Send Email Blast" onclick="javascript: email_send_2()"></div>';
	
document.getElementById('email_preview').innerHTML = html;

}


function email_send_2(){

var client_id = document.getElementById('client_id').value;
var blast_content = '';
if (document.getElementById('promo').checked)
blast_content = 'promo';
if (document.getElementById('message').checked)
blast_content = 'message';

var x = document.getElementById('status');
var recipients = ""; 
for (i=0; i < x.length; i++) 
	{ 
		if (x.options[i].selected == true)
			recipients += "'" + x.options[i].value + "',"; 
	}
recipients = encodeURI(recipients.substring(0,(recipients.length - 1)));

// Set te random number to add to URL request
nocache = Math.random();

var params = 'client_id='+client_id+'&blast_content='+blast_content+'&recipients='+recipients+'&nocache='+nocache;


	if (confirm('Are you sure you want to send this email blast?')){
	document.getElementById('email_preview').innerHTML = '';

	// Pass the login variables like URL variable
	http.open('post', 'sql/sql_email_send.php', false);
	
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	
	//http.onreadystatechange = insertReply;
	http.send(params);
	//http.send(null);
	
		if(http.readyState == 4){
			document.getElementById('email_preview').innerHTML = http.responseText;
		}
	}


}

function scratchpad_insert(){

var client_id = document.getElementById('client_id').value;
var notes = document.getElementById('notes').value;

	var m = Math.round(notes.length / 6);
	
	var m1 = notes.substring(0,m);
	var m2 = notes.substring(m,2*m);
	var m3 = notes.substring(2*m,3*m);
	var m4 = notes.substring(3*m,4*m);
	var m5 = notes.substring(4*m,5*m);
	var m6 = notes.substring(5*m);

// Set te random number to add to URL request
nocache = Math.random();

var params = 'client_id='+client_id+'&m1='+m1+'&m2='+m2+'&m3='+m3+'&m4='+m4+'&m5='+m5+'&m6='+m6+'&nocache='+nocache;

// Pass the login variables like URL variable
http.open('post', 'sql/sql_scratchpad_insert.php', false);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

//http.onreadystatechange = insertReply;
http.send(params);
//http.send(null);

	if(http.readyState == 4){
		document.getElementById('last_update').innerHTML = http.responseText;
	}
}

function send_remind(lead_id){

// Set te random number to add to URL request
nocache = Math.random();

var params = 'lead_id='+lead_id+'&nocache='+nocache;

if (confirm('Are you sure you want to send an appt reminder?')){
// Pass the login variables like URL variable
http.open('post', 'sql/sql_send_remind.php', false);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

//http.onreadystatechange = insertReply;
http.send(params);
//http.send(null);

	if(http.readyState == 4){
		document.getElementById('appt_remind_'+lead_id).innerHTML = http.responseText;
	}
}

}

