var lastid;
var last_orderid;
function toggle_rows() {
	num_row = document.getElementById('numeric_row');
	item_row = document.getElementById('createitem_row');
	range_row = document.getElementById('range_row');
	select_box = document.getElementById('type');
	if (select_box.value == 'number') {
		num_row.style.display = '';
		item_row.style.display = 'none';
		range_row.style.display = 'none';
	} else if (select_box.value == 'dropdown') {
		num_row.style.display = 'none';
		item_row.style.display = '';
		range_row.style.display = 'none';
	} else if (select_box.value == 'range') {
		num_row.style.display = 'none';
		item_row.style.display = 'none';
		range_row.style.display = '';
	} else {
		num_row.style.display = 'none';
		item_row.style.display = 'none';
		range_row.style.display = 'none';
	}
}
function addItemField(div,page) {
	var itemfields = document.getElementById(div);
	var num = document.add_element.itemcount.value - 1;
	num += 2;
	document.add_element.itemcount.value = num;

	var newinput = document.createElement('span');
	newinput.innerHTML = 'Label: ';
	itemfields.appendChild(newinput);

	newinput = document.createElement('input');
	newinput.setAttribute('type','text');
	newinput.setAttribute('name','label'+num);
	itemfields.appendChild(newinput);

	newinput = document.createElement('span');
	newinput.innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Price Value: ';
	itemfields.appendChild(newinput);

	newinput = document.createElement('input');
	newinput.setAttribute('type','text');
	newinput.setAttribute('name','price'+num);
	itemfields.appendChild(newinput);

	if (page == 'builder') {
		newinput = document.createElement('span');
		newinput.innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hours: ';
		itemfields.appendChild(newinput);

		newinput = document.createElement('input');
		newinput.setAttribute('type','text');
		newinput.setAttribute('name','hours'+num);
		itemfields.appendChild(newinput);
	}

	newinput = document.createElement('br');
	itemfields.appendChild(newinput);
	newinput = document.createElement('br');
	itemfields.appendChild(newinput);
}
function addRangeField(div, page) {
	var rangefields = document.getElementById(div);
	var num = document.add_element.rangecount.value - 1;
	num += 2;
	document.add_element.rangecount.value = num;

	var newinput = document.createElement('span');
	newinput.innerHTML = 'Min: ';
	rangefields.appendChild(newinput);

	newinput = document.createElement('input');
	newinput.setAttribute('type','text');
	newinput.setAttribute('name','min'+num);
	rangefields.appendChild(newinput);

	newinput = document.createElement('span');
	newinput.innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Max: ';
	rangefields.appendChild(newinput);

	newinput = document.createElement('input');
	newinput.setAttribute('type','text');
	newinput.setAttribute('name','max'+num);
	rangefields.appendChild(newinput);

	newinput = document.createElement('span');
	newinput.innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Price Value: ';
	rangefields.appendChild(newinput);

	newinput = document.createElement('input');
	newinput.setAttribute('type','text');
	newinput.setAttribute('name','rangeprice'+num);
	rangefields.appendChild(newinput);

	if (page == 'builder') {
		newinput = document.createElement('span');
		newinput.innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hours: ';
		rangefields.appendChild(newinput);

		newinput = document.createElement('input');
		newinput.setAttribute('type','text');
		newinput.setAttribute('name','rangehours'+num);
		rangefields.appendChild(newinput);
	}

	newinput = document.createElement('br');
	rangefields.appendChild(newinput);
	newinput = document.createElement('br');
	rangefields.appendChild(newinput);
}
function GetXmlHttpObject() {
	var xmlHttp = null;
	try {
		// Firefox, Opera, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		// IE
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	if (xmlHttp == null) {
		alert("Your browser is not compatible with AJAX.");
		return;
	}
	return xmlHttp;
}
function loadTags(page,selected){
	xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange = tagsStateChanged;
	var url = "load_tags.php";
	url += '?page='+ page;
	url += '&sel='+ selected;
	url += '&sid='+Math.random();
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return false;
}
function tagsStateChanged() {
	if (xmlHttp.readyState == 4) {
		document.getElementById('tagsdropdown').innerHTML = xmlHttp.responseText;
	}
}
function multiplierChanged(eid) {
	document.getElementById('multdiv_' + eid).innerHTML = "<img src='/images/ajax-loader.gif'>";
	xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange = multiplierStateChanged;
	lastid = eid;
	var url = "/accounts/multiplier.php";
	url += '?eid='+ eid;
	url += '&value=' + document.getElementById('multiplier_' + eid).value;
	url += '&sid='+Math.random();
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return false;
}
function multiplierStateChanged() {
	if (xmlHttp.readyState == 4) {
		document.getElementById('multdiv_' + lastid).innerHTML = xmlHttp.responseText;
	}
}
function toggleHours(page, type) {
	if (page == 'builder' && type == 'number') {
		document.getElementById('hours_row').style.display = '';
		document.getElementById('tags_row').style.display = 'none';
	} else {
		document.getElementById('hours_row').style.display = 'none';
		document.getElementById('tags_row').style.display = '';
	}
}
function sendVerificationEmail(orderid) {
	var pass = confirm("This will email the client for order # " + orderid + ". Are you sure?");
	if (!pass) {
		return false;
	}
	xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange = emailStateChanged;
	var url = "/accounts/verification_actions.php";
	url += '?orderid='+ orderid;
	url += '&action=sendemail';
	url += '&sid='+Math.random();
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return false;
}
function emailStateChanged() {
	if (xmlHttp.readyState == 4) {
		alert("Verification Email Succesfully Sent.");
	}
}
function verifyOrder(orderid) {
	var pass = confirm("Are you sure you want to authorize order # " + orderid);
	if (!pass) {
		return false;
	}
	xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange = orderStateChanged;
	last_orderid = orderid;
	var url = "/accounts/autodelivery.php";
	url += '?orderid='+ orderid;
	url += '&action=authorize';
	//url += '&sid='+Math.random();
	//var boo = confirm("[DEBUG] Opening URL: "+url);
	//if (!pass) {
	//	return false;
	//}

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return false;
}
function orderStateChanged() {
	if (xmlHttp.readyState == 4) {
		alert("Order " + last_orderid + " Succesfully Authorized.");
		document.getElementById('row_' + last_orderid).style.display = 'none';
	}
}
function saveNote(orderid) {
	var note = prompt("Enter the note you'd like to add","");
	if (note == null || note == "") {
		return false;
	}
	xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange = noteStateChanged;
	var url = "/accounts/verification_actions.php";
	url += '?orderid='+ orderid;
	url += '&action=savenote';
	url += '&note='+note;
	url += '&sid='+Math.random();
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return false;
}
function noteStateChanged() {
	if (xmlHttp.readyState == 4) {
		alert("Note succesfully saved.");
	}
}

function failAuth(orderid) {
	var note = prompt("Enter the reason for failed authorization","");
	if (note == null || note == "") {
		return false;
	}
	xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange = failStateChanged;
	var url = "/accounts/verification_actions.php";
	url += '?orderid='+ orderid;
	url += '&action=failauth';
	url += '&note='+note;
	url += '&sid='+Math.random();
	last_orderid = orderid;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return false;
}
function failStateChanged() {
	if (xmlHttp.readyState == 4) {
		alert("Order has failed verification. Note saved.");
		document.getElementById('row_' + last_orderid).style.display = 'none';
	}
}

function getNote(orderid) {
	xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange = getNoteStateChanged;
	var url = "/accounts/verification_actions.php";
	url += '?orderid='+ orderid;
	url += '&action=getnote';
	url += '&sid='+Math.random();
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return false;
}
function getNoteStateChanged() {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.responseText == "") {
			alert("No notes for this order...");
		} else {
			alert(xmlHttp.responseText);
		}
	}
}

