function clearText(element){
	if(element.value == 'Name' || element.value == 'email@example.com') element.value = '';	
}
function rand( min, max ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Leslie Hoare
    // *     example 1: rand(1, 1);
    // *     returns 1: 1
 
    if( max ) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    } else {
        return Math.floor(Math.random() * (min + 1));
    }
}
function Gallery() {

	this.arrImages = Array();
	this.currentImg = 0;
	this.noImages = 0;
	
	this.registerImage = function( strFile, intImageNo ) {
	
		this.arrImages[intImageNo] = strFile;
		this.noImages++;
	
	}

	this.switchGallery = function( intImageNo ) {

		// get image elements		
		objMainImg = document.getElementById('main_gallery_image');
		objText = document.getElementById('img_text');
		objHeading = document.getElementById('img_heading');
		
		if( objMainImg.style.display == 'none' ) objMainImg.style.display = 'inline';
		
		strNewImage = this.arrImages[intImageNo];
		objNewText = document.getElementById('img_text_'+intImageNo);
		objNewHeading = document.getElementById('img_heading_'+intImageNo);
		strNewHeading = objNewHeading.innerHTML;
		strNewText = objNewText.innerHTML;
	
		// split new url with original filename on the end
		var arrTmp = objMainImg.src.split("\/m_");
	
		// change file to large copy
		arrTmp[arrTmp.length-1] = "m_"+strNewImage;
	
		// join source back together
		strTmp = arrTmp.join("/");

		// switch image
		objMainImg.src = strTmp;
		objText.innerHTML = strNewText;
		objHeading.style.display = 'block';
		objHeading.innerHTML = strNewHeading;
		this.currentImg = intImageNo;
		this.fixHeight();

	}
	
	this.randImg = function() {
	
		intRand = rand(1,this.arrImages.length);
		
		exit = false;
		intStart = 1;
		
		while( !exit ) {
			for( x in this.arrImages ) {
				if( intRand == intStart++ ) {
				
					this.switchGallery(x);
					exit = true;
				}
			}
		}
	
	}

	this.prevImage = function() {


		if( this.currentImg == 0 ) this.currentImg = this.noImages;
		
		this.currentImg--;
		
		this.switchGallery( this.currentImg );

	}

	this.nextImage = function() {

		if( this.currentImg == (this.noImages-1) ) {
			this.currentImg = 0;
		} else {
			this.currentImg++;
		}
		
		this.switchGallery( this.currentImg );

	}
	
	this.fixHeight = function() {
	
		//objBigImg = document.getElementById('main_gallery_image');
		
		//intH = objBigImg.offsetHeight;
		
		//objBigImg.style.top = "50%";
		//objBigImg.style.marginTop = "-"+(intH/2)+"px";
		
	}

}

objGallery = new Gallery();


function submitContact( objRef ) {
	
	strRef = ( typeof( objRef ) != 'object' ) ? objRef : objRef.id;
	objRef = ( typeof( objRef ) != 'object' ) ? document.getElementById(objRef) : objRef;

	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	strName = objRef.name.value;
	strEmail = objRef.email.value;
	strPhone = objRef.phone.value;

	if( strName == '' ) {
		alert( "Please fill out your name." );
		return;
	}
	
	if( strEmail == '' && strPhone == '' ) {
		alert( "Please fill out either your e-mail address, or phone number." );
		return;
	}

	if(strPhone == '' && (reg.test(strEmail) == false)) {
   			
      	alert('Please supply a valid e-mail address.');
      	return;
      	
   	}
   	
   	
	setTimeout("doSubmit('"+strRef+"');", 50);

}
function submitSubscribe( objRef ) {
	
	strRef = ( typeof( objRef ) != 'object' ) ? objRef : objRef.id;
	objRef = ( typeof( objRef ) != 'object' ) ? document.getElementById(objRef) : objRef;

	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	strName = document.getElementById('name').value;
	strEmail = document.getElementById('mski-mski').value;
	strCPhone = document.getElementById('CellPhone').value;
	strAddr = document.getElementById('MailingAddress').value;
	strCity = document.getElementById('City').value;
	strProv = document.getElementById('State/Province').value;
	strPostal = document.getElementById('Zip/PostalCode').value;
	strCountry = document.getElementById('Country').value;

	if( strName == '' || strEmail == '' || strCPhone == '' || strAddr == '' || strCity == '' || strProv == '' || strPostal == '' || strCountry == '' ) {
		alert( "Please fill out all required fields." );
		return;
	}
	if(reg.test(strEmail) == false) {
   			
      	alert('Please supply a valid e-mail address.');
      	return;
      	
   	}
   	
   	
	setTimeout("doSubmit('"+strRef+"');", 50);

}

function doSubmit( ref ) {
	document.getElementById(ref).submit();
}
function checkGcSubmit( objRef ) {
	objRef = typeof(objRef) == 'object' ? objRef : document.getElementById(objRef);
	arr = new Array();
	arr[0] = objRef.t_fname;
	arr[1] = objRef.t_lname;
	arr[2] = objRef.f_fname;
	arr[3] = objRef.f_lname;
	arr[4] = objRef.msg;
	arr[5] = objRef.amt;
	//email = objRef.rec_email.value;
	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	send = true;
	strMsg = '';
	
	for( x in arr ) {
		if(typeof(arr[x])!='object') continue;
		tmp = '';
		
		switch(Number(x)) {
			case 0: tmp = 'First Name'; break;
			case 1: tmp = 'Last Name'; break;
			case 2: tmp = 'First Name'; break;
			case 3: tmp = 'Last Name'; break;
			case 4: tmp = 'Your Custom Message'; break;	
			case 5: tmp = 0.00; break;
		}
		
		if( arr[x].value == '' || arr[x].value == tmp ) {
			
			strMsg = 'Please fill out all fields.  ';
			send = false;
			arr[x].style.backgroundColor = '#ffc7c7';
			
		} else {
			arr[x].style.backgroundColor = '#fff';
		}
		
	}
	/*

	if( objRef.send_to_rec.value == 'yes' ) {
		if(email=='' || (reg.test(email) == false)) {
			send = false;
			strMsg += 'Please supply a valid Email address.';
			objRef.rec_email.style.backgroundColor = '#ffc7c7';
		} else {
			objRef.rec_email.style.backgroundColor = "#fff";
		}
	}
   	
*/
	strRef = ( typeof( objRef ) != 'object' ) ? objRef : objRef.id;
	if(send) {
		setTimeout("doSubmit('"+strRef+"');", 50);
	} else {
		alert(strMsg);
		return false;
	}
}

function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   myfield.form.submit();
   return false;
   }
else
   return true;
}

function submitRsvp( objRef ) {
	
	strRef = ( typeof( objRef ) != 'object' ) ? objRef : objRef.id;
	objRef = ( typeof( objRef ) != 'object' ) ? document.getElementById(objRef) : objRef;

	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	strfName = objRef.fname.value;
	strlName = objRef.lname.value;
	strOrg = objRef.organization.value;
	strEmail = objRef.email.value;
	strPhone = objRef.phone.value;

	if( strfName == '' || strlName == '' ) {
		alert( "Please fill out your first and last name." );
		return;
	}
	if( strOrg == '' ) {
		alert( "Please fill out your organization." );
		return;
	}
	
	if( strEmail == '' && strPhone == '' ) {
		alert( "Please fill out either your e-mail address, or phone number." );
		return;
	}

	if(strPhone == '' && (reg.test(strEmail) == false)) {
   			
      	alert('Please supply a valid e-mail address.');
      	return;
      	
   	}
	
	if( objRef.guest.checked ) {
	
		strfName = objRef.guest_fname.value;
		strlName = objRef.guest_lname.value;
		strOrg = objRef.guest_organization.value;
		strEmail = objRef.guest_email.value;
		strPhone = objRef.guest_phone.value;
	

		if( strfName == '' || strlName == '' ) {
			alert( "Please fill out your guest's first and last name." );
			return;
		}
		if( strOrg == '' ) {
			alert( "Please fill out your guest's organization." );
			return;
		}
		
		if( strEmail == '' || strPhone == '' ) {
			alert( "Please fill out either your guest's e-mail address and phone number." );
			return;
		}
	
		if(reg.test(strEmail) == false) {
				
			alert('Please supply a valid e-mail address for your guest.');
			return;
			
		}
	
	}
   	
   	
	setTimeout("doSubmit('"+strRef+"');", 50);

}

	
function submitForm(strRef) {
	obj = $(strRef);
	
	req = $(strRef+' .required');
	
	num = req.length;
	
	send = true;
	strMsg = '';
	
	for( x = 0; x < num; x++ ) {
	
		val = req.get(x).value;
		
		if( val == '' ) {
			
			req.eq(x).css("background-color","#ffc7c7");
			send = false;
			strMsg = 'Please fill out all required fields.';
			
		} else {
		
			req.eq(x).css("background-color","#fff");
			
		}
	
	}
	
	if(send) {
		obj.submit();
	} else {
		alert(strMsg);
		return false;
	}

}

$(document).ready(function() {

	$("a.rsvp").click(function(e){
		e.preventDefault();
		
		obj = $("#"+this.id+"_div");
		
		if(obj.css('display')=='none') {
			obj.slideDown();
			$(this).text('Hide this form');
		} else {
			obj.slideUp();
			$(this).text('Send RSVP');
		}
		
	});
	
	// any images with the class 'hover' will add _ovr to the filename on hover,  also preloads images to be hovered
	$("img.hover").hover(function(e) {this.h = this.src;var tmp = this.h.split('/');var tmp2 = tmp[tmp.length-1].split('.');tmp2[0] += '_ovr';tmp2 = tmp2.join('.');tmp[tmp.length-1] = tmp2;var href = tmp.join('/');this.src = href;},function(){this.src = this.h;});i=$("img.hover");num=i.length;for(x=0;x<num;x++){$("<img>").attr("src",i.eq(x).src);}
	
	$('#paypalSubmit').keyup(function(e) {
		//alert(e.keyCode);
		if(e.keyCode == 13) {
			email = $("#email").get(0).value;
			id = $("[name=conf]").get(0).value;
			
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(email == '' || reg.test(email) == false) {
				alert("Please supply a valid Email Address");
				return;
			}
			
			$.ajax({   
				url: "ajax.php",
				cache: false,
				data: "page=update_gc&email="+email+"&id="+id,
				success: function(html){	
					$("#button_submit").click();
				}
			});
		}
	});
	
	// do an ajax call before submitting to paypal
	$("#fake_button").click(function() {
		email = $("#email").get(0).value;
		id = $("[name=conf]").get(0).value;
		
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(email == '' || reg.test(email) == false) {
			alert("Please supply a valid Email Address");
			return;
		}
		
		$.ajax({   
			url: "ajax.php",
			cache: false,
			data: "page=update_gc&email="+email+"&id="+id,
			success: function(html){	
				$("#button_submit").click();
			}
		});
		
	});

	// ajax function
	function ajax_replace(strPage,strVars,strDest,strExt) {
		loading(strDest,strExt);
		
		$.ajax({   
			url: strPage,
			cache: false,
			data: strVars,
			success: function(html){	
				$(strDest).html(html);
				$(strDest).get(0).scrollTop = 0;
				done_loading();
			}
		});
		
	}
	// overlays a "loading" screen on top of the elements provided as arguments (accepts jquery selectors, but try not to select multiple elements with one selector) until done_loading is called
	// CSS value for transparency not suppported by IE7 and below; I'm pretty sure it just breaks in IE at the moment
	function loading() {
		
		done_loading();
		
		for( x = 0; x < arguments.length; x++ ) {
		
			// get dimensions of the target
			tmp = $(arguments[x]);
			
			// make sure it exists
			if(tmp.length == 0) continue;
			
			// get dimensions and position
			h = tmp.height();
			w = tmp.width();
			t = tmp.position().top;
			l = tmp.position().left;
			
			
			// put a loading animation over the first element
			if(x==0)
				tmp = $('<div class="loading_screen"><span style="display: block; padding: 5px; margin: 10px; width: 65px; border: solid 2px #666; background: #fff">Loading <img style="vertical-align: middle;" src="images/ajax-loader.gif" alt="" /></span></div>');
			else
				tmp = $('<div class="loading_screen"></div>');
					
			// don't break <= IE7
			if(!jQuery.browser.msie){		
				tmp.css("background-color","rgba(255,255,255,0.8)");
			} else {
				tmp.css("background-color","#fff");
			}
			
			// insert box
			tmp.css("position","absolute")
				.css("height",h+"px")
				.css("width",w+"px")
				.css("top",t+"px")
				.css("left",l+"px");
			$("body").append(tmp);
		}	
		
	}
	function done_loading() {
		$(".loading_screen").remove();
	}

	// IE6 does not like what we do here, so don't do it	
	if(!(jQuery.browser.msie && (jQuery.browser.version.substr(0,1)=="6"))){
		$("a.submenu").click(function(e) {
			e.preventDefault();
			
			tmp = this.href.split('?');
			ajax_replace("ajax.php","page=page_content&"+tmp[tmp.length-1],"#bottom .m", "#bottom .r");
			
			tmp = this.href.split("cat=");
			img = $('<img src="admin/user_files/images/category/side_'+tmp[tmp.length-1]+'.jpg" />');
			$("#bottom .r").html(img);
			
			$("a.submenu").removeClass("selected");
			$(this).addClass("selected");
		
		});
	}
	
	$("select[name='gc_types']").change(function() {
	
		img = $("#gc_image").get(0);
		
		tmp = img.src.split('/');
		tmp[tmp.length-1] = this.value;
		
		img.src = tmp.join('/');
	
	});
	$('#gc_types').change();
	
	$("input[name='guest']").change(function() {
	
		if(this.checked) {
			$(".guest").slideDown();
		} else {
			$(".guest").slideUp();
		}
	
	});

});