/*************************************************/
/*				BOLUS A LA TUCKER				*/
/*************************************************/
function putItBack(){
	//No re-typing fart
	var hash = getUrlVars();
	if (hash['restaurant'] != undefined)
		document.getElementById('restaurant').value = hash['restaurant'].replace(/%20/, " ");
	if (hash['address'] != undefined)
		document.getElementById('address').value = hash['address'].replace(/%20/, " ");
	if (hash['phone'] != undefined)
		document.getElementById('phone').value = hash['phone'].replace(/%20/, " ");
	if (hash['fax'] != undefined)
		document.getElementById('fax').value = hash['fax'].replace(/%20/, " ");
	if (hash['fname'] != undefined)
		document.getElementById('fname').value = hash['fname'].replace(/%20/, " ");
	if (hash['lname'] != undefined)
		document.getElementById('lname').value = hash['lname'].replace(/%20/, " ");
	if (hash['email'] != undefined)
		document.getElementById('email').value = hash['email'].replace(/%40/, "@");
	if (hash['username'] != undefined)
		document.getElementById('username').value = "";
	if (hash['confirmuser'] != undefined)
		document.getElementById('confirmuser').value = "";
	if (hash['paypal'] != undefined)
		document.getElementById('paypal').value = hash['paypal'];
}

function getUrlVars()
{
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++){
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}

function formValidator(){
	// Make quick references to our fields
	var restaurant = document.getElementById('restaurant');
	var address = document.getElementById('address');
	var phone = document.getElementById('phone');
	var fax = document.getElementById('fax');
	var fname = document.getElementById('fname');
	var lname = document.getElementById('lname');
	var email = document.getElementById('email');
	var username = document.getElementById('username');
	var confirmuser = document.getElementById('confirmuser');
	var paypal = document.getElementById('paypal');
	
	// Check each input in the order that it appears in the form!
	if(isAlphabet(restaurant, "Please enter only letters for Restaurant Name")){
		if(isAlphanumeric(address, "Numbers and Letters Only for Address")){
			if(isNumeric(phone, "Please enter a valid phone number")){
				if(isNumeric(fax, "Please enter a valid fax number")){ // this is the IsFax? segment! madniss.... may need to be deleted to allow the ass to enter here or not
					if(isAlphabet(fname, "Please enter only letters for your first name")){
						if(isAlphabet(lname, "Please enter only letters for your last name")){
							if(emailValidator(email, "Please enter a valid email address")){
								if(lengthRestriction(username, 6, 8)){
									//if(isSame(username, confirmuser, "Please enter the same password as in \"username\"")){
										if(madeSelection(paypal, "Please Choose One!")){
											//return true; to be used if need for self subscribtion
											//popUp('pop-up-create-account-thanks.html'); //orginally there but now put on the first page with a get label
											location.href = "senddem.php?restaurant="+restaurant.value+"&address="+address.value+"&phone="+phone.value+"&fax="+fax.value+"&fname="+fname.value+"&lname="+lname.value+"&email="+email.value+"&paypal="+paypal.value+"&username="+username.value;
										}
									//}
								}
							}
						}
					}
				}
			}
		}
	}	
	return false;
}

/*function isSame(elem1, elem2, helperMsg){
	if(elem1.value.match(elem2)){
		return true;
	}else{
		alert(helperMsg);
		elem2.focus();
		return false;
	}
}*/

function isEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus();
		return true;
	}
	return false;
}

function isNumeric(elem, helperMsg){
	var numericExpression = /^[-0-9 ]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z ]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[-0-9a-zA-Z ]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Please adjust your USERNAME to be between " +min+ " and " +max+ " characters");
		elem.focus();
		return false;
	}
}

function madeSelection(elem, helperMsg){
	if(elem.value == "Please Choose"){
		alert(helperMsg);
		elem.focus();
		return false;
	}//BULLSHIT!
	else if((elem.value == "Online: Paypal") || (elem.value == "Mail: cheque")){
		alert("You can enable \"FREE\" instead");
		elem.focus();
		return false;
	}///////////////////////////////////////
	else
	{
		return true;
	}
}

function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

/***************************************************************************************************************************************************************/

function deleteMeLast(value,from,t_super) {
	location.href = "add-your-restaurant_enter-menu.php?from="+from+"&deleteThisLast="+value+"&subCategoryType="+t_super+"&menuitems="+from+"#menuitems";	
}

function deleteMeSub(value,from) {
	location.href = "add-your-restaurant_enter-menu.php?from="+from+"&deleteThisSub="+value+"&subCategoryType="+from+"#subcategory";	
}

function changeSubCategoryTitle(value) {
	location.href = "add-your-restaurant_enter-menu.php?subCategoryType="+value+"#subcategory";
}

function changeLastCategoryTitle(value,from) {
	location.href = "add-your-restaurant_enter-menu.php?subCategoryType="+from+"&menuitems="+value+"#menuitems";
}

function saveLast(oldType,oldPrice,t_super,from) {
	var newType = document.getElementById('EditSubCategory').value;
	var newPrice = document.getElementById('NewPrice').value;
	location.href = "pop-up-edit-menuitem.php?newType="+newType+"&newPrice="+newPrice+"&oldType="+oldType+"&type="+newType+"&price="+newPrice+"&from="+from+"&super="+t_super+"&menuitems="+from+"&subCategory="+t_super+"&oldPrice="+oldPrice;
	opener.location.reload(true);
	opener.location.href = "add-your-restaurant_enter-menu.php?subCategoryType="+t_super+"&menuitems="+from+"#menuitems";		
}

function saveSub(oldType,from) {
	var newType = document.getElementById('EditSubCategory').value;
	location.href = "pop-up-edit-subcategory.php?newType="+newType+"&oldType="+oldType+"&type="+newType+"&subCategory="+from+"&from="+from;
	opener.location.reload(true);
	opener.location.href = "add-your-restaurant_enter-menu.php?subCategoryType="+from+"#subcategory";
}

function save(oldType) {
	var newType = document.getElementById('EditSubCategory').value;
	location.href = "pop-up-edit-category.php?newType="+newType+"&oldType="+oldType+"&type="+newType;
	opener.location.reload(true);
	opener.location.href = "add-your-restaurant_enter-menu.php";
}

function neatclose(){
	opener.location.reload(true);
	window.close();
}

function deleteMe(value) {
	location.href = "add-your-restaurant_enter-menu.php?deleteThis="+value;	
}

function addMe(value) {
	var myTextField = document.getElementById(value);
	if(value == 'EnterCategory')
	{
		location.href = "add-your-restaurant_enter-menu.php?value="+myTextField.value+'#addCat';
	}
	if(value == 'EnterSubCategory')
	{
		var mySubHeader = document.getElementById('SubHeader').innerHTML;
		location.href = "add-your-restaurant_enter-menu.php?subCategoryType="+mySubHeader+"&newCategoryType="+myTextField.value+"#subcategory";
	}
}

function addMeLast(value,price) {
	var myTextField1 = document.getElementById(value);
	var myTextField2 = document.getElementById(price);
	var mySubHeader = document.getElementById('SubHeader').innerHTML;
	var myLastHeader = document.getElementById('LastHeader').innerHTML;
	location.href = "add-your-restaurant_enter-menu.php?subCategoryType="+mySubHeader+"&menuitems="+myLastHeader+"&newMenuItem="+myTextField1.value+"&newPriceItem="+myTextField2.value+"#menuitems";
}

function change(obj) {
	if (!(/.jpg$/.test(obj.value))) {
		if (obj.value != "")
			alert('I am sorry, but that last entry was NOT a Jpg file. Please try again.');
	}
}

function tucker_popup(location) {
	window.open( location, "Image Cropper", "status = 0, height = 600, width = 650, resizable = 0" );
}

function weather_popup(location) {
	window.open( location, "Weather", "toolbar=0, status = 0, height = 350, width = 800, resizable = 0" );
}

//NEWS CRUD RELATED//////////////////////////////////////////////////////
function updateCrud(location) {
	window.open( location, "Update", "scrollbars=1, toolbar=0, status = 0, height = 800, width = 950, resizable = 0" );
}

function popupX(mylink, windowname)
{
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
	   href=mylink;
	else
	   href=mylink.href;
	window.open(href, windowname, 'width=850,height=800,scrollbars=yes');
	return false;
}
////////////////////////////////////////////////////////
function tucker_popup4() {

//	window.open( "ult_slide.php", "SlideShow", "status = 0, height = 630, location=0, width = 852, resizable = 0" ); //begrudgingly made

day = new Date();
	id = day.getTime();
//	eval("page" + id + " = window.open(ult_slide.php, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=852,height=630,left = 10,top = 10');");
}

function popupWindow() {

//	window.open( "ult_slide.php", "SlideShow", "status = 0, height = 630, location=0, width = 852, resizable = 0" ); //begrudgingly made

day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(ult_slide.php, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=852,height=630,left = 10,top = 10');");
}
function tucker_popup3() {
	window.open( "phpSlide.php", "SlideShow", "status = 0, height = 550, location=0, width = 620, resizable = 0" ); //begrudgingly made
}

function tucker_popup2(URL) 
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=475,height=280,left = 10,top = 10');");
}

function jslideshow(who) {
	where = "slideshow.php?restaurant="+who;
	window.open( where, "Slideshow", "scroll = 0, location = 1, status = 1, height = 500, width = 550, resizable = 1" );
}

function submitform()
	{
  		document.forms[0].submit();
	}

function resetform()
	{
  		document.forms[0].reset();
	}




function houdini(title, direction){
	var my_locale=location.href;
	var my_array=my_locale.split("=");
	if(my_array[1] != null)
	{
		if(my_array[1] != -1)
		{
			document.write("<tr>");
			document.write("<td><a href=\""+direction+"\" class=\"blue-menu\">"+title+"</a></td>");
			document.write("<td><img src=\"images/5x5.gif\" width=\"5\" height=\"20\" /></td>");
			document.write("<td width=\"30\" align=\"center\" valign=\"middle\" bgcolor=\"#1996D0\"><div align=\"center\"><a href=\""+direction+"\" class=\"main-wht\"><strong>go</strong></a></div></td>");
			document.write("</tr>");
		}
	}
}
/*************************************************/
/*				FIN A LA TUCKER				*/
/*************************************************/