// General JavaScript Functions



function popCenter(URL,name,w,h) {

	l = (screen.width - w) / 2;

	t = (screen.height - h) / 2;

	window.open(URL,name,'width='+w+',height='+h+',left='+l+',top='+t+',toolbars=0,scrollbars=0,location=0,statusbars=0,menubars=1,resizable=1'); 

}





function ShowDish(URL) {
	setTimeout("window.location.href='/show_dish.asp?l="+encodeURI(URL)+"';", 0);
}

function SaveButton(imgelement, mode) {
	if (imgelement.src.lastIndexOf('/images/saved_btn.gif') != -1) { return true; }
	switch (mode) {
		case "ON":
			imgelement.src = '/images/save_btn_on.gif';
			break; 
		case "OFF":
			imgelement.src = '/images/save_btn_off.gif';
			break;
	}
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}


function enter(evt) {
  //firefox
  var key = (window.Event) ? evt.which : evt.keyCode;
  if (key == 13) {
	 //alert("Enter");
	 doSubmit('login');
  }

  //IE
  if (window.event && window.event.keyCode == 13) {
	doSubmit('login');
  }
}







function changeTabs(tab,isover) {
	// this helper function wraps the changeImages call
	// requres Prototype lib
	var img;
	var ploadimg;
	var iobj;
	var icursor;

	// exit if the tab is selected or processing
	iobj = $(tab);
	icursor = iobj.style.cursor;
	if (icursor=='' || icursor=='default' || icursor=='wait') { return true;}
	
	// process the action
	switch(tab) {
		case 'tab0':
			img = 'all_results';
			break;
		case 'tab1':
			img = 'starters';
			break;
		case 'tab2':
			img = 'entrees';
			break;
		case 'tab3':
			img = 'desserts';
			break;
	}
	if (isover) {
		img = '/images/'+img+'_on.gif';
	} else {
		img = '/images/'+img+'_off.gif';
	}	
	changeImages(tab, img);
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		go_btn_over = newImage("/images/go_btn-over.jpg");
		signup_btn_over = newImage("/images/signup_btn-over.jpg");
		take_a_tour_over = newImage("/images/take_a_tour_over.gif");
		about_over = newImage("/images/about_over.gif");
		contact_over = newImage("/images/contact_over.gif");
		
		//tabs
		all_results_over = newImage("/images/all_results_on.gif");
		starters_over = newImage("/images/starters_on.gif");
		entrees_over = newImage("/images/entrees_on.gif");
		desserts_over = newImage("/images/desserts_on.gif");
		all_results = newImage("/images/all_results_off.gif");
		starters = newImage("/images/starters_off.gif");
		entrees = newImage("/images/entrees_off.gif");
		desserts = newImage("/images/desserts_off.gif");
		
		//serach results
		save_over = newImage("/images/save_btn_on.gif");
		
		//register
		submit_over = newImage("/images/submit_btn_over.gif");

		//Kitchen/Exclude Listing tabs
		tab_1 = newImage("/images/kitchen_bg_tab_1.gif");
		tab_2 = newImage("/images/kitchen_bg_tab_2.gif");
		tab_over = newImage("/images/kitchen_bg_tab_over.gif");
		
		//Keyword Search Img Btn
		search_kw_btn = newImage("/images/search_kw_btn.gif");
		search_kw_btn_over = newImage("/images/search_kw_btn_over.gif");
		cancel_kw_btn = newImage("/images/cancel_kw_btn.gif");
		cancel_kw_btn_over = newImage("/images/cancel_kw_btn_over.gif");
		
		//go
		go_btn = newImage("/images/go_btn_over.gif");
		
		preloadFlag = true;
	}
}

function clearDefaultandCSS(q) {
	if(q.value=='Enter Ingredient') {
		q.value='';
		q.style.color = '#73ae1e';
	}
	if(q.value=='Enter Keyword') {
		q.value='';
		q.style.color = '#FF9900';
	}
}

function clearDefaultandCSS_ex(q) {
	if(q.value=='Enter Ingredient') {
		q.value='';
		q.style.color = '#FF0012';
	}
}

function setDefaultandCSS(q) {
	if(q.value=='') {
		q.value='Enter Ingredient';
		q.style.color = '#CCCCCC';
	}
}



function showChangePwd() {
	var d = document.getElementById('pwd_form');
	if (d) {d.style.display='block';}
	
	var d = document.getElementById('pwd_link');
	if (d) {d.style.display='none';}
	//focus on the form field
	document.pwd_update.newpass.focus();
}

function hideChangePwd() {
	var d = document.getElementById('pwd_form');
	if (d) {d.style.display='none';}
	
	var d = document.getElementById('pwd_link');
	if (d) {d.style.display='block';}
}


function doSubmit(myform,uid){
	
	var formString = eval('document.'+myform);
 	//validate registration form
	if (myform == "registerForm") {
		validateRegForm(uid);
	} else {
		formString.submit();
	}

}

function validateRegForm(uid) {
	//username
	if (uid=="") {
		if ($("reg_username").value == "") {
    		alert( "Please create a username." );
    		$("reg_username").focus();
    		return false ;
  		}
	}
	
	//password------------
	if (uid=="") {
		if ($("reg_password").value == "") {
			alert( "Please create a password." );
			$("reg_password").focus();
			return false ;
		}
		if ($("reg_password_confirm").value == "") {
			alert( "Please confirm your password." );
			$("reg_password_confirm").focus();
			return false ;
		}
		
		if ($("reg_password_confirm").value != $("reg_password").value) {
			alert( "Your password entries are different!" );
			$("reg_password_confirm").focus();
			return false ;
		}
	}
	//end password--------------------------
	
	
	//email------------------this.value.toLowerCase();">
	if (!checkEmail($("reg_email").value)) { 
		alert( "Please enter a valid email address." );
		$("reg_email").focus();
		return false;
	}

	
	//return true;
	document.registerForm.submit();
	
}


function checkEmail(addy) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(addy)){
		return (true);
	}
}




//This array is used to remember mark status of rows in browse mode
var marked_row = new Array;


/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object    the table row
 * @param   interger  the row number
 * @param   string    the action calling this script (over, out or click)
 * @param   string    the default background color
 * @param   string    the color to use for mouseover
 * @param   string    the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)

{
 	//alert("sup");
	var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('div');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function

function leapto(form) {
var myindex=form.dest.selectedIndex;

parent.right.location.href=(form.dest.options[myindex].value);
myindex = 0; // reset pulldown menu
}

// Start Cookie Code (Public Domain)
function getCookie( name ) {
  var start = document.cookie.indexOf( name + "=" );
  var len = start + name.length + 1;
  if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
    return null;
  }
  if ( start == -1 ) return null;
  var end = document.cookie.indexOf( ";", len );
  if ( end == -1 ) end = document.cookie.length;
  return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
  //alert(value);
  var today = new Date();
  today.setTime( today.getTime() );
  if ( expires ) {
    expires = expires * 1000 * 60 * 60 * 24;
  }
  var expires_date = new Date( today.getTime() + (expires) );
  document.cookie = name+"="+escape( value ) +
    ( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

function deleteCookie( name, path, domain ) {
  if ( getCookie( name ) ) document.cookie = name + "=" +
    ( ( path ) ? ";path=" + path : "") +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
// End Cookie Code (Public Domain)

