// JavaScript Document
var $tabs;
totalArticles = 7;

function validateForm() {
	var name = document.getElementById("name");
	var email = document.getElementById("email");
	var phone = document.getElementById("phone");
	var message = document.getElementById("message");
	
	if(checkblank(name.value)) {
		alert("Please enter your name");
		name.focus();
		return false;
	}
	
	if(checkblank(email.value)) {
		alert("Please enter your email");
		email.focus();
		return false;
	}
	
	var email2 = email.value;
	if((email2.indexOf("@") == -1) || (email2.indexOf(".") == -1)) {
		alert("Please enter a valid email address");
		email.focus();
		return false;
	}
	
	if(checkblank(message.value)) {
		alert("Please enter your message");
		message.focus();
		return false;
	}
	
	submitForm();
	
	return false;
}

function submitForm() {
	$.post("server.php?request=contactform",{ name:$('#name').val(),company:$('#company').val(),email:$('#email').val(),phone:$('#phone').val(),state:$('#state').val(),message:$('#message').val(),rand:Math.random() } ,function(data)
	{
		data = "GOOD";
		var mess = document.getElementById("contactform");
		if(data == "GOOD") {
			var theH = "<span class=\"good\">Thank you for submitting the form.<br />One of our representatives will be contacting you shortly.</span>";
			//clearForm();
		} else {
			var theH = "<span class=\"error\">An unexpected error was encountered. Please try again.</span>";
		}
		theH = "<center><br /><br /><br />" + theH + "</center>";
		mess.innerHTML = theH;
	});
}

function clearForm() {
	$('#name').val("");
	$('#company').val("");
	$('#email').val("");
	$('#phone').val("");
	$('#state').val("");
	$('#message').val("");
}

function checkblank(vvalue) {
	var er = /^\s*$/
	if ( er.test(vvalue) ) {
		return true;
	}
	
	return false;
}

function getArticle(vchar) {
	for(var i=1; i<=totalArticles; i++) {
		if(document.getElementById("link" + i)) {
			document.getElementById("article" + i).style.display = "none";
			document.getElementById("link" + i).className = "links";
		}
	}
	document.getElementById("article" + vchar).style.display = "block";
	document.getElementById("link" + vchar).className = "links active";
}

function makePayment() {
	$.post("server.php?request=changetransstatus",{ complex:$('#complex').val(),status:"2",rand:Math.random() } ,function(data)
	{
		checkSession(data);
		if(data == "GOOD") {
			var accountid = document.getElementById("accountid");
			var userid = document.getElementById("userid");
			var complex = document.getElementById("complex");
			
			var cancel_return = document.getElementById("cancel_return");
			var xreturn = document.getElementById("return");
			var notify_url = document.getElementById("notify_url");
			
			cancel_return.value += "?id=" + complex.value + "," + accountid.value + "," + userid.value;
			xreturn.value += "?id=" + complex.value + "," + accountid.value + "," + userid.value;
			notify_url.value += "?id=" + complex.value + "," + accountid.value + "," + userid.value;
		
			document.paypal_paymentform.submit();
		}
	});
}

function createTransaction_old() {
	var accountid = document.getElementById("accountid");
	var userid = document.getElementById("userid");
	if(checkblank(accountid.value) || checkblank(userid.value)) {
		location.href = "secure.php";
		return false;
	}
	
	var description = document.getElementById("item_name");
	var amount = document.getElementById("amount");
	
	if(checkblank(description.value)) {
		inlineMsg('item_name','Enter the description.',2);
		return false;
	}
	if(checkblank(amount.value)) {
		inlineMsg('amount','Enter the amount.',2);
		return false;
	}
	var validCurrency = validateCurrency(amount.value);
	if(!validCurrency) {
		inlineMsg('amount','Enter a valid value for amount.<br />Decimal required<br />e.g. 100.00',5);
		return false;
	}
	
	$.post("server.php?request=createtransaction",{ description:$('#item_name').val(),amount:$('#amount').val(),rand:Math.random() } ,function(data)
	{
		checkSession(data);
		var response = data.split("|");
		if(response[0] == "GOOD") {
			var cancel_return = document.getElementById("cancel_return");
			var xreturn = document.getElementById("return");
			var notify_url = document.getElementById("notify_url");
			
			cancel_return.value += "?id=" + response[1] + "," + accountid.value + "," + userid.value;
			xreturn.value += "?id=" + response[1] + "," + accountid.value + "," + userid.value;
			notify_url.value += "?id=" + response[1] + "," + accountid.value + "," + userid.value;
			
			document.paypal_paymentform.submit();
		} else if(response[0] == "ERRORCREATINGCOMPLEX") {
			alert("Error Creating Complex Encountered");
		} else {
			alert("UNKNOWN ERROR");
			alert(data);
		}
	});
}

function checkSession(data) {
	if (data == "SESSIONEXPIRED") {
		location.href = "secure.php";
	}
}

function validateCurrency(str) {
	var xvalid = false;
	var xcheck;
	var isCurrency = /^\s*(\+|-)?((\d+(\.\d\d)?)|(\.\d\d))\s*$/;
	str = alltrim(str);
	xcheck = String(str).search (isCurrency) != -1;
	if(xcheck) {
		if(Number(str) >= 1) {
			if(str.indexOf(".") != -1) {
				xvalid = true;
			}
		}
	}
	
	return xvalid;
}

function alltrim(str) {
	return str.replace(/^\s+|\s+$/g, '');
}

function saveUser_old(type,id) {
	var company = document.getElementById("company");
	var address1 = document.getElementById("address1");
	var address2 = document.getElementById("address2");
	var city = document.getElementById("city");
	var state = document.getElementById("state");
	var zipcode = document.getElementById("zipcode");
	
	var fname = document.getElementById("fname");
	var lname = document.getElementById("lname");
	var phone = document.getElementById("phone");
	var email = document.getElementById("email");
	var password = document.getElementById("password");
	var status = document.getElementById("status");
	
	if(checkblank(company.value)) {
		inlineMsg('company','Please enter the company name',4);
		return false;
	}
	
	if(checkblank(state.value)) {
		inlineMsg('state','Please select the state',4);
		return false;
	}
	
	if(checkblank(fname.value)) {
		inlineMsg('fname','Please enter the first name',4);
		return false;
	}
	

	if(checkblank(lname.value)) {
		inlineMsg('lname','Please enter the last name',4);
		return false;
	}
	
	if(checkblank(email.value)) {
		inlineMsg('email','Please enter the email address',4);
		return false;
	}
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	var emailvalue = email.value;
	if(!emailvalue.match(emailRegex)) {
		inlineMsg('email','<strong>Error</strong><br />Invalid email address.',4);
		return false;
	}
	
	if(checkblank(password.value)) {
		inlineMsg('password','Please enter the password',4);
		return false;
	}
	
	if(checkblank(status.value)) {
		inlineMsg('status','Please select the user status',4);
		return false;
	}
	
	$("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
	$.post("server.php?request=verifyemail",{ email:$('#email').val(),rand:Math.random() } ,function(data)
	{
		if(data == "DUPLICATE") {
			$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{
				document.getElementById("email").focus();
				$(this).html('Email address already exists').addClass('messageboxerror').fadeTo(900,1);
			});
		} else if(data == "GOOD") {
			$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{
				$(this).html('Saving User').addClass('messageboxok').fadeTo(900,1);
			});
			saveUser2(type,id);
		} else {
			$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{
				$(this).html('Unexpected error encountered').addClass('messageboxerror').fadeTo(900,1);
			});
		}
	});
}

function saveUser2_old(type,id) {
	$.post("server.php?request=saveuser",{ type:type,id:id,company:$('#company').val(),address1:$('#address1').val(),address2:$('#address2').val(),city:$('#city').val(),state:$('#state').val(),zipcode:$('#zipcode').val(),fname:$('#fname').val(),lname:$('#lname').val(),phone:$('#phone').val(),email:$('#email').val(),password:$('#password').val(),status:$('#status').val(),rand:Math.random() } ,function(data)
	{
		checkSession(data);
		if(data == "GOOD") {
			$tabs.tabs('select', 0); // switch to third tab
		} else {
			$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{
				$(this).html('Unexpected error encountered').addClass('messageboxerror').fadeTo(900,1);
			});
		}
	});
}

function getUsers() {
	$.post("server.php?request=getusers",{ userbody:"1",rand:Math.random() } ,function(data)
	{
		document.getElementById("usersbody").innerHTML = data;
		$("#dialog").dialog('close');
		//$tabs.tabs('select', 0);
	});
}

function editUser(userid) {
	var type = "EDIT";
	$.post("server.php?request=getuser",{ type:type,id:userid,rand:Math.random() } ,function(data)
	{
		checkSession(data);
		var values = data.split("|");		
		$('#usertype').val("EDIT");
		$('#accountid').val(values[0]);
		$('#userid').val(userid);
		$('#company').val(values[1]);
		$('#address1').val(values[2]);
		$('#address2').val(values[3]);
		$('#city').val(values[4]);
		$('#state').val(values[5]);
		$('#zipcode').val(values[6]);
		$('#fname').val(values[7]);
		$('#lname').val(values[8]);
		$('#phone').val(values[9]);
		$('#email').val(values[10]);
		$('#password').val(values[11]);
		$('#status').val(values[12]);
		
		$('#dialog').dialog('open');
	});
}

function editPayment(transid) {
	var type = "EDIT";
	$.post("server.php?request=getpayment",{ type:type,id:transid,rand:Math.random() } ,function(data)
	{
		checkSession(data);
		var values = data.split("|");
		
		var fieldset = document.getElementById("payments_fieldset");
		var TheH = "";
		
		TheH += "<label for=\"p_contact\">Contact</label>";
		TheH += "<select name=\"pcontact\" id=\"pcontact\" size=\"1\" disabled class=\"text ui-widget-content ui-corner-all\">" + getContactList() + "</select>";
		TheH += "<label for=\"p_description\">Description</label>";
		TheH += "<input type=\"text\" name=\"pdescription\" id=\"pdescription\" disabled maxlength=\"50\" class=\"text ui-widget-content ui-corner-all\" />";
		TheH += "<label for=\"p_amount\">Amount</label>";
		TheH += "<input type=\"text\" name=\"pamount\" id=\"pamount\" disabled maxlength=\"10\" value=\"\" class=\"text ui-widget-content ui-corner-all\" />";
		TheH += "<label for=\"p_status\"><b>Status</b></label>";
		TheH += "<select name=\"pstatus\" id=\"pstatus\" size=\"1\" class=\"text ui-widget-content ui-corner-all\">" + getPaymentStatus(values[6]) + "</select>";
		TheH += "<label for=\"p_date\">Date of Last Status Update</label>";
		TheH += "<input type=\"text\" name=\"pdate\" id=\"pdate\" disabled maxlength=\"10\" value=\"\" class=\"text ui-widget-content ui-corner-all\" />";
		fieldset.innerHTML = TheH;	
		
		$('#paymenttype').val("EDIT");
		$('#paymentid').val(values[0]);
		$('#pcontact').val(values[1]);
		$('#pdescription').val(values[2]);
		$('#pamount').val(values[3]);
		$('#pstatus').val(values[4]);
		$('#pdate').val(values[5]);
		
		document.getElementById("validateTips2").innerHTML += "<br />Only the 'status' can be modified";
		
		$('#dialog2').dialog('open');
	});
}

function getPaymentStatus(xlists) {
	var xreturn = "<option value=\"\">Select a Status</option>";
	var xlist = xlists.split("[X]");
	for(var ii=0; ii<xlist.length; ii++) {
		var xstatus = xlist[ii].split(",");
		
		xreturn += "<option value=\"" + xstatus[0] + "\">" + xstatus[1] + "</option>";
	}
	
	return xreturn;
}

function newUser() {
	$('#usertype').val("NEW");
	$('#accountid').val("");
	$('#userid').val("");
	$('#company').val("");
	$('#address1').val("");
	$('#address2').val("");
	$('#city').val("");
	$('#state').val("");
	$('#zipcode').val("");
	$('#fname').val("");
	$('#lname').val("");
	$('#phone').val("");
	$('#email').val("");
	$('#password').val("");
	$('#status').val("");
	
	$('#dialog').dialog('open');
}

function newPayment() {
	var fieldset = document.getElementById("payments_fieldset");
	var TheH = "";
	
	TheH += "<label for=\"p_contact\"><b>Contact</b></label>";
	TheH += "<select name=\"pcontact\" id=\"pcontact\" size=\"1\" class=\"text ui-widget-content ui-corner-all\">" + getContactList() + "</select>";
	TheH += "<label for=\"p_description\"><b>Description</b></label>";
	TheH += "<input type=\"text\" name=\"pdescription\" id=\"pdescription\" maxlength=\"50\" class=\"text ui-widget-content ui-corner-all\" />";
	TheH += "<label for=\"p_amount\"><b>Amount</b></label>";
	TheH += "<input type=\"text\" name=\"pamount\" id=\"pamount\" maxlength=\"10\" value=\"\" class=\"text ui-widget-content ui-corner-all\" />";
	TheH += "<input type=\"hidden\" name=\"pstatus\" id=\"pstatus\" value=\"1\" />";

	fieldset.innerHTML = TheH;	
	$('#dialog2').dialog('open');
}

function getPayments() {
	$.post("server.php?request=getpayments",{ userbody:"1",rand:Math.random() } ,function(data)
	{
		document.getElementById("usersbody2").innerHTML = data;
		$("#dialog2").dialog('close');
	});
}

function savePayments() {
	$.post("server.php?request=savepayment",{ type:$('#paymenttype').val(),paymentid:$('#paymentid').val(),contact:$('#pcontact').val(),description:$('#pdescription').val(),amount:$('#pamount').val(),status:$('#pstatus').val(),rand:Math.random() } ,function(data)
	{
		checkSession(data);
		if(data == "GOOD") {
			getPayments();
		} else if(data == "ERRORCREATINGCOMPLEX") {
			alert("An unexpected error was encountered while creating the unique complex");
		} else {
			alert("An unexpected error was encountered while saving");
		}
	});
}

function getContactList() {
	var list = "<option value=\"\">Select a Contact</option>";
	var contacts = document.getElementById("contactlist").value;
	var contact = contacts.split("[X][X]");
	for(var i=0; i<contact.length; i++) {
		var temp = contact[i];
		var info = temp.split("[W]");
		
		var accountid = info[0];
		var userid = info[1];
		var name1 = info[2];
		var name2 = info[3];
		
		list += "<option value=\"" + accountid + "-" + userid + "\">" + name1 + " :: " + name2 + "</option>";
	}
	return list;
}

function checkDuplicateEmail() {
	$.post("server.php?request=verifyemail",{ type:$('#usertype').val(),accountid:$('#accountid').val(),userid:$('#userid').val(),email:$('#email').val(),rand:Math.random() } ,function(data)
	{
		if(data == "DUPLICATE") {
			alert("The email address already exists for another user.");
		} else if(data == "GOOD") {
			saveUser();
		} else {
			alert("An unexpected error was encounterd while verifying the email address");
		}
	});
}

function saveUser() {
	$.post("server.php?request=saveuser",{ type:$('#usertype').val(),accountid:$('#accountid').val(),userid:$('#userid').val(),company:$('#company').val(),address1:$('#address1').val(),address2:$('#address2').val(),city:$('#city').val(),state:$('#state').val(),zipcode:$('#zipcode').val(),fname:$('#fname').val(),lname:$('#lname').val(),phone:$('#phone').val(),email:$('#email').val(),password:$('#password').val(),status:$('#status').val(),rand:Math.random() } ,function(data)
	{
		checkSession(data);
		if(data == "GOOD") {
			getUsers();
		} else {
			alert("An unexpected error was encountered while saving the user");
		}
	});
}