function grab_code(name, url, type){

code = 	"Code for "+name+"<br/>"+
		"<textarea style='width:400px; height:100px;'  onclick='this.focus();this.select();' readonly><script type=\"text/javascript\"> "+
		"url='" +url+"'; "+
		"type='"+type+"'; "+
		"</script> "+
		"<script src=\"http://www.wikindex.com/plugin/script.js\" type=\"text/javascript\"></script></textarea><br/>"+
		"put this code to a php file found in your wiki. e.g. 'monobook.php' for mediawiki";
	

	$.prompt(code,{buttons: {close:'close'}});
}

function tr_hover(id,wiki_id){
	document.getElementById(id).className = "tr_hover";
}

function tr_out(id){
	document.getElementById(id).className = "tr_normal";
}

function about_us(){
	document.getElementById('add_form_div').style.display = "block";
	addForm = ''+
	'<div id="about_form">'+
	'<a href="javascript: close_form();" id="close">close</a>'+
	'<h3>about wikindex.com</h3>'+
	'<br/>'+
	'<p style="text-indent:20px;">'+
	'<a href="http://wikindex.com" style="text-decoration:none;">'+
	'<span style="color:#99CC33!important;">wiki</span><span style="color:#006699!important;">ndex.com</span>'+
	'</a>'+
	' shows the relative activity level of selected wiki sites.  The goal is to find those wikis with the most active community, which should lead to the best information.  It is also a good place to search for a wiki on a given subject: skiing, linux, history, biology, pokemon cards.  If you don\'t see a wiki here, please go ahead and <a href="javascript:add_a_wiki();" style="color:#006699; text-decoration:none;">add a wiki</a>.  Our spider will pick up the information automatically, and update this index once each day.  If you have any questions or comments, feel free to mail us via the <a href="javascript:contact_us();" style="color:#006699; text-decoration:none;">contact us</a> form.'+
	'</p>'+
	'<a href="http://wikindex.com" style="text-decoration:none;">'+
	'<span style="color:#99CC33!important;">wiki</span><span style="color:#006699!important;">ndex.com</span>'+
	'</a>'+
	' is built by <a href="http://davejenkins.com" style="text-decoration:none;" target="_blank"><span style="color:#006699!important;">Dave Jenkins</span></a> and <span style="color:#006699!important;">Matt Libo-on</span>'+
	'</div>'+
	'';
	document.getElementById('add_form_div').innerHTML = addForm;

}

function contact_us(){
	document.getElementById('add_form_div').style.display = "block";
	addForm = ''+
	'<div id="contact_form">'+
		'<table>'+
			'<tr>'+
				'<td></td>'+
				'<td><a href="javascript: close_form();" id="close">close</a></td>'+
			'</tr>'+
			'<tr>'+
				'<td>Name:</td>'+
				'<td><input type="text" size="30" id="contact_name"></td>'+
			'</tr>'+
			'<tr>'+
				'<td>Email:</td>'+
				'<td><input type="text" size="30" id="contact_email"></td>'+
			'</tr>'+
			'<tr>'+
				'<td>Comment:</td>'+
				'<td><textarea id="contact_comment"></textarea></td>'+
			'</tr>'+
			'<tr>'+
				'<td></td>'+
				'<td align="center"><input type="image" src="images/send.gif" onclick="send_comment()"></td>'+
			'</tr>'+
		'</table>'+
		'<div id="contact_message"></div>'+
	'</div>'+
	'';
	document.getElementById('add_form_div').innerHTML = addForm;
}

function check_contact_input(){
	if(trimAll(document.getElementById('contact_name').value)  == ""){
		return "Please enter your <span style=\"color:#AF0017;\">NAME</span>.";
	}
	else if(email_checker('contact_email') == false){
		return "Please enter a valid <span style=\"color:#AF0017;\">EMAIL ADDRESS</span>.";
	}
	else if(trimAll(document.getElementById('contact_comment').value) == ""){
		return "Please add your <span style=\"color:#AF0017;\">COMMENT</span>.";
	}
	else{
		return true;
	}
}

function email_checker(elementID){
	if(trimAll(document.getElementById(elementID).value) == ""){
		return false;
	}
	else{
		if(document.getElementById(elementID).value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi)){
			return true;
		}
		else{
			return false;
		}
	}
}

function send_comment(){
	if(check_contact_input() == true){
		contact_xmlhttpPost("ajax/email.php","contact_message","<img src=\"images/loading.gif\">");
	}
	else{
		document.getElementById('contact_message').innerHTML = check_contact_input();
	}
	
}

function contact_xmlhttpPost(strURL,elementID,outputString) {
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	document.getElementById(elementID).innerHTML = outputString;	
	
		self.xmlHttpReq.onreadystatechange = function() {
			if (self.xmlHttpReq.readyState == 4) {
					document.getElementById(elementID).innerHTML = self.xmlHttpReq.responseText;
					if(document.getElementById(elementID).innerHTML == "Message sent"){
						window.location = "index.php";
					}
			}
		}
		self.xmlHttpReq.send(contact_getQueryString());
		
}

function contact_getQueryString() {
	inputString = document.getElementById('contact_name').value +"|"
				 +document.getElementById('contact_email').value+"|"
				 +document.getElementById('contact_comment').value;

	qstr = 'input= ' + inputString;
	return qstr;
}


function close_form(){
	document.getElementById('add_form_div').style.display = "none";
}

function trimAll(sString){ 
     while (sString.substring(0,1) == ' '){ 
          sString = sString.substring(1, sString.length); 
     } 
     while (sString.substring(sString.length-1, sString.length) == ' '){ 
          sString = sString.substring(0,sString.length-1); 
     } 
     return sString; 
}

function add_a_wiki(){
	document.getElementById('add_form_div').style.display = "block";
	addForm = ''+
	'<div id="add_form">'+
		'<table>'+
			'<tr>'+
				'<td></td>'+
				'<td><a href="javascript: close_form();" id="close">close</a></td>'+
			'</tr>'+
			'<tr>'+
				'<td>Wiki Software:</td>'+
				'<td style="font-size:10px; color:#006699">'+
				'<select id="wikiSoftware">'+
				'<option value="MediaWiki">MediaWiki</option>'+
				'<option value="TikiWiki">TikiWiki</option>'+
				'<option value="DekiWiki">DekiWiki</option>'+
				'</select>'+
				'</td>'+
			'</tr>'+
			'<tr>'+
				'<td>URL:</td>'+
				'<td><input type="text" size="30" id="add_url"  onFocus="this.value = \'http://\'" maxlength="100"></td>'+
			'</tr>'+
			'<tr>'+
				'<td>Name:</td>'+
				'<td><input type="text" size="30" id="add_name" maxlength="50"></td>'+
			'</tr>'+
			'<tr>'+
				'<td>Short Description:</td>'+
				'<td><input type="text" size="30" id="add_desc"></td>'+
			'</tr>'+
			'<tr>'+
				'<td></td>'+
				'<td align="center"><input type="image" src="images/add.gif" onclick="add_wiki()"></td>'+
			'</tr>'+
		'</table>'+
		'<div id="add_message"></div>'+
	'</div>'+
	'';
	document.getElementById('add_form_div').innerHTML = addForm;
}

function check_add_input(){
	if(trimAll(document.getElementById('add_url').value)  == ""){
		return "Please enter a <span style=\"color:#AF0017;\">URL</span>.";
	}
	else if(trimAll(document.getElementById('add_name').value) == ""){
		return "Please enter the <span style=\"color:#AF0017;\">NAME</span> of the site.";
	}
	else if(trimAll(document.getElementById('add_desc').value) == ""){
		return "Please enter a <span style=\"color:#AF0017;\">SHORT DESCRIPTION</span> about the site.";
	}
	else{
		return true;
	}				
}

function add_wiki(){
	if(check_add_input() == true){
		xmlhttpPost("ajax/insert_wiki.php","add_message","<img src=\"images/loading.gif\">");
	}
	else{
		document.getElementById('add_message').innerHTML = check_add_input();
	}
	
}

function xmlhttpPost(strURL,elementID,outputString) {
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	document.getElementById(elementID).innerHTML = outputString;	
	
		self.xmlHttpReq.onreadystatechange = function() {
			if (self.xmlHttpReq.readyState == 4) {
					document.getElementById(elementID).innerHTML = self.xmlHttpReq.responseText;
					if(document.getElementById(elementID).innerHTML == "Insert Successful"){
						window.location = "index.php";
					}
			}
		}
		self.xmlHttpReq.send(getQueryString());
		
}

function getQueryString() {
	inputString = catch_wikipedia_url(document.getElementById('add_url').value) +"|"
				 +document.getElementById('add_name').value+"|"
				 +document.getElementById('add_desc').value+"|"
				 +document.getElementById('wikiSoftware').value;

	qstr = 'input= ' + inputString;
	return qstr;
}


function catch_wikipedia_url(url){
var re = /wikipedia\.org/;

	if(re.test(url)){
		return url.replace(/\/wiki\/.*/, "/wiki/Main_Page");
	}
	else{
		return url;
	}
}

function wiki_software(wiki){
	software = new Array('Mediawiki','TikiWiki','DekiWiki');

	document.getElementById('hidden_software').value = wiki;
	document.getElementById(wiki).style.color = "#AF0017";
	document.getElementById(wiki).innerHTML = wiki;

	for(i=0;i<software.length;i++){
		if(software[i] != wiki){
			document.getElementById(software[i]).innerHTML = "<a href=\"javascript: wiki_software('"+software[i]+"');\">"+software[i]+"</a>";
		}
	}
	
	wiki_software_xmlhttpPost("ajax/wiki_software.php","data");
}

function wiki_software_xmlhttpPost(strURL,elementID) {
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	document.getElementById('loading').innerHTML = "processing<br/><img style=\"margin-left:10px;\" src=\"images/loading.gif\">";		
	
		self.xmlHttpReq.onreadystatechange = function() {
			if (self.xmlHttpReq.readyState == 4) {
				document.getElementById(elementID).innerHTML = self.xmlHttpReq.responseText;
				document.getElementById('loading').innerHTML = "";
			}
		}
		self.xmlHttpReq.send(get_wiki_software_QueryString());
		
}

function get_wiki_software_QueryString() {
	inputString = document.getElementById('hidden_software').value;

	qstr = 'input= ' + inputString;
	return qstr;
}

function wiki_search_software(wiki){
	software = new Array('Mediawiki','TikiWiki','DekiWiki');
	document.getElementById('hidden_search_software').value = wiki;
	document.getElementById(wiki).style.color = "#AF0017";
	document.getElementById(wiki).innerHTML = wiki;

	for(i=0;i<software.length;i++){
		if(document.getElementById(software[i]) != null){
			if(software[i] != wiki){
				document.getElementById(software[i]).innerHTML = "<a href=\"javascript: wiki_search_software('"+software[i]+"');\">"+software[i]+"</a>";
			}
		}
	}
	
	wiki_search_software_xmlhttpPost("ajax/wiki_software_search.php","search_data");
}

function wiki_search_software_xmlhttpPost(strURL,elementID) {
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	document.getElementById('loading').innerHTML = "processing<br/><img style=\"margin-left:10px;\" src=\"images/loading.gif\">";		
	
		self.xmlHttpReq.onreadystatechange = function() {
			if (self.xmlHttpReq.readyState == 4) {
				document.getElementById(elementID).innerHTML = self.xmlHttpReq.responseText;
				document.getElementById('loading').innerHTML = "";
			}
		}
		self.xmlHttpReq.send(get_wiki_search_software_QueryString());
		
}

function get_wiki_search_software_QueryString() {
	inputString = document.getElementById('hidden_search_software').value;
	qrystr = document.getElementById('hidden_querystr').value;

	qstr = 'input= ' + inputString+ '|' + qrystr;
	return qstr;
}

