function Trim(s) 
{
  // Remove leading spaces and carriage returns
  
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }

  // Remove trailing spaces and carriage returns

  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}


function createOption(name) {
	var o = new Option(name, name, false, false);
	return o;
}

function addC3(name) {
	document.editform.cat3.options[document.editform.cat3.length] = createOption(name);
}

function addC2(name) {
	document.editform.cat2.options[document.editform.cat2.length] = createOption(name);
}


function changeOne() {
	var index = document.editform.cat1.options[document.editform.cat1.selectedIndex].value;
	fillInCat2(index);	
}

function changeTwo() {
	var index = document.editform.cat2.options[document.editform.cat2.selectedIndex].value;
	fillInCat3(index);	
}

function addNumToSteps(e) {
	var key;
	if(window.event) {
		// for IE, e.keyCode or window.event.keyCode can be used
		key = e.keyCode; 
	}
	else if(e.which) {
		// netscape
		key = e.which; 
	}
	else {
		// no event, so pass through
		return true;
	}

	if (key == '13') {		
		var s_array = document.editform.steps.value.split("\n");
		document.editform.steps.value = "";
		var index = 0;
		for (i = 0; i < s_array.length; i++) {	
			step = s_array[i];
			if (step.indexOf(".") < 5)  {
				step = step.substring(step.indexOf(".") + 1, step.length);
			}			
			step = Trim(step);
			if (step == "" && i ==  s_array.length-1) {
				continue;
			}
			//alert(step);
			if (index > 0) {
				document.editform.steps.value = document.editform.steps.value + "\n" + (index+1) + ".  " + step;
			} else {
				document.editform.steps.value =  "1.  " + step;
			}
			index++;
		
		}
		
		if (index == 0) {
			document.editform.steps.value = document.editform.steps.value  + (index+1) + ".  ";
		} else {
			document.editform.steps.value = document.editform.steps.value  + "\n" + (index+1) + ".  ";
		}
		document.editform.steps.scrollTop = document.editform.steps.scrollHeight;
	}
	document.editform.minorchange.value = "false";
}

function addNumToTips(e) {
	var key;
	if(window.event) {
		// for IE, e.keyCode or window.event.keyCode can be used
		key = e.keyCode; 
	}
	else if(e.which) {
		// netscape
		key = e.which; 
	}
	else {
		// no event, so pass through
		return true;
	}

	if (key == '13') {
		var s_array = document.editform.tips.value.split("\n");
		document.editform.tips.value = "";
		var index = 0;
		for (i = 0; i < s_array.length	; i++) {	
			step = s_array[i];
			if (step.indexOf("*") < 5)  {
				step = step.substring(step.indexOf("*") + 1, step.length);
			}
			step = Trim(step);
			if (step == "" && i ==  s_array.length-1) {
				continue;
			}
			if (i > 0) {
				document.editform.tips.value = document.editform.tips.value +  "\n*  " + step;
			} else {
				document.editform.tips.value =  "*  " + step;
			}
			index++;
		}
		if (index == 0) {
			document.editform.tips.value = document.editform.tips.value  + "*  ";
		} else {
			document.editform.tips.value = document.editform.tips.value  + "\n*  ";
		}
		document.editform.tips.scrollTop = document.editform.tips.scrollHeight;
	}
	document.editform.minorchange.value = "false";
}

function addStars(e, element) {
	var key;
	if(window.event) {
		// for IE, e.keyCode or window.event.keyCode can be used
		key = e.keyCode; 
	}
	else if(e.which) {
		// netscape
		key = e.which; 
	}
	else {
		// no event, so pass through
		return true;
	}

	if (key == '13') {
		var s_array = element.value.split("\n");
		element.value = "";
		var index = 0;
		for (i = 0; i < s_array.length; i++) {	
			step = s_array[i];
			if (step.indexOf("*") < 5)  {
				step = step.substring(step.indexOf("*") + 1, step.length);
			}
			step = Trim(step);
			if (step == "" && i ==  s_array.length-1) {
				continue;
			}
			if (i > 0) {
				element.value = element.value +  "\n*  " + step;
			} else {
				element.value =  "*  " + step;
			}
			index++;
		}
		if (index == 0) {
			element.value = element.value  + "*  ";
		} else {
			element.value = element.value  + "\n*  ";
		}
		element.scrollTop = element.scrollHeight;
	}
	
	document.editform.minorchange.value = "false";
}

function addCategories() {
	options = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=500,height=350";
	popupWindow = window.open("cat.php", "", options);
	if (!popupWindow.opener) popupWindow.opener = self;
}

function removeCategories() {
	url = "cat.php?action=remove&cats=" + escape(document.editform.categories.value);
	options = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=500,height=300";
	popupWindow = window.open(url, "", options);
	if (!popupWindow.opener) popupWindow.opener = self;
}

function checkForm() {
	if (document.editform.title && document.editform.title.value == "") {
		alert ('Please enter a title for your wikiHow.');
		document.editform.title.focus();
		return false;
	}
	if (document.editform.title &&  document.editform.title.value.indexOf(".") >= 0) {
		alert ('The follow characters are not allowed in the title: .');
		document.editform.title.focus();
		return false;
	}	
	if (document.editform.title &&  document.editform.title.value.indexOf("?") >= 0) {
		alert ('The follow characters are not allowed in the title: ?');
		document.editform.title.focus();
		return false;
	}
	if (document.editform.title &&  document.editform.title.value.indexOf(":") >= 0) {
		alert ('The follow characters are not allowed in the title: :');
		document.editform.title.focus();
		return false;
	}
	if (document.editform.summary.value == "") {
		alert ('Please enter a summary for your wikiHow.');
		document.editform.summary.focus();
		return false;
	}
	
	if (document.editform.title) {
		output = document.editform.title.value.replace(" ", "-");
		document.editform.action += "&title=" + output;
	}
	
	document.editform.related_list.value = "";
	if (document.editform.related) {
        for(var f=0; f<document.editform.related.length; ++f)
		  document.editform.related_list.value += document.editform.related.options[f].value + "|";
        }   
	return true;
}

function imagePopup (url, e) {
    popupWindow=open(url, null, 'scrollbars=no,status=no,width=400,height=400');
    if (popupWindow.opener != null) popupWindow.opener = self;        
    else alert ("Oops! A pop-up blocker may have blocked this window. Please add ehow.com to your list of allowed sites for your pop-up blocker.");
}

