<?php if (isset ($_SESSION['priv'])) { ?>

<style>
		body { font-size: 62.5%; }
		label, input { display:block; margin:5px 0px 0px 0px; width:300px }
		/*input.text { margin-bottom:12px; width:95%; padding: .4em; }*/
		fieldset { padding:0; border:0; margin-top:20px; }
		h1 { font-size: 1.2em; margin: .6em 0; }
		div#users-contain { width: 350px; margin: 20px 0; }
		div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; }
		div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; }
		.ui-dialog .ui-state-error { padding: 0px 0px 0px 5px; }
		.validateTips { border: 1px solid transparent; padding: 0.3em; }
	</style>
	<script>

	$(function() {
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
		$( "#dialog:ui-dialog" ).dialog( "destroy" );
		
		var pid = $( "#pid" ),
			public = $( "#public" ),
			allFields = $( [] ).add( pid ).add( public ),
			tips = $( ".validateTips" );

		function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
		}

		function checkLength( o1, min) {
			if ( o1.val().length < min ) {
				//o1.addClass( "ui-state-error" );
				updateTips( "Please submit page id." );
				return false;
			} else {
				return true;
			}
		}

		function checkRegexp( o, regexp, n ) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass( "ui-state-error" );
				updateTips( n );
				return false;
			} else {
				return true;
			}
		}

		$( "#dialog-form" ).dialog({
			autoOpen: false,
			height: 300,
			width: 350,
			modal: true,
			resizable: false,
			buttons: {
				"Submit page": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );

					bValid = checkLength( pid, 1 );

					if (pid.val().length > 0) {
						bValid = bValid && checkRegexp( pid, /^([0-9])+$/, "Page Id is not number" );
					}
										
					if ( bValid ) {
						// INSERT PAGE
						// CREATE NOTE
						
						xmlhttp = new XMLHttpRequest();
						xmlhttp.open("POST","ajax/submit_page.php",true);
						xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
						xmlhttp.send("pid="+pid.val()+"&public="+public.val());
						
						xmlhttp.onreadystatechange=function() {
  							if (xmlhttp.readyState==4 && xmlhttp.status==200) {
    							if (xmlhttp.responseText == "success") {
									//updateTips (xmlhttp.responseText);
									$( "#dialog-form" ).dialog( "close" );				
									$( "#dialog-message" ).dialog( "open" );
								}
							}
						}
						
						// title.val()
						// pagelink.val() 
						// pid.val() 
						//$( this ).dialog( "close" );
					}
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
		
		$( "#dialog-message" ).dialog({
			autoOpen: false,
			height: 300,
			width: 350,
			modal: true,
			resizable: false,
			buttons: {
				Ok: function() {
					$( "#dialog-form" ).dialog( "close" );
					$( this ).dialog( "close" );
				}
			}
		});

		$( "#create_page" )
			.button()
			.click(function() {
				$( "#dialog-form" ).dialog( "open" );
			});
	});
	
	</script>


<div id="dialog-form" title="Submit new page">
	<p class="validateTips">Submit page by entering page id.</p>

	<form>
	<fieldset>
		<label for="pid">Page Id</label>
		<input type="text" name="pid" id="pid" value="" class="text ui-widget-content ui-corner-all" />
        <?php if (isset($_SESSION['priv'])) { ?>
        <label for="public">Public</label>
        <select name="public" id="public" class="text ui-widget-content ui-corner-all">
        <option value="1" disabled="disabled">Yes</option>
        <option value="0" selected="selected">No</option>
        </select>
        <?php } else { ?>
        <input type="hidden" name="public" id="public" value="0" />
        <br />
        <?php } ?>
	</fieldset>
	</form>
	<p>
	<br />
<a href="#">Need help?</a></p>    
</div>


<div id="dialog-message" title="Page sucessfully submitted">
	<p>It may take up to 24 hours to index page. <br />
You'll receive notes about progress.<br /><br />
Thank You!</p>
</div>



<div class="content_box">
<div class="content_box_custom">
<?php /*?><form action="#" method="post">
<input name="newpage" value="Page name" id="newpage" onfocus="eraseText('Page name', this.id);" onblur="retrieveText('Page name', this.id);" />
<button id="create_page">Create Page</button>
</form>
<?php */?>

<button id="create_page">Submit Page</button>

</div>
</div>

<?php } ?>