$(document).ready(function() {
	$("#checkHeaderForm").submit(function() {
		$("#chInfo").show('slow');
		$("#chsInfo").show('slow');
		$("#chInfoBody").html('');
		$("#chsInfoBody").html('');
		$("#chInfoDiv").block({
			css: {
			    border: '0px solid #e6e6e6',
				padding: '5px',
				backgroundColor: '#ffffff',
				opacity: '.9',
				filter: 'alpha(opacity = 90)',
				zoom: '1',
				color: '#000000',
				width: '400px',
				textAlign: 'left'
			},
			overlayCSS: {
			    border: '0px solid #e6e6e6',
				backgroundColor: '#000000',
				opacity: '.5',
				filter: 'alpha(opacity = 50)',
				zoom: '1'
			},
			message: '<div style="line-height:50px;" class="domainTitle"><img src="/images/ajax-loader.gif" style="float:left; padding-right:10px;" />Getting Response Headers and info...</div>'
		});
		//now, get the data
		$.ajax({
			type: "GET",
			url: "http://bclaydev.bruceclay.com/services/sitechecker.py/GetHeaderInfo",
			data: "url="+$("#chBox").val(),
			dataType: 'jsonp',
			jsonp: 'jsonp',
			timeout: 12000,
			success: function(json) {
				if(json.error) {
					$("#chInfoDiv").unblock().block({
						css: {
				            border: '0px solid #e6e6e6',
				            padding: '5px',
				            backgroundColor: '#ffffff',
				            opacity: '.9',
				            filter: 'alpha(opacity = 90)',
				            zoom: '1',
				            color: '#000000',
				            width: '600px',
				            textAlign: 'left'
				        },
				        overlayCSS: {
				            border: '0px solid #e6e6e6',
				            backgroundColor: '#000000',
				            opacity: '.5',
				            filter: 'alpha(opacity = 50)',
				            zoom: '1'
				        },
				        message: '<div style="line-height:50px;" class="domainTitle">This page could not be checked. Please check the page URL that you entered. <input type="button" id="chInvalid" value="OK" /></div>'
					});
					$("#chInvalid").click(function() {
		    			$("#chInfoDiv").unblock();
		    			return false;
		    		});
				} else {
				    chHtml = '';
				    $.each(json.Criteria, function(i,item) {
					    if (item.CheckedCriterion == 'Status') {
						chHtml += '<tr><td>Status</td><td>'+ item.Description + '</td></tr>';
					    } else {
						//break up Headers
						//strip all <br />
						headers = item.Description.split('<br />');
						$.each(headers, function(i) {
							head = headers[i].split(":");
							chHtml += '<tr><td>'+head[0]+'</td><td>'+head[1]+'</td></tr>';
						    });
					    }
					});
				    $("#chInfoBody").html(chHtml);
				}
			}
		});
		$.ajax({
			type: "GET",
			url: "http://bclaydev.bruceclay.com/services/sitechecker.py/GetSiteInfo",
			data: "url="+$("#chBox").val(),
			dataType: 'jsonp',
			jsonp: 'jsonp',
			timeout: 12000,
			success: function(json) {
				if(json.error) {
					$("#chInfoDiv").unblock().block({
						css: {
				            border: '0px solid #e6e6e6',
				            padding: '5px',
				            backgroundColor: '#ffffff',
				            opacity: '.9',
				            filter: 'alpha(opacity = 90)',
				            zoom: '1',
				            color: '#000000',
				            width: '600px',
				            textAlign: 'left'
				        },
				        overlayCSS: {
				            border: '0px solid #e6e6e6',
				            backgroundColor: '#000000',
				            opacity: '.5',
				            filter: 'alpha(opacity = 50)',
				            zoom: '1'
				        },
				        message: '<div style="line-height:50px;" class="domainTitle">This page could not be checked. Please check the page URL that you entered. <input type="button" id="chInvalid" value="OK" /></div>'
					});
					$("#chInvalid").click(function() {
		    			$("#chInfoDiv").unblock();
		    			return false;
		    		});
				} else {
				    chsHtml = '';
				    $.each(json.Criteria, function(i,item) {
					    cc = item.CheckedCriterion;
					    if (cc == 'Original URL' || cc == 'DNS IP' || cc == 'Ping') {
						chsHtml += '<tr><td width="150">'+cc+'</td><td width="350">'+item.Description+'</td></tr>';
					    }
					});
				    $("#chsInfoBody").html(chsHtml);
				    $("#chInfoDiv").unblock();
				}
			}
		});
		return false;
	});
});

