var posX = 0;
var posY = 0;
function getMouseXY(e) {
	posX = 0;
	posY = 0;
   var e = (!e) ? window.event : e;
   if (e.pageX || e.pageY) {
      posX = e.pageX;
      posY = e.pageY;
   }
   else if (e.clientX || e.clientY) {
      if (document.body.scrollLeft || document.body.scrollTop) {
	 posX = e.clientX + document.body.scrollLeft;
	 posY = e.clientY + document.body.scrollTop;
      }
      else {
	 posX = e.clientX + document.documentElement.scrollLeft;
	 posY = e.clientY + document.documentElement.scrollTop;
      }
   }
   //alert('x,y='+posX+','+posY);
}
function showPopUp(obj1,followme,offsetX,offsetY) {
	if (!offsetX) offsetX = -50;
	if (!offsetY) offsetY = 15;
	document.getElementById(obj1).style.display='none'; 
	if (followme!=-1) document.getElementById(obj1).style.left = offsetX + posX + 'px';
	if (followme==1) document.getElementById(obj1).style.top = offsetY + posY + 'px';
	document.getElementById(obj1).style.display='inline'; 
	return;
}
function hidePopUp(obj1) {
	document.getElementById(obj1).style.display='none'; 
	return false;
}
function showURL(url,title,sizeX,sizeY) {
	if (!sizeX) sizeX = 380;
	if (!sizeY) sizeY = 550;
	window.open(url,title,'width='+sizeX+',height='+sizeY+',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=no'); 
	return false;
}

function doValidation(theForm) {
	var msg = '';
	if (theForm.customers_firstname.value.length<2) {
		msg += 'Please enter your First Name\n';
	}
	if (theForm.customers_lastname.value.length<2) {
		msg += 'Please enter your Last Name\n';
	}

	if (msg.length>0) {
		alert(msg);
		return false;
	}
	// disable registration button
	//theForm.regbutton.disabled=true;  

	/* Allows only valid Email address. */
	var checkStr=theForm.email.value;
	if(checkStr=="" || checkStr.lastIndexOf("@")==-1 || checkStr.lastIndexOf(".")==-1)
	{
		alert("Please enter a valid Email\nWe do not sell or spam your Email\n");
		theForm.email.focus();
		return false;
	}

	var emailArr=checkStr.split("@");
	var user = new String( emailArr[0] );
	var domainAddress = new String( emailArr[1] );
	var domainAddressArr = domainAddress.split(".");
	var domain = new String( domainAddressArr[0] );
	var net = new String( domainAddressArr[1] );
	
	if(user.length==0)
	{
		alert("Please enter a valid Email address");
		theForm.email.focus();
		return false;	
	}

	if (theForm.telephone.value.length<9) {
		msg += 'Please enter your Phone Number\n';
	}
	if (theForm.customers_street_address.value.length<4) {
		msg += 'Please enter your Address\n';
	}
	if (theForm.customers_city.value.length<3) {
		msg += 'Please enter your City\n';
	}
	if (theForm.customers_state.value.length<2) {
		msg += 'Please enter your State\n';
	}
	if (theForm.customers_postcode.value.length<5) {
		msg += 'Please enter your ZipCode\n';
	}

	if ((theForm.pass1.value.length) && (theForm.pass1.value != theForm.pass2.value)) {
		alert("Please retype your password, and password confirmation.  Passwords do not match. ");
		theForm.pass1.focus();
		return false;
	}
	if (msg.length>0) {
		alert(msg);
		return false;
	}
	return true;
	// re-enable registration button
	//theForm.regbutton.disabled=false;
}

function doValidationRestaurant(theForm)
{
	var msg = '';
	if (theForm.manufacturers_name.value.length<2) {
		msg += 'Please enter Restaurant\'s Name\n';
	}
	if (theForm.manufacturers_street_address.value.length<2) {
		msg += 'Please enter Restaurant\'s Street Address\n';
	}
	if (theForm.manufacturers_city.value.length<3) {
		msg += 'Please enter Restaurant\'s City\n';
	}
	if (theForm.manufacturers_state.value.length<2) {
		msg += 'Please enter Restaurant\'s State\n';
	}
	if (theForm.manufacturers_zipcode.value.length<5) {
		msg += 'Please enter Restaurant\'s ZipCode\n';
	}

	if (msg.length>0) {
		alert(msg);
		return false;
	}
	// disable registration button
	//theForm.regbutton.disabled=true;  
	
	if (theForm.manufacturers_delivery_area.value.length<5) {
		msg += 'Please enter Restaurant\'s Delivery Area\n';
	}
	
	if (theForm.manufacturers_phone.value.length<9) {
		msg += 'Please enter Restaurant\'s Phone Number\n';
	}
	
	if (msg.length>0) {
		alert(msg);
		return false;
	}
	
	if (theForm.manufacturers_contact_name.value.length<2) {
		msg += 'Please enter a Contact Name\n';
	}
	
	if (theForm.manufacturers_contact_phone.value.length<9) {
		msg += 'Please enter a Contact Phone Number\n';
	}
	
	/* Allows only valid Email address. */
	var checkStr=theForm.manufacturers_contact_email.value;
	if(checkStr=="" || checkStr.lastIndexOf("@")==-1 || checkStr.lastIndexOf(".")==-1)
	{
		msg += 'Please enter a valid Contact Email\nWe do not sell or spam your Email\n';
		theForm.manufacturers_contact_email.focus();
	}
	
	if (msg.length>0) {
		alert(msg);
		return false;
	}

	var emailArr=checkStr.split("@");
	var user = new String( emailArr[0] );
	var domainAddress = new String( emailArr[1] );
	var domainAddressArr = domainAddress.split(".");
	var domain = new String( domainAddressArr[0] );
	var net = new String( domainAddressArr[1] );
	
	if(user.length==0)
	{
		msg += 'Please enter a valid Email address';
		theForm.manufacturers_contact_email.focus();
	}
	
	if (msg.length>0) {
		alert(msg);
		return false;
	}
		// re-enable registration button
	//theForm.regbutton.disabled=false;
}


/**
*The SlideShow object
*Arguments:
*slideel: the element that contains the slides
*faddingSpeed: anything starting from 1, the higher the slowest
*stopTime: the time in milliseconds for a slide to be displayed until it moves to the next slide
*stopOnMouseOver: if the animation should stop if the mouse is over the slides
*/
function SlideShow(slideel, faddingSpeed, stopTime, stopOnMouseOver) {
	var mouseIsOver = false; //mouseover flag
	
	//if we want to use "stopOnMouseOver"
	if (stopOnMouseOver) {
		//add an event handler to flag mouse is over when... mouse is over
		slideel.addEvent('mouseover', function() {
			mouseIsOver = true;
		});
 
		//flag mouse is not over, and finnaly call the next slide
		slideel.addEvent('mouseout', function() {
			mouseIsOver = false;
			self.next();
		});
	}
 
	this.next = function() { //go to the next slide
		if (mouseIsOver) //if mouse is over do nothing
			return;
 
		this.current.fadeOut();
		this.current = this.current.nextSlide;
		this.current.fadeIn();
	}
 
	var self = this; //there's a reason for this sentence, check at the end
 
	//this function creates the slides, does not need to be a function
	//but this way the arrays will be eliminated from memory after the function call
	function createSlides() {
		var imgs = slideel.getElementsByTagName('img');
		var slides = [];
        
		for (var i = 0; i < imgs.length; i++) {		
			slides[i] = new SlideShowImage(imgs[i], self);
		}
        
		for (var i = 0; i < slides.length; i++) {
			if (i == slides.length - 1)
				slides[i].nextSlide = slides[0];
			else
				slides[i].nextSlide = slides[i + 1];
		}
        
		self.current = 	slides[0]; //the current slide will be the first
		slides[0].fadeIn(); //fade it in
 
		//the SlideShowImage
		
		function SlideShowImage(img, slideShow) {
			img.style.opacity = '0'; //hide the image in firefox
			img.style.filter = 'alpha(opacity=0)'; //hide the image in ie
 
			//fadeIn and fadeOut methods, use the animation frame scheduling method, see at the end of the code
			
			this.fadeIn = function() {
				var i = 0;
				while (++i <= 50) {
					window.setTimeout(function() {
						addFade(0.02);
					}, i * faddingSpeed);
				}
 
				//after completely fadding in, wait "stopTime" ann call the next slide
				window.setTimeout(function() {
					slideShow.next();
				}, 50 * faddingSpeed + stopTime);
			}
 
			this.fadeOut = function() {
				var i = 0;
				while (++i <= 50) {
					window.setTimeout(function() {
						addFade(-0.02);
					}, i * faddingSpeed);
				}
			}
 
			//this function retrieves the opacity value in IE
			function getIeOpacity() {
				var opacity = img.style.filter.match(/alpha\(opacity=\d{0,3}\)/);
				if (!opacity)
					return 100;
				else {
					var value = parseInt(opacity[0].replace(/alpha\(opacity=/,'').replace(/\)/, ''));
					return value;
				}
			}
 
			function addFade(value) {
				//if (document.all) //if we're facing the evilness of IE
				if ((navigator.appVersion.indexOf("MSIE")!= -1) && !window.opera) //if we're facing the evilness of IE
					img.style.filter = 'alpha(opacity=' + (getIeOpacity() + value * 100) + ')';
				else
					img.style.opacity = parseFloat(img.style.opacity) + value;
			}
		}
	}
 
	createSlides(slideel); //create the slides
}


// next 3 functions are for showing/hiding divs.
function show_hide_non_member_doc_fields(val,id) {
	if (val == 0) { document.getElementById(id).style.display='block'; } 
	else { document.getElementById(id).style.display='none'; }
}

function show_hide_element(id) {
	cur_status = document.getElementById(id).style.display;
	if (cur_status == 'none') document.getElementById(id).style.display = "block";
	else document.getElementById(id).style.display = "none";
}

function set_unset_border(id,default_border,new_border,row_id) {
	el = document.getElementById(id);
	hidden_div = document.getElementById('hidden_details_div'+row_id).style.display;
	if (hidden_div != 'none') el.style.border = new_border;
	else el.style.border = default_border;
}

function run_lightbox(link) {
	var caller = document.createElement('a');
	caller.href = link;
	valid = new lightbox(caller);
	if (browser == 'Internet Explorer'){
		valid.getScroll();
		valid.prepareIE('100%', 'scroll');
		valid.setScroll(0,0);
		valid.hideSelects("hidden");
	}
	valid.displayLightbox("block");
}
// place the call of this function in the body tag, not in some parent element. !!!
function autorun_lightbox(link) {
	var caller = document.createElement('input');
	caller.className ='lbOn';
	caller.id ='lbOnAuto';
	caller.type = "hidden";
	caller.value = link;
	document.body.appendChild(caller);
}

function scroll_top(){
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		window.scroll(0,0);
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		document.body.scroll(0,0);
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		document.documentElement.scrollTop = 0;
	}
}

function flipCheckboxes(the_form, the_element, do_check)
{
    var elts      = (typeof(document.forms[the_form].elements[the_element]) != 'undefined')
                  ? document.forms[the_form].elements[the_element]
                  : (typeof(document.forms[the_form].elements[the_element]) != 'undefined')
          ? document.forms[the_form].elements[the_element]
          : document.forms[the_form].elements[the_element];
    var elts_cnt  = (typeof(elts.length) != 'undefined')
                 ? elts.length
                 : 0;
    if (elts_cnt) {
        for (var i = 0; i < elts_cnt; i++) {
            elts[i].checked = do_check;
        } // end for
    } else {
        elts.checked        = do_check;
    } // end if... else
    return true;
}



/*function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}*/
/*
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}*/
