var host = 'http://' + window.location.hostname;

function verifyURL(iURL, name, version, store) {
	var myConn = new XHConn();
	
	var fnWhenDone = function (oHTML) { var responseText = oHTML.responseText.split('|'); name.value = responseText[0]; version.value = responseText[1]; document.getElementById('_name').value = name.value; document.getElementById('_version').value = version.value; validateUploadField(name); validateUploadField(version); };

	if (!myConn) alert("Sorry, your browser does not support HTTP Request.");	
	myConn.connect(host + "/pgs/parse.php", "GET", "parse=" + iURL.value + "&store=" + store, fnWhenDone);
}

function startUpload() {	
	document.getElementById('progress').innerHTML = 'Uploading...<br /><img src="' + host + '/img/upload-loader.gif" />';
	document.getElementById('progress').style.display = 'block';
	
	return true;
}

function addTask() {
	var myConn = new XHConn();
	var task = document.getElementById('task').value;
	var fnWhenDone = function (oHTML) { showAdmin('todo'); };
	
	myConn.connect(host + '/pgs/main_admin_todo.php', 'GET', 'add=' + task, fnWhenDone);
}

function doComment(action, itunes_id) {
	var myConn = new XHConn();
	var extra = '';
	switch (action)
	{
		case "add":
			if (!document.getElementById('comment').value.replace(/ /g, ""))
			{
				alert("Comment entry required.");
				return;
			}
			extra = "&comment=" + document.getElementById('comment').value.replace(/\n/g, "<br />");
			break;
		case "mod":
			if (!document.getElementById('comment-mod').value.replace(/ /g, ""))
			{
				alert("Moderated comment entry required.");
				return;
			}
			extra = "&comment=" + document.getElementById('comment-mod').value.replace(/\n/g, "<br />");
			break;
		case "del":
			if (!document.getElementById('comment-del').value.replace(/ /g, ""))
			{
				alert("Deletion reason entry required.");
				return;
			}
			extra = "&reason=" + document.getElementById('comment-del').value.replace(/\n/g, "<br />");
			break;
	}

	var fnWhenDone = function (oHTML)
	{
		switch (action)
		{
			case "mget":
				var dForm = '<h1>Moderate <span="white">Comment</span></h1><form onsubmit="doComment(\'mod\', ' + itunes_id + '); return false;"><p><label>Comment</label><textarea id="comment-mod" rows="40" cols="50">' + document.getElementById('comment-' + itunes_id).innerHTML.replace(/<br>/g, "\n") + '</textarea><br /><br /><input class="button" type="submit" value="Moderate"> <input class="button" type="reset"></p></form>';
				document.getElementById('moderate').innerHTML = dForm;
				document.getElementById('add').style.display = 'none';
				document.getElementById('moderate').style.display = 'block';
				break;
			case "dget":
				var dForm = '<h1>Delete <span="white">Comment</span></h1><form onsubmit="doComment(\'del\', ' + itunes_id + '); return false;"><p><label>Reason</label><textarea id="comment-del" rows="40" cols="50"></textarea><br /><br /><input class="button" type="submit" value="Delete"> <input class="button" type="reset"></p></form>';
				document.getElementById('moderate').innerHTML = dForm;
				document.getElementById('add').style.display = 'none';
				document.getElementById('moderate').style.display = 'block';
				break;
			default:
				if (oHTML.responseText)
				{
					itunes_id = oHTML.responseText;
				}
				showComments(itunes_id);
				break;
		}
	}	

	if (!myConn) alert("Sorry, your browser does not support HTTP Request.");
	myConn.connect(host + "/pgs/main_comment.php", "GET", "action=" + action + "&itunes_id=" + itunes_id + extra, fnWhenDone);
}

function doAffiliate(site) {
	var myConn = new XHConn();
	var fnWhenDone = function (oHTML) {};
	
	myConn.connect(host + '/pgs/sidemenu_affiliates.php', 'GET', 'click=' + site, fnWhenDone);
}

function addNews() {
	var myConn = new XHConn();
	var text = encodeURIComponent(document.getElementById('text').value.replace(/\n/g, "<br />"));
	var title = encodeURIComponent(document.getElementById('title').value);
	var force = document.getElementById('force').checked;
	if (document.getElementById('force').checked)
	{
		force = 1;
	}
	else
	{
		force = 0;
	}
	var fnWhenDone = function (oHTML) { showAdmin('news'); };
	
	myConn.connect(host + '/pgs/main_admin_news.php', 'GET', 'add=' + title + "&text=" + text + "&force=" + force, fnWhenDone);
}

function doHide(id) {
	var myConn = new XHConn();
	var fnWhenDone = function (oHTML) {	var innerHTML = '<a href="javascript: void(0)" onclick="doHide(\'' + id + '\');">';	if (oHTML.responseText == '1') { innerHTML += 'Unhide</a>'; } else { innerHTML += 'Hide</a>'; } document.getElementById('hidden-' + id).innerHTML = innerHTML; };
	
	myConn.connect(host + '/pgs/main_admin_files.php', 'GET', 'hide=' + id, fnWhenDone);
}

function doFeature(id) {
	var myConn = new XHConn();
	var fnWhenDone = function (oHTML) {	document.getElementById('feature-' + id).innerHTML = '<a href="javascript: void(0)" onclick="doFeature(\'' + id + '\');">Unfeature</a>'; document.getElementById('feature-' + oHTML.responseText).innerHTML = '<a href="javascript: void(0)" onclick="doFeature(\'' + oHTML.responseText + '\');">Feature</a>'; };
	
	myConn.connect(host + '/pgs/main_admin_files.php', 'GET', 'feature=' + id, fnWhenDone);
}

function doAction(id, action) {
	var myConn = new XHConn();
	var fnWhenDone = function (oHTML) {
		document.getElementById('action-' + id).innerHTML = '';
		switch (action) {
			case 'started':
				document.getElementById('status-' + id).innerHTML = "<center>In Progress</center>";
				document.getElementById('action-' + id).innerHTML = "<center><a href=\"javascript: void(0)\" onclick=\"doAction('" + id + "', 'finished');\">Complete</a>&nbsp;&nbsp;&nbsp;<a href=\"javascript: void(0)\" onclick=\"doAction('" + id + "', 'deleted');\">Abandon</a></center>";
				break;
			case 'finished':
				document.getElementById('status-' + id).innerHTML = "<center>Knocked Out</center>";
				break;
			case 'deleted':
				document.getElementById('status-' + id).innerHTML = "<center>Abandoned</center>";
				break;
		}
	};
	
	myConn.connect(host + '/pgs/main_admin_todo.php', 'GET', 'action=' + action + '&id=' + id, fnWhenDone);
}

function doVerify(id, action) {
	var myConn = new XHConn();
	var fnWhenDone = function (oHTML) {
		document.getElementById('results').innerHTML = oHTML.responseText;
		document.getElementById('results').style.display = 'block';
		
		setTimeout("document.getElementById('results').style.display = 'none'; document.getElementById('results').innerHTML = '';", 3500);
	};
	
	myConn.connect(host + '/pgs/main_admin_verify.php', 'GET', action + '=' + id, fnWhenDone);
}

function doDelUpdate(id, action, display) {
	var myConn = new XHConn();
	var fnWhenDone = function (oHTML) {
		document.getElementById('results').innerHTML = oHTML.responseText;
		document.getElementById('results').style.display = 'block';
		
		setTimeout("document.getElementById('results').style.display = 'none'; document.getElementById('results').innerHTML = '';", 3500);
		showPage('updates', findCurrent());
	};
	
	myConn.connect(host + '/pgs/main_updates.php', 'GET', action + '=' + id + '&display=' + display, fnWhenDone);
}
function doFunction(func) {
	var myConn = new XHConn();
	var fnWhenDone = function (oHTML) {	document.getElementById('results').innerHTML = oHTML.responseText; document.getElementById('results').style.display = 'block';	setTimeout("document.getElementById('results').style.display = 'none'; document.getElementById('results').innerHTML = '';", 3500); };
	
	if (!myConn) alert("Sorry, your browser does not support HTTP Request.");	
	myConn.connect(host + "/pgs/main_admin_functions.php", "GET", func + "=1", fnWhenDone);
}

function doIgnore(id) {
	var myConn = new XHConn();
	var fnWhenDone = function (oHTML) {	document.getElementById('status-' + id).innerHTML = 'Ignored'; document.getElementById('action-' + id).innerHTML = ''; };
	
	if (!myConn) alert("Sorry, your browser does not support HTTP Request.");	
	myConn.connect(host + "/pgs/main_admin_contact.php", "GET", "ignore=" + id, fnWhenDone);
}

function doReply() {
	var myConn = new XHConn();
	var id = document.getElementById('contact-id').value;
	var text = encodeURIComponent(document.getElementById('text').value);
	var fnWhenDone = function (oHTML) { document.getElementById('results').innerHTML = 'Your reply was sent successfully.'; document.getElementById('status-' + id).innerHTML = 'Replied'; document.getElementById('action-' + id).innerHTML = ''; setTimeout("document.getElementById('results').style.display = 'none'; document.getElementById('results').id = 'div_reply_form';", 3500); };
	
	document.getElementById('div_reply_form').innerHTML = 'Your reply is being sent. Please wait...';
	document.getElementById('div_reply_form').id = 'results';
	myConn.connect(host + '/pgs/main_admin_contact.php', 'GET', 'reply=' + id + "&text=" + text, fnWhenDone);
}

function setupReply(id) {
	document.getElementById('div_reply_form').innerHTML = '<h1>Email <span class="white">Reply</span></h1><form onsubmit="doReply(); return false;" name="reply_form"><p><label>Automated Replies:&nbsp;&nbsp;&nbsp;<a href="javascript: void(0)" onclick="document.getElementById(\'text\').value = \'     We do not handle requests through the contact form. If you would like to make a request please use our donation feature which can be used to purchase a request code that is redeemable for one application. There is no limit to how many donations you can make for request codes. Any and all donations are for purchasing unseen applications and are for server costs and expenses.\'; doReply(); return false;">Request</a>&nbsp;&nbsp;&nbsp;<a href="javascript: void(0)" onclick="document.getElementById(\'text\').value = \'     Completely clearing your browser cache and restarting your browser should fix this problem. During high traffic, your downloads may finish abruptly and is beyond our control but we are currently working to see if there is anything we can do on our end. You can avoid the hassle by always verifying your download to the size on the application listing.\'; doReply(); return false;">Download</a></label><textarea id="text" name="text" rows="40" cols="50"></textarea><input id="contact-id" name="contact-id" type="hidden" value="" /><br /><br /><input class="button" value="Reply" type="submit" /> <input class="button" type="reset"></p></form>';
	document.getElementById('div_reply_form').style.display = 'block';
	document.getElementById('contact-id').value = id;
	document.getElementById('status-' + id).innerHTML = 'Replying';
}

function submitContact() {	
	document.getElementById('results').style.display = 'block';
	
	if (document.getElementById('results').innerHTML.search(/contacted/) != -1) { clearValue('alias'); clearValue('email'); clearValue('text'); }
	
	setTimeout("document.getElementById('results').style.display = 'none'; document.getElementById('results').innerHTML = '';", 5000);
}

function emailCode(email, code) {
	var myConn = new XHConn();
	var fnWhenDone = function (oHTML) { return true; };
	
	if (!myConn) alert("Sorry, your browser does not support HTTP Request.");	
	myConn.connect(host + "/pgs/donation.php", "GET", "donation_email=1&email=" + email + "&rcode=" + code, fnWhenDone);
}

function validateUploadField(field) {
	var myConn = new XHConn();
	var fnWhenDone = function (oHTML) {
		var innerHTML = '';
		if (field.name == 'itunes') {
			document.getElementById('_name').value = ''; document.getElementById('_version').value = '';
			document.getElementById('name').value = ''; document.getElementById('version').value = '';
			document.getElementById('itunes_id').value = oHTML.responseText;
			verifyURL(field, document.getElementById('name'), document.getElementById('version'), document.getElementById('store').value);
		}
		switch (oHTML.responseText) {
			case '0':
				innerHTML = '<img src="' + host + '/img/bad.gif">';
				break;
			default:
				innerHTML = '<img src="' + host + '/img/good.gif">';
				break;
		}			
		document.getElementById('span_' + field.name).innerHTML = innerHTML;
		if (document.getElementById('span_itunes').innerHTML.search(/good/) != -1 && document.getElementById('span_name').innerHTML.search(/good/) != -1 && document.getElementById('span_version').innerHTML.search(/good/) != -1 && document.getElementById('span_credit').innerHTML.search(/good/) != -1 && document.getElementById('span_ipa').innerHTML.search(/good/) != -1) {
			document.getElementById('upload_button').disabled = false;
		} else {
			document.getElementById('upload_button').disabled = true;
		}
	}
	//alert(document.getElementById('itunes_id').value);
	
	if (!myConn) alert("Sorry, your browser does not support HTTP Request.");
	myConn.connect(host + "/pgs/validate.php", "GET", "validate_" + field.name + "=" + document.getElementById('itunes_id').value + "&store=" + document.getElementById('store').value + "&value=" + field.value.replace('&', '%26').replace(/^\s\s*/, '').replace(/\s\s*$/, ''), fnWhenDone);
}

function validateReqField(field) {
	var myConn = new XHConn();
	var fName = field.title;
	var fnWhenDone = function (oHTML) {
		var innerHTML = '';
/*		if (fName == 'rcode') {
			switch (oHTML.responseText) {
				case '0':
					innerHTML = '<img src="' + host + '/img/good.gif"> <font style="color: #00FF33">Request Code is redeemable!</font>';
					break;
				case '1':
					innerHTML = '<img src="' + host + '/img/bad.gif"> <font style="color: #FF0000">Request Code already redeemed!</font>';
					break;
				case '2':
					innerHTML = '<img src="' + host + '/img/bad.gif"> <font style="color: #FF0000">Request Code not issued!</font>';
					break;
				case '3':
					innerHTML = '<img src="' + host + '/img/bad.gif"> <font style="color: #FF0000">Request Code not found!</font>';
					break;
			}
		} else { */
		if (fName == 'rurl') {
			switch (oHTML.responseText) {
				case '0':
					innerHTML = '<img src="' + host + '/img/bad.gif"> <font style="color: #FF0000">Not a valid iTunes App Store URL!</font>';
					break;
				case '1':
					innerHTML = '<img src="' + host + '/img/bad.gif"> <font style="color: #FF0000">The latest version of this application has already been cracked!</font>';
					break;
				case '2':
					innerHTML = '<img src="' + host + '/img/bad.gif"> <font style="color: #FF0000">This is a free application!</font>';
					break;
				case '3':
					innerHTML = '<img src="' + host + '/img/bad.gif"> <font style="color: #FF0000">You have used your request quota of (1) for the week!</font>';
					break;
				default:
					innerHTML = '<img src="' + host + '/img/good.gif"> <font style="color: #00FF33">You have chosen to request ' + oHTML.responseText + '!</font>';
					break;
			}
		}
		
		/*}*/
		document.getElementById(fName).innerHTML = innerHTML;
		//if (document.getElementById('rcode').innerHTML.search(/good/) != -1 && document.getElementById('rurl').innerHTML.search(/good/) != -1) {
			if (document.getElementById('rurl').innerHTML.search(/good/) != -1) {
			document.getElementById('request_button').disabled = false;
		} else {
			document.getElementById('request_button').disabled = true;
		}
	}
	
	if (!myConn) alert("Sorry, your browser does not support HTTP Request.");
	if (field.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '') != '') { myConn.connect(host + "/pgs/validate.php", "GET", "validate_" + fName + "=1&value=" + field.value.replace('&', '%26').replace(/^\s\s*/, '').replace(/\s\s*$/, ''), fnWhenDone); } else { document.getElementById(fName).innerHTML = ''; }
}

function resetRequestForm() {
	//document.getElementById('rcode').innerHTML = '';
	document.getElementById('rurl').innerHTML = '';
	//document.getElementById('code').value = '';
	document.getElementById('url').value = '';
	document.getElementById('request_button').disabled = true;
}

function requestSubmitted(request) {
	document.getElementById('request_status').innerHTML = 'Your request for ' + request + ' has been submitted successfully!';
	document.getElementById('request_status').style.display = 'block';
	resetRequestForm();
	refreshRequests();
	setTimeout("document.getElementById('request_status').style.display = 'none';", 3000);
	return true;
}

function refreshRequests() {
	var myConn = new XHConn();
	var fnWhenDone = function (oHTML) { document.getElementById('sidemenu_requests').innerHTML = oHTML.responseText; };
	
	if (!myConn) alert("Sorry, your browser does not support HTTP Request.");	
	myConn.connect(host + "/pgs/sidemenu_requests.php", "GET", "", fnWhenDone);
}

function stopUpload(success) {
	var result = '';
	
	if (success == 1) {
		result = 'The file was uploaded successfully!';
	}
	else {
		result = 'There was an error during file upload!';
	}

	result = success;
	
	document.getElementById('progress').innerHTML = result;
	
	clearValue('ipa'); clearValue('itunes'); clearValue('name'); clearValue('version'); clearValue('credit'); clearHTML('span_ipa'); clearHTML('span_itunes'); clearHTML('span_name'); clearHTML('span_version'); clearHTML('span_credit'); document.getElementById('exclusive').checked = false;
	document.getElementById('itunes').focus();
			
	setTimeout("document.getElementById('progress').style.display = 'none';", 1500);
		
	return true;   
}

function clearValue(id) {
	document.getElementById(id).value = '';
}

function clearHTML(id) {
	document.getElementById(id).innerHTML = '';
}

function changeField(field) {
	var id = field.id.split('-');
	field.innerHTML = '<input id="' + id[0] + '" name="' + id[1] + '" type="text" value="' + field.innerHTML + '" onblur="saveValue(this);" />';
	document.getElementById(id[0]).focus();
}

function setCurrentPage(clicked, current) {
	if (document.getElementById('current')) document.getElementById('current').id = current;
	if (document.getElementById(clicked)) document.getElementById(clicked).id = 'current';
}

function showLoading() {
	document.getElementById('main').innerHTML = '<div style="text-align: center; padding-top: 200px;"><img src="' + host + '/img/ajax-loader.gif" /></div>';
}

function findCurrent() {
	var ele = document.getElementById('current');
	
	if (ele) { 
		var current = ele.firstChild.name; 
	} else { 
		var current = null; 
	}
	
	return current;
}

function saveValue(field) {
	var myConn = new XHConn();
	var fnWhenDone = function (oHTML) { document.getElementById(field.parentNode.id).innerHTML = oHTML.responseText; };
	
	if (!myConn) alert("Sorry, your browser does not support HTTP Request.");	
	myConn.connect(host + "/pgs/main_admin_files.php", "GET", "id=" + field.name + "&field=" + field.id + "&value=" + field.value, fnWhenDone);
}

function queryiTunes(value) {
	var myConn = new XHConn();
	var fnWhenDone = function (oHTML) { document.getElementById('itunes').innerHTML = oHTML.responseText; };
	
	myConn.connect(host + '/pgs/main_format_post.php', 'GET', 'itunes=' + value + '&store=0', fnWhenDone);
}


function submitForm(form, page, target) {
	var myConn = new XHConn();
	var paramStr = form.name + "=1&";
	var buildParam = function (name, value) { return name + "=" + value + "&"; };
	var fnWhenDone = function (oHTML) {
		document.getElementById(target).innerHTML = oHTML.responseText;
		if (document.getElementById('current')) document.getElementById('current').id = document.getElementById('current').firstChild.name;
		if (page == 'main_contact_form') submitContact();
	};
	
	if (!myConn) alert("Sorry, your browser does not support HTTP Request.");
	for (i = 0; i < form.elements.length; i++) {
		switch (form.elements[i].type) {
			case "text":
			case "textarea":
				paramStr += buildParam(form.elements[i].name, form.elements[i].value.replace(/\n/g, "<br />"));
				break;
			case "checkbox":
				if (form.elements[i].checked) {
					paramStr += buildParam(form.elements[i].name, form.elements[i].value);
				} else {
					paramStr += buildParam(form.elements[i].name, '');
				}
				break;
			case "radio":
				if (form.elements[i].checked) paramStr += buildParam(form.elements[i].name, form.elements[i].value);
				break;
			case "select-one":
				if (form.elements[i].selectedIndex != -1) paramStr += buildParam(form.elements[i].name, form.elements[i].options[form.elements[i].selectedIndex].value);
				break;
			case "hidden":
				paramStr += buildParam(form.elements[i].id, form.elements[i].value);
				break;
		}
	}
	myConn.connect(host + "/pgs/" + page + ".php", "GET", paramStr, fnWhenDone);
}
