var id=0;
var email="";
var lastlogin="";
var passcode="";

function leftSidebarCallBackResult() {
	var txtContent = ajaxreq.responseText;
	//ert(txtContent);
	$('#login').html( txtContent );
}

function removeItemsCallBackResult() {
	var xmlDoc = ajaxreq.responseXML.documentElement;
	var email = xmlDoc.getElementsByTagName('email')[0].firstChild.nodeValue;
	var ppStream = xmlDoc.getElementsByTagName('ppStream')[0].firstChild.nodeValue;
	ppStream = ppStream.replace(/@/g,'&');
	ppStream += "&EMAIL=" + email;
	processPayment( ppStream );
}

function itemCallBackResult() {
	var txtContent = ajaxreq.responseText;
	$("#viewitem_wait").css( { visibility: 'hidden' } );
	$("#viewitem_content").html(txtContent);
	mainContentDisplay ('viewitem' );
}

function paymentCallBackResult() {
	$("#viewitem_wait").css( { visibility: 'hidden' } );
	var txtContent = ajaxreq.responseText;
	$("#payment_content").html(txtContent);
	mainContentDisplay('payment');
}

function paymentProcessCallBackResult() {
	var xmlDoc = ajaxreq.responseXML.documentElement;
	var result = xmlDoc.getElementsByTagName('ACK')[0].firstChild.nodeValue;
	
	if ( result == "Success" ) {
		var trxid = xmlDoc.getElementsByTagName('ID')[0].firstChild.nodeValue;
		var trxtime = xmlDoc.getElementsByTagName('TIME')[0].firstChild.nodeValue;
		var trxamnt = xmlDoc.getElementsByTagName('AMT')[0].firstChild.nodeValue;
		var items = xmlDoc.getElementsByTagName('ITEM');
		var email = xmlDoc.getElementsByTagName('EMAIL')[0].firstChild.nodeValue;
		var itemcodes = new Array();
		for ( var i=0; i<items.length; i++ ) {
			itemcodes[i] = items[i].firstChild.nodeValue;
		}
		var message = "<h3>Order Complete</h3>";
		message += "<p>Your order was processed succesfully.</p>";
		message += "<p>Please note for future reference:</p>";
		message += "<p>Transaction ID: <span style=\"color:red\">" + trxid + "</span></p>";
		message += "<p>Transaction Date: <span style=\"color:red\">" + trxtime + "</span></p>";
		message += "<p>Transaction Amount: <span style=\"color:red\">$" + trxamnt + "</span></p>";
		message += "<p>For Item(s) (texders): <span style=\"color:red\">";
		for ( var i=0; i<itemcodes.length; i++ ) {
			message += itemcodes[i] + " ";
		}
		message += "</span></p>";
		message += "<br /><p>Thank you for your business!</p>";
		$.facebox(message);
		getAccountSummary( email )	//		mainContentDisplay('accntsum');
	} else {
		var reason = xmlDoc.getElementsByTagName('REASON')[0].firstChild.nodeValue;
		var reason2 = xmlDoc.getElementsByTagName('REASON2')[0].firstChild.nodeValue;
		var message = "<h3 style=\"color:red\">Order Problem</h3>";
		message += "<p>There was a problem with your payment information:</p>";
		message += "<p></p>";
		message += "<p style=\"color:red\">" + reason + "</p>";
		message += "<p></p>";
		message += "<p style=\"color:red\">" + reason2 + "</p>";
		$.facebox(message);
		mainContentDisplay('accntsum');
	}
}

function accntSumCallBackResult() {
	var txtContent = ajaxreq.responseText;
	$("#summary_wait").css( { display: 'none' } );
	$("#accntsum_content").html(txtContent);
	updateLeftSide( id, email, lastlogin, passcode );
	mainContentDisplay('accntsum');
}		
	
function addItemcallBackResult() {
	$("#additem_wait").css( { visibility: 'hidden' } );
	var xmlDoc = ajaxreq.responseXML.documentElement;
	var smscode = xmlDoc.getElementsByTagName('smscode')[0].firstChild.nodeValue;
	var status = xmlDoc.getElementsByTagName('status')[0].firstChild.nodeValue;
	var smscontent = Base64.decode ( xmlDoc.getElementsByTagName('smscontent')[0].firstChild.nodeValue );
	var start = xmlDoc.getElementsByTagName('start')[0].firstChild.nodeValue;
	var stop = xmlDoc.getElementsByTagName('stop')[0].firstChild.nodeValue;
	var email = xmlDoc.getElementsByTagName('email')[0].firstChild.nodeValue;
	var amntdue = parseFloat( xmlDoc.getElementsByTagName('amntdue')[0].firstChild.nodeValue.replace(/[^0-9.\- ]/g, "") );
	var amntdue = "Amount due: $" + fmt0_00(amntdue);
	
	var itemaddedcontrol ="<h3>Texder <span style=\"color:red;textdecoration:underline;\">" + smscode + "</span> added</h3>";
	itemaddedcontrol += "<p>To receive your post on a mobile phone:</p>";
	itemaddedcontrol += "<p style=\"font-style:italic;color:black;\">";
	itemaddedcontrol += "Text: &#8221;";
	itemaddedcontrol += "<span style=\"color:red;\">texder " + smscode + "</span>"; 
	//itemaddedcontrol += "<span>" + smscode + "</span>";
	itemaddedcontrol += "<span style=\"color:black;\">&#8222; to: &#8221;</span>";
	itemaddedcontrol += "<span style=\"color:red;\">95495</span>";
	itemaddedcontrol += "<span style=\"color:black;\">&#8222;</span>";
	//itemaddedcontrol += "</span>";
	itemaddedcontrol += "</p>";
	itemaddedcontrol += "<div style=\"border:1px solid #777;text-align:center;margin:10px 40px 10px 40px;max-width:250px;\">" + smscontent + "</div>";
	itemaddedcontrol += "<p>Active on: " + start + ", expires " + stop + "</p>";
	itemaddedcontrol += "<p>" + amntdue + "</p>";
	itemaddedcontrol += "<p>";
	if ( status == 0 ) {
		itemaddedcontrol += "<input type=\"button\" id=\"paynow_btn\" value=\"pay\" onclick=\"$.facebox.close(); payment();\" />";
	}
	itemaddedcontrol += "</p>";
	
	/*
	$("#smsCodeAdded").innerHTML = "<span>"+smscode+"</span>";
	$("#smsCodeAdded2").innerHTML = "<span>"+smscode+"</span>";
	$("#smsCodeContent").innerHTML = "<span>"+smscontent+"</span>";
	$("#smsStart").innerHTML = "<span>"+start+"</span>";
	$("#smsStop").innerHTML = "<span>"+stop+"</span>";
	$("#amntDue").innerHTML = "<span>"+amntdue+"</span>";
	if ( status == 0 ) {
		$("#payitnow").innerHTML = "<input type=\"button\" id=\"paynow_btn\" value=\"pay\" onclick=\"$.facebox.close(); payment();\" />";
	} else {
		$("#payitnow").innerHTML = "";
	}
	*/
	$.facebox( itemaddedcontrol );
	//$("#itemadded").slideDown('medium');
	
	getAccountSummary( email )
}

function displayAmntDue() {
	$("#total_wait").css( { visibility: 'hidden' } );
	var xmlDoc = ajaxreq.responseXML.documentElement;
	var promodescrip = xmlDoc.getElementsByTagName('promodescription')[0].firstChild.nodeValue;
	var itemprice = xmlDoc.getElementsByTagName('price')[0].firstChild.nodeValue;
	var discamnt = xmlDoc.getElementsByTagName('discount')[0].firstChild.nodeValue;
	var amntdue = parseFloat( xmlDoc.getElementsByTagName('amntdue')[0].firstChild.nodeValue.replace(/[^0-9.\- ]/g, "") );
	var enddate = xmlDoc.getElementsByTagName('enddate')[0].firstChild.nodeValue;

	var totaldisplay = "<table border=\"1\" style=\"text-decoration:none;border-collapse:collapse;\">";
	 totaldisplay += "<tr><th style=\"text-align:right;\">Price:</th><td style=\"font-weight:700;text-align:right;\">" + itemprice + "</td></tr>";
	 totaldisplay += "<tr><th style=\"text-align:right;\">Discount:</th><td id=\"promoamnt\" style=\"font-weight:700;text-align:right;\">" + discamnt + "</td></tr>";
	 totaldisplay += "<tr><th style=\"text-align:right;\">Due:</th><td id=\"amntdue\" style=\"font-weight:700;text-align:right;\">" + fmt0_00(amntdue) + "</td></tr>";
	 totaldisplay += "<tr><td colspan=\"2\" align=\"center\"><input id=\"update_totals_btn\" type=\"button\" value=\"update\" onclick=\"calcTotals();\" /></td></tr>";
	 totaldisplay += "</table>";
	 
	 $("#promo_descrip").html( promodescrip );
	 $("#end_date").html( "Expiration date: " + enddate );
	 $("#total_display").html( totaldisplay );
}
	 

function tinyURLcallBackResult() {
	var xmlDoc = ajaxreq.responseXML.documentElement;
	var smallURL = xmlDoc.getElementsByTagName('smallURL')[0].firstChild.nodeValue;
	var gotoURL = xmlDoc.getElementsByTagName('largeURL')[0].firstChild.nodeValue;
	var smscontent = $("#SMS_addItemContent").val();
	
	$("#largeURL").text( gotoURL );
	$("#smallURL").text( smallURL );

	$('#showSmallURL').slideDown('medium', function() {
		$("#makeSmallURL_wait").css( {visibility: 'hidden'} );
		document.getElementById('bigURL').value = "http://";
	});
	
	$("#SMS_addItemContent").val( smscontent + " " + smallURL );
	$("#addItemCharactercount").html( $("#SMS_addItemContent").val().length );
	
}


function contactUsCallBackResult() {
	$("#contactEmail_wait").css( {visibility: 'hidden'} );
	var xmlDoc = ajaxreq.responseXML.documentElement;
	//var result = xmlDoc.getElementsByTagName('result')[0].firstChild.nodeValue;
	var target =  xmlDoc.getElementsByTagName('texderEmail')[0].getAttribute('target');
	var mode = xmlDoc.getElementsByTagName('texderEmail')[0].getAttribute('mode');

	$.facebox("<h4>Thank you for your inquiry and comments.\nYou will receive a response very soon.</h4>");
	cancelContactUs();
}
// remove after launch ??
function notifyUsCallBackResult() {
	$("#notifyEmail_wait").css( {visibility: 'hidden'} );
	$("#notifyEmailThanks").show('medium', function() {
		$("#notifyReturnEmail").val("");
		$("#notifyEmailName").val("");
		var tT = setTimeout( "$('#notifyEmailThanks').hide('medium')", 3000 );
	});
}

function checkEmailCallBackResult() {
	var xmlDoc = ajaxreq.responseXML.documentElement;
	var result = parseInt( xmlDoc.getElementsByTagName('result')[0].firstChild.nodeValue );
	document.getElementById('email_dup').style.display="none";
	if ( result ) {
		document.getElementById('email_dup').style.display="block";
		document.prereg_form.email.focus();
	}
}

function resetPasswordCallBackResult() {
	var xmlDoc = ajaxreq.responseXML.documentElement;
	var result = parseInt( xmlDoc.getElementsByTagName('result')[0].firstChild.nodeValue );
	if ( result != "OK" ) {
		$("#login_email_notfound").css( { display: 'block' } );
		var tT = setTimeout( "$('#login_email_notfound').css( { display: 'none' } );", 5000 );
		return;
	}
}	
	
function changePasswordCallBackResult() {
	$("#changepwd_wait").css( { display: 'none' } );
	var xmlDoc = ajaxreq.responseXML.documentElement;
	var result = xmlDoc.getElementsByTagName('result')[0].firstChild.nodeValue;

	if ( result != "OK" ) {
		$("#changepwdemailerror").css( { display: 'block' } );
		var tT = setTimeout( "$('#changepwdemailerror').css( { display: 'none' } );", 5000 );
		return;
	} else {
		var newpwd = xmlDoc.getElementsByTagName('newpwd')[0].firstChild.nodeValue;
		$("#passcode").value = newpwd;
		expireCookie("pswd");
		setCookie("pswd", newpwd, 90);
		$("#changepassword").css( { display: 'none' } );
		$.facebox("<h3>Your password was updated succesfully.</h3>");
	}
}	
	
		
function callBackResult() {
	var xmlDoc = ajaxreq.responseXML.documentElement;
	var result = xmlDoc.getElementsByTagName('result')[0].firstChild.nodeValue;
	var mode = xmlDoc.getElementsByTagName('login')[0].getAttribute('mode');

	if ( mode == "profileUpdate" ) {
		$("#profile_update_wait").css( { visibility: 'hidden' } );
		var cpU = setTimeout("cancelProfileUpdate();", 2000);
	}
	
	if ( mode == "contentUpdate" ) {
		$("#SMScontent_wait").css( { visibility: 'hidden' } );
		$.facebox("<h3>SMS content</h3><p>texder SMS content is updated</p>");
/*
		if ( typeof( xmlDoc.getElementsByTagName('passcode')[0] ) != 'undefined') {
			var id = xmlDoc.getElementsByTagName('id')[0].firstChild.nodeValue;
			var email = xmlDoc.getElementsByTagName('email')[0].firstChild.nodeValue;
			var lastlogin = xmlDoc.getElementsByTagName('lastlogin')[0].firstChild.nodeValue;
			var passcode = xmlDoc.getElementsByTagName('passcode')[0].firstChild.nodeValue;
			updateLeftSide( id, email, lastlogin, passcode );
		}
*/
		document.SMScontent.SMS_itemContent.focus();
	}
	
	if ( mode== "stopItem" ) {
		var email =  xmlDoc.getElementsByTagName('email')[0].firstChild.nodeValue;
		var itemno =  xmlDoc.getElementsByTagName('id')[0].firstChild.nodeValue;
		ajaxCallback = itemCallBackResult;
		var program = "_itempage.php";
		var myArgs = "k=" + itemno + "&email=" + email;
		ajaxRequest(program, myArgs);
	}
	
	if ( mode== "restartItem" ) {
		var email =  xmlDoc.getElementsByTagName('email')[0].firstChild.nodeValue;
		var itemno =  xmlDoc.getElementsByTagName('id')[0].firstChild.nodeValue;
		ajaxCallback = itemCallBackResult;
		var program = "_itempage.php";
		var myArgs = "k=" + itemno + "&email=" + email;
		ajaxRequest(program, myArgs);
	}

	if ( mode== "extendItem" ) {

		//var email =  xmlDoc.getElementsByTagName('email')[0].firstChild.nodeValue;
		//var itemno =  xmlDoc.getElementsByTagName('id')[0].firstChild.nodeValue;
		payment();
		/*
		ajaxCallback = itemCallBackResult;
		var program = "_itempage.php";
		var myArgs = "k=" + itemno + "&email=" + email;
		ajaxRequest(program, myArgs);
		*/
	}
	
	if ( mode == "login" ) {
		$('#login_wait').css( { visibility: 'hidden' } );
		if ( result == "OK" ) {
			var member = ""
			var outstanding_items = false;

			//document.getElementById('mode').value = "update";
			 id =  xmlDoc.getElementsByTagName('id')[0].firstChild.nodeValue;
			email =  xmlDoc.getElementsByTagName('email')[0].firstChild.nodeValue;
			passcode =  xmlDoc.getElementsByTagName('passcode')[0].firstChild.nodeValue;
			var fname =  xmlDoc.getElementsByTagName('fname')[0].firstChild.nodeValue;
			var lname = xmlDoc.getElementsByTagName('lname')[0].firstChild.nodeValue;
			lastlogin = xmlDoc.getElementsByTagName('lastlogin')[0].firstChild.nodeValue;
			var itemcount = parseInt( xmlDoc.getElementsByTagName('SMSitems')[0].getAttribute('count') );

			// the following data is for the profile update page
			var address = xmlDoc.getElementsByTagName('address')[0].firstChild.nodeValue.replace(/^-/, '');
			var address2 = xmlDoc.getElementsByTagName('address2')[0].firstChild.nodeValue.replace(/^-/, '');
			var city = xmlDoc.getElementsByTagName('city')[0].firstChild.nodeValue.replace(/^-/, '');
			var state = xmlDoc.getElementsByTagName('state')[0].firstChild.nodeValue.replace(/^-/, '');
			var zip = xmlDoc.getElementsByTagName('zip')[0].firstChild.nodeValue.replace(/^-/, '');
			var country = xmlDoc.getElementsByTagName('country')[0].firstChild.nodeValue;
			var phone = xmlDoc.getElementsByTagName('phone')[0].firstChild.nodeValue.replace(/^-/, '');
			var subscription = xmlDoc.getElementsByTagName('subscription')[0].firstChild.nodeValue;
			if ( subscription == "yes" ) $("#update_subscription").checked = true;

			document.getElementById('update_email').value = email;
			document.getElementById('update_fname').value = fname;
			document.getElementById('update_lname').value = lname;
			if ( address ) document.getElementById('update_address').value = address;
			if ( address2 )	document.getElementById('update_address2').value = address2;
			if ( city )	document.getElementById('update_city').value = city;
			if (state ) document.getElementById('update_state').value = state;
			document.getElementById('update_zip').value = zip;
			document.getElementById('update_country').value = country;
			if ( phone ) document.getElementById('update_phone').value = phone;
			
			if( subscription == 'yes' ) {
					document.getElementById('update_subscription').checked = true;
			}
			// end of profile update page data insertion

			$("#registration_done_content").css( { display: 'none' } );
			getAccountSummary( email )
			externalLinks();
			$('#demonstration').slideUp('slow');
		} else {
			$('#login_error').css( { visibility: 'visible' } );
			setTimeout("$('#login_error').css( { visibility: 'hidden' } );$('#forgot_pwd').css( { visibility: 'visible' } );", 4000);
		}

	}
}

function updateLeftSide( id, email, lastlogin, passcode ) {
			var program = "_leftsidebar.php"
			var myArgs = "id="+id+"&email="+email+"&lastlogin="+lastlogin+"&passcode="+passcode;
			ajaxCallback = leftSidebarCallBackResult;
			ajaxRequest(program, myArgs);
}

function getAccountSummary( email ) {
	$("#summary_wait").css( { display: 'block' } );
	var myArgs = "email=" + email;
	var program = "_accntsumry.php";
	ajaxCallback = accntSumCallBackResult;
	ajaxRequest(program, myArgs);
}


function demoPhoneReturn() {
			document.getElementById('samplephoneno').value = "10 digit phone number";
}

function registrationCallBackResult() {
	document.getElementById('register_wait').style.visibility="hidden";
	var xmlDoc = ajaxreq.responseXML.documentElement;
	var result = xmlDoc.getElementsByTagName('result')[0].firstChild.nodeValue;
	var mode = xmlDoc.getElementsByTagName('register')[0].getAttribute('mode');

	if ( mode == "insert") {
		var result = xmlDoc.getElementsByTagName('result')[0].firstChild.nodeValue;
		var email = xmlDoc.getElementsByTagName('email')[0].firstChild.nodeValue;
		var pw = xmlDoc.getElementsByTagName('passwd')[0].firstChild.nodeValue;
		var fname = xmlDoc.getElementsByTagName('fname')[0].firstChild.nodeValue;
		var lname = xmlDoc.getElementsByTagName('lname')[0].firstChild.nodeValue;
		var phone = xmlDoc.getElementsByTagName('phone')[0].firstChild.nodeValue;
//		$.facebox("<h3>Thank you for joining Texder!</h3><p>We’ve sent a confirmation email to the address you provided.</p><p>Follow the simple instructions to complete your registration and start customizing your first texder.</p>");
		$.facebox("<h3>Registration confirmation</h3><p>"+fname+" "+lname+", thank you for registering.</p><p>Your login is: "+email+"</p><p>Password: "+pw+"</p><p>A confirmation email was sent to: "+email+"</p><p>Please reply to complete this registration.</p>");
//		alert("Thank you for joining Texder!\nWe’ve sent a confirmation email to the address you provided.\nFollow the simple instructions to complete your registration and start customizing your first texder.");

		mainContentDisplay();
	}
}

function putTestSMSResult() {
	document.getElementById('abuser').innerHTML = "";
	document.getElementById('invalidnumber').innerHTML = "";
	var xmlDoc = ajaxreq.responseXML.documentElement;
	var result = xmlDoc.getElementsByTagName('smsresult')[0].firstChild.nodeValue;
	var phone = xmlDoc.getElementsByTagName('phonenum')[0].firstChild.nodeValue;
	if ( result == "ABUSE" ) {
		document.getElementById('abuser').innerHTML = phone;
		document.getElementById("sampleprocess").style.display = "none";	
		document.getElementById('samplephone_abuse').style.display="inline";
		setTimeout( 'document.getElementById("samplephone_abuse").style.display = "none";', 4000 );
		document.getElementById('samplephoneno').value = "";
	}
	
	if ( result == "INVALIDNUMBER" ) {
		document.getElementById('invalidnumber').innerHTML = phone;
		document.getElementById("sampleprocess").style.display = "none";	
		document.getElementById('sampleinput').style.display="block";
		document.getElementById('samplephone_error').style.display="none";
		document.getElementById('samplephone_invalid').style.display="inline";
		setTimeout( 'document.getElementById("samplephone_invalid").style.display = "none";', 4000 );
		document.getElementById('samplephoneno').value = "";
	}
	
}

