function addToMyYahoo (url, name, publisher, output) {
	if (output == null) output = true;
	var html = '<a class="my_yahoo" target="_blank" href="http://us.rd.yahoo.com/my/atm/'
	+ publisher + '/' + name + "/*http://add.my.yahoo.com/rss?url="
	+ encodeURIComponent(url) + '">'
	+ '<img src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif" width="91" height="17" border="0" align="middle" alt="Add to My Yahoo!"></a>';
	if (output) document.write(html);
	else return html;
}

function biggerBrowse(num) {
	var browse = document.getElementById('browse_ecards').style;
	if (num == 1) browse.height = '400px';
	else browse.height = '255px'
}

function blurIt (id, default_txt) {
	var obj = document.getElementById(id);
	if (!obj.value.length) obj.value = default_txt;
}

function changeVisibility (objectID, newVisibility) {
	var styleObject = getStyleObject(objectID);
	if (styleObject) {
		styleObject.visibility = newVisibility;
		return true;
	} else {
		return false;
	}
}

function checkAge(bDay) {
	now = new Date()
	 bD = bDay.split('/');
	 if(bD.length==3){
	   born = new Date(bD[2], bD[1]*1-1, bD[0]);
	   years = Math.floor((now.getTime() - born.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
	   return years + 1;
	 }
}

function checkEmail (email, invalidaddress) {
	var testresults;
	var invalidcheck = 0;
	var str = email;
	var filter = /^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str)) {
		var tempstring = str.split("@");
		tempstring = tempstring[1].split(".");
		if (invalidaddress != null) {
			for (i=0; i<invalidaddress.length; i++){
				if (tempstring[0]==invalidaddress[i]) invalidcheck = 1;
			}
		}
		if (invalidcheck != 1) return true;
		else return false;
	} else return false;
}

function checkPersonalForm (form_name, send_ecard, next_step) {
	var ok			= true;
	var form_id		= document.getElementById("form_name").innerHTML;
	var form		= document.getElementById(form_id);
	
	if (form_id == 'invite_form'){
		var fields	= new Array('bride','groom','sender_name','sender_email','friend_name','friend_email');
		for (var i=0; i<fields.length; i++) {
			if (fields[i] == 'friend_email') {
				if(!checkEmail(form[fields[i]].value)) {
					form[fields[i]].style.background = '#FFE2DF';
					$('form_error').show();
					ok = false;
				} else form[fields[i]].style.background = '#FFFFFF';
			} else if (fields[i] == 'sender_email') {
				if(!checkEmail(form[fields[i]].value)) {
					form[fields[i]].style.background = '#FFE2DF';
					$('form_error').show();
					ok = false;
				} else form[fields[i]].style.background = '#FFFFFF';
			} else {
				if (form[fields[i]].value == '') {
					form[fields[i]].style.background = '#FFE2DF';
					$('form_error').show();
					ok = false;
				} else form[fields[i]].style.background = '#FFFFFF';
			}
		}
	} else if (form_id == 'propose_form') {
		var fields	= new Array('fiance_name','fiance_email','sender_name','sender_email');
		for (var i=0; i<fields.length; i++) {
			if (fields[i] == 'fiance_email') {
				if(!checkEmail(form[fields[i]].value)) {
					form[fields[i]].style.background = '#FFE2DF';
					$('form_error').show();
					ok = false;
				} else form[fields[i]].style.background = '#FFFFFF';
			} else if (fields[i] == 'sender_email') {
				if(!checkEmail(form[fields[i]].value)) {
					form[fields[i]].style.background = '#FFE2DF';
					$('form_error').show();
					ok = false;
				} else form[fields[i]].style.background = '#FFFFFF';
			} else {
				if (form[fields[i]].value == '') {
					form[fields[i]].style.background = '#FFE2DF';
					$('form_error').show();
					ok = false;
				} else form[fields[i]].style.background = '#FFFFFF';
			}
		}
	} else if (form_id == 'crash_form') {
		var fields	= new Array('friend_name','friend_email','sender_name','sender_email');
		for (var i=0; i<fields.length; i++) {
			if (fields[i] == 'friend_email') {
				if(!checkEmail(form[fields[i]].value)) {
					form[fields[i]].style.background = '#FFE2DF';
					$('form_error').show();
					ok = false;
				} else form[fields[i]].style.background = '#FFFFFF';
			} else if (fields[i] == 'sender_email') {
				if(!checkEmail(form[fields[i]].value)) {
					form[fields[i]].style.background = '#FFE2DF';
					$('form_error').show();
					ok = false;
				} else form[fields[i]].style.background = '#FFFFFF';
			} else {
				if (form[fields[i]].value == '') {
					form[fields[i]].style.background = '#FFE2DF';
					$('form_error').show();
					ok = false;
				} else form[fields[i]].style.background = '#FFFFFF';
			}
		}
	}
	
	if (ok == true && send_ecard == true && next_step != '') {
		sendEcard(form_id, next_step);
	} else if (ok == true && next_step != '') {
		$('ecard_preview').show();
		if (form_id == 'invite_form') {
			var bride 			= form.bride.value;
			var groom 			= form.groom.value;
			// var bride_parent_1	= form.bride_parent_1.value;
			// var bride_parent_2	= form.bride_parent_2.value;
			// var groom_parent_1	= form.groom_parent_1.value;
			// var groom_parent_2	= form.groom_parent_2.value;
			// var wedding_day		= form.wedding_day.value;
			// var wedding_month	= form.wedding_month.value;
			// var wedding_year	= form.wedding_year.value;
			var sender_name		= form.sender_name.value;
			var sender_email	= form.sender_email.value;
			var friend_name		= form.friend_name.value;
			var friend_email	= form.friend_email.value;
			var ecard			= document.getElementById('e_id').innerHTML;
   
			var url		= project_path + "inc/ajax/ecard_preview.php?";
			var parameters =
					'bride=' + encodeURIComponent(bride) +
					'&groom=' + encodeURIComponent(groom) +
					// '&bride_parent_1=' + encodeURIComponent(bride_parent_1) +
					// '&bride_parent_2=' + encodeURIComponent(bride_parent_2) +
					// '&groom_parent_1=' + encodeURIComponent(groom_parent_1) +
					// '&groom_parent_2=' + encodeURIComponent(groom_parent_2) +
					// '&wedding_day=' + encodeURIComponent(wedding_day) +
					// '&wedding_month=' + encodeURIComponent(wedding_month) +
					// '&wedding_year=' + encodeURIComponent(wedding_year) +
					'&sender_name=' + encodeURIComponent(sender_name) +
					'&sender_email=' + encodeURIComponent(sender_email) +
					'&friend_name=' + encodeURIComponent(friend_name) +
					'&friend_email=' + encodeURIComponent(friend_email) +
					'&ecard=' + encodeURIComponent(ecard);
					
			nextStep(3);
			document.getElementById('iframe_detail').src = url + parameters;
		
		} else if (form_id == 'propose_form') {
			var fiance_name		= form.fiance_name.value;
			var fiance_email	= form.fiance_email.value;
			var sender_name		= form.sender_name.value;
			var sender_email	= form.sender_email.value;
			var ecard			= document.getElementById('e_id').innerHTML;

			var url = project_path + "inc/ajax/ecard_preview.php?";
			var parameters =
					'sender_name=' + encodeURIComponent(sender_name) +
					'&sender_email=' + encodeURIComponent(sender_email) +
					'&fiance_name=' + encodeURIComponent(fiance_name) +
					'&fiance_email=' + encodeURIComponent(fiance_email) +
					'&ecard=' + encodeURIComponent(ecard);
			nextStep(3);
			document.getElementById('iframe_detail').src = url + parameters;
		
		} else if (form_id == 'crash_form') {
			var friend_name		= form.friend_name.value;
			var friend_email	= form.friend_email.value;
			var sender_name		= form.sender_name.value;
			var sender_email	= form.sender_email.value;
			var ecard			= document.getElementById('e_id').innerHTML;

			var url = project_path + "inc/ajax/ecard_preview.php?";
			var parameters =
					'sender_name=' + encodeURIComponent(sender_name) +
					'&sender_email=' + encodeURIComponent(sender_email) +
					'&friend_name=' + encodeURIComponent(friend_name) +
					'&friend_email=' + encodeURIComponent(friend_email) +
					'&ecard=' + encodeURIComponent(ecard);
			nextStep(3);
			document.getElementById('iframe_detail').src = url + parameters;
		}
	} else return ok;
}

function checkSweeps () {
	var form	= document.getElementById('sweeps_signup');
	var fields	= new Array('email','fname','lname','home_address1','home_city','home_zip','v20744_19371','birthmonth','birthday','birthyear','home_state');
	var ok		= true;
	
	var birthmonth	= form.birthmonth.value;
	var birthday	= form.birthday.value;
	var birthyear	= form.birthyear.value;
	
	for (var i=0; i<fields.length; i++) {
		if (fields[i] == 'email') {
			if(!checkEmail(form[fields[i]].value)) {
				form[fields[i]].style.background = '#FFE2DF';
				$('sweeps_error').show();
				ok = false;
			} else form[fields[i]].style.background = '#FFFFFF';
		} else {
			if (form[fields[i]].value == '') {
				form[fields[i]].style.background = '#FFE2DF';
				$('sweeps_error').show();
				ok = false;
			} else form[fields[i]].style.background = '#FFFFFF';
		}
	}
	
	if ((checkAge(birthmonth+"/"+birthday+"/"+birthyear)) <= 21) {
		alert('Sorry, you must be at least 21 years old to join.');
		ok = false;
		return;
	}
	
	if (ok == true) {
		$('sweeps_error').hide();
		alert("We're good.");
	}
	
}

//Bookmark on del.icio.us
function deliciousBookmark (targetURL, titleStr) {
	var loc = location.href;
	if (targetURL && targetURL.length>0) loc = targetURL;
	var apos = loc.indexOf('#');
	loc = (apos>0?loc.substring(0,apos):loc);
	var t = document.title;
	if (titleStr && titleStr.length) t = titleStr;
	location.href='http://del.icio.us/post?v=2&url=' 
	+ encodeURIComponent(loc)
	+ '&title='
	+ encodeURIComponent(t);
	return false;
}

//Bookmark on Facebook
function facebookBookmark (targetURL, titleStr) {
	var loc = location.href;
	if (targetURL && targetURL.length>0) loc = targetURL;
	var apos = loc.indexOf('#');
	loc = (apos>0?loc.substring(0,apos):loc);
	var t = document.title;
	if (titleStr && titleStr.length) t = titleStr;
	var myhref='http://www.facebook.com/sharer.php?u=' 
	+ encodeURIComponent(loc)
	+ '&title='
	+ encodeURIComponent(t);
	window.open(myhref,'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

//bookmark on digg
function diggBookmark (url, title, body, media, topic) {
	var loc = location.href;
	var submitTitle = document.title;
	if (url && url.length>0) loc = url;
	if (title && title.length>0) submitTitle = title;
	var apos = loc.indexOf('#');
	loc = (apos>0?loc.substring(0,apos):loc);
	var strloc = 'http://digg.com/submit?url=' + encodeURIComponent(loc)
	+ '&title=' + encodeURIComponent(submitTitle)
	+ '&bodytext=' + encodeURIComponent(body);
	if (media && media.length) strloc += '&media=' + encodeURIComponent(media)
	if (topic && topic.length) strloc += '&topic=TOPIC' + encodeURIComponent(topic);
	location.href= strloc;
	return false;
}

function focusIt (id, default_txt) {
	var obj = document.getElementById(id);
	if (obj.value == default_txt) obj.value = '';
}

function getStyleObject (objectId) {
	// cross-browser function to get an object's style object given its id
  if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
		return document.getElementById(objectId).style;
  } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
		return document.all(objectId).style;
  } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
		return document.layers[objectId];
  } else {
		return false;
  }
} // getStyleObject

function getQueryVariable (variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) return pair[1];
	}
}

function hideId(id) {
	document.getElementById(id).style.display = 'none';
}

function numbersonly (myfield, e, dec) {
	var key;
	var keychar;
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	   
	keychar = String.fromCharCode(key);
	
	if ((key==null) || (key==0) || (key==8) || 
		(key==9) || (key==13) || (key==27) )
	   return true;
	
	else if ((("0123456789").indexOf(keychar) > -1))
	   return true;
	
	else if (dec && (keychar == "."))
	   {
	   myfield.form.elements[dec].focus();
	   return false;
	   }
	else
	   return false;
}

//open a centered popup
function popup(url,width,height,title,scrollbar,resizable,center) {

	//set defaults
	if (width == null) width = 600;
	if (height == null) height = 450;
	if (title == null) title = 'auto_popup';
	if (scrollbar == null) scrollbar = false;
	if (resizable == null) resizable = false;
	if (center == null) center = true;

	//set props
	scrollbar = scrollbar == true ? 'yes' : 'no';
	resizable = resizable == true ? 'yes' : 'no';
	winProperties = 'height='+height+',width='+width+',toolbar=no,location=no,scrollbars='+scrollbar+',titlebar=no,menubar=no,resizable='+resizable+',status=no';

	//open and move
	win = window.open(url,title, winProperties);
	if (!win) alert('Please disable your popup blocker to use this site.');
	else if (center) {
		if (self.screen) {
			sw = self.screen.width;
			sh = self.screen.height;
		} else return;
		win.moveTo((sw-width)/2,(sh-height)/2);
	}

}

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 "";
}

//get browser size
//http://www.quirksmode.org/viewport/compatibility.html
function getWindowSize() {
	var x,y;
	
	// all except Explorer
	if (self.innerHeight) {
		x = self.innerWidth;
		y = self.innerHeight;
	// Explorer 6 Strict Mode
	} else if (document.documentElement && document.documentElement.clientHeight) {
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	// other Explorers
	} else if (document.body) {
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return new Array(x,y);
}

function nextStep (step_id) {
	for (i=1; i<=4; i++) {
		var step = document.getElementById('step_' + i).style;
		var head = document.getElementById('head_' + i).style;
		
		if (step_id == i) {
			step.display = 'block';
			head.color = '#cf0000';
		} else {
			step.display = 'none';
			head.color = '#97978B';
			if (step_id > 1) changeVisibility('done-step-' + (step_id - 1), 'visible');
		}
	}

}

function resetEcard() {
	// hide green checkmarks
	for (i=1; i<=4; i++) {
		changeVisibility('done-step-'+i, 'hidden');
	}
	
	// reset forms on step 2
	document.getElementById('invite_form').reset();
	document.getElementById('propose_form').reset();
	document.getElementById('crash_form').reset();
	
	
	setTabs(1,'page',3);
	setTabs(1,'card',12,'invite',1);
	setForms(1,'forms',3);
	nextStep(1); // back to step one
}

// DYNAMIC RESIZING OF FLASH MOVIES DONE USING THIS:
// http://hossgifford.com/downloads.htm
// I did make some changes to it though
function resizeSWF(id,nWidth,nHeight) {
	if (nWidth == null || !nWidth) nWidth = '';
	if (nHeight == null || !nHeight) nHeight = '';
	nWidth = nWidth.toString();
	nHeight = nHeight.toString();
	if (nWidth.length > 0) {
		if (nWidth.substr(nWidth.length-2) != 'px' && nWidth.substr(nWidth.length-1) != '%') nWidth += "px";
		document.getElementById(id).style.width = nWidth;
		document.getElementById(id+'_div').style.width = nWidth;
	}
	if (nHeight.length > 0) {
		if (nHeight.substr(nHeight.length-2) != 'px' && nHeight.substr(nHeight.length-1) != '%') nHeight += "px";
		document.getElementById(id).style.height = nHeight;
		document.getElementById(id+'_div').style.height = nHeight;
	}
}

function selectAll (id) {
    document.getElementById(id).focus();
    document.getElementById(id).select();
}

function selectShare (id) {
	for(i=1; i<=3; i++) {
		if (i == id) {
			document.getElementById('badges_' + i).style.display = 'block';
			if (i == 1) {
				document.getElementById('myspace_1').checked = 'checked';
			} else if (i == 2) {
				document.getElementById('facebook_1').checked = 'checked';
			} else document.getElementById('blog_1').checked = 'checked';
		} else document.getElementById('badges_' + i).style.display = 'none';
	}
}

function sendEcard (form_id, next_step) {
	var form		= document.getElementById(form_id);
	
	if (form_id == 'invite_form') {
		var bride 			= form.bride.value;
		var groom 			= form.groom.value;
		var sender_name		= form.sender_name.value;
		var sender_email	= form.sender_email.value;
		var friend_name		= form.friend_name.value;
		var friend_email	= form.friend_email.value;
		var ecard			= document.getElementById('e_id').innerHTML;
  
		var url = project_path + "inc/ajax/sendmail.php";
		new Ajax.Request(url, {
			onSuccess: function(transport) {
				$('ecard_preview').hide();
				changeVisibility('done-step-4', 'visible');
				nextStep(next_step);
			},
			parameters:
				'bride=' + encodeURIComponent(bride) +
				'&groom=' + encodeURIComponent(groom) +
				'&sender_name=' + encodeURIComponent(sender_name) +
				'&sender_email=' + encodeURIComponent(sender_email) +
				'&friend_name=' + encodeURIComponent(friend_name) +
				'&friend_email=' + encodeURIComponent(friend_email) +
				'&form_id=' + encodeURIComponent(form_id) +
				'&ecard=' + encodeURIComponent(ecard)
		});
	} else if (form_id == 'propose_form') {
		var fiance_name		= form.fiance_name.value;
		var fiance_email	= form.fiance_email.value;
		var sender_name		= form.sender_name.value;
		var sender_email	= form.sender_email.value;
		var ecard			= document.getElementById('e_id').innerHTML;
		
		var url = project_path + "inc/ajax/sendmail.php";
		new Ajax.Request(url, {
			onSuccess: function(transport) {
				$('ecard_preview').hide();
				changeVisibility('done-step-4', 'visible');
				nextStep(next_step);
			},
			parameters:
				'sender_name=' + encodeURIComponent(sender_name) +
				'&sender_email=' + encodeURIComponent(sender_email) +
				'&fiance_name=' + encodeURIComponent(fiance_name) +
				'&fiance_email=' + encodeURIComponent(fiance_email) +
				'&form_id=' + encodeURIComponent(form_id) +
				'&ecard=' + encodeURIComponent(ecard)
		});
		
	} else if (form_id == 'crash_form') {
		var friend_name		= form.friend_name.value;
		var friend_email	= form.friend_email.value;
		var sender_name		= form.sender_name.value;
		var sender_email	= form.sender_email.value;
		var ecard			= document.getElementById('e_id').innerHTML;
		
		var url = project_path + "inc/ajax/sendmail.php";
		new Ajax.Request(url, {
			onSuccess: function(transport) {
				$('ecard_preview').hide();
				changeVisibility('done-step-4', 'visible');
				nextStep(next_step);
			},
			parameters:
				'sender_name=' + encodeURIComponent(sender_name) +
				'&sender_email=' + encodeURIComponent(sender_email) +
				'&friend_name=' + encodeURIComponent(friend_name) +
				'&friend_email=' + encodeURIComponent(friend_email) +
				'&form_id=' + encodeURIComponent(form_id) +
				'&ecard=' + encodeURIComponent(ecard)
		});
	}
}

function setForms (tab_id, tab_prefix, tab_total) {
	for (i=1; i<=tab_total; i++) {
		var ccontent = tab_prefix + '_form_' + i;
		if (tab_id == i) {
			if (tab_id == 1) {
				document.getElementById('form_name').innerHTML = 'invite_form';
				$('link1').style.color = "#D00E0E";
				$('link2').style.color = "#97978B";
				$('link3').style.color = "#97978B";
			} else if (tab_id == 2) {
				document.getElementById('form_name').innerHTML = 'propose_form';
				$('link2').style.color = "#D00E0E";
				$('link1').style.color = "#97978B";
				$('link3').style.color = "#97978B";
			} else if (tab_id == 3) {
				document.getElementById('form_name').innerHTML = 'crash_form';
				$('link3').style.color = "#D00E0E";
				$('link2').style.color = "#97978B";
				$('link1').style.color = "#97978B";
			}
			document.getElementById(ccontent).style.display = 'block';
		} else {
			document.getElementById(ccontent).style.display = 'none';
		}
	}
}

function setPreview (ecard_type, ecard_id) {
	var ecard = "<img src=\"" + ecard_img_path + "card" + ecard_id + "-" + ecard_type + "-preview.jpg" +"\" alt=\"\" />";
	document.getElementById('e_num').innerHTML = ecard;
	document.getElementById('e_num_2').innerHTML = ecard;
	document.getElementById('e_id').innerHTML = "ecard" + ecard_id + "-" + ecard_type;
}

function setTabs (tab_id, tab_prefix, tab_total, ecard_type, ecard_id) {
	for (i=1; i<=tab_total; i++) {
		var ccontent = tab_prefix + '_detail_' + i;
		if (tab_id == i) {
			document.getElementById(ccontent).style.display = 'block';
			setPreview(ecard_type, ecard_id);
		} else {
			document.getElementById(ccontent).style.display = 'none';
		}

	}
}

function submitShare () {
	var form = document.forms['badge_share'];
	var badge = getCheckedValue(form.elements['thestyle']);
	var share_location = getCheckedValue(form.elements['share_location']);
	var share_content = getCheckedValue(form.elements['share_content']);
	var txtField = document.getElementById('txtCode');
	var code = '<div><embed id="vegasweddingbadge_' + share_location + '" src="'+ badges_path +'badge.swf" type="application/x-shockwave-flash" wmode="transparent" width="300" height="250" flashvars="style=' + badge + '&location=' + share_location + '&feed=' + share_content + '" />'
	+ '<script>try{var flashObj = document.getElementById("vegasweddingbadge_'+ share_location + '");flashObj.SetVariable("pageURL", document.location);}catch(err){}</script>'
	+ '<div style="font-family:Verdana;font-size:11px;font-weight:bold">Get your own badge at <a style="color:#ce0000; text-decoration:underline" href="http://valentinesinvegas.yahoo.com/share.php">http://valentinesinvegas.yahoo.com</a></div></div>';
	txtField.value = code;
}

//stop 100% flash movie from shrinking below a certain size
function swfSizeMin(swf_id,min_w,min_h) {

	//set it up to update
	window.onresize = function() {
		swfSizeMin(swf_id,min_w,min_h)
	}
	
	//get window size
	var sizes = getWindowSize();
	var win_w = sizes[0];
	var win_h = sizes[1];

	//figure out what to set flash size to
	if (win_w < min_w) var w = min_w;
	else var w = "100%";
	if (win_h < min_h) var h = min_h;
	else var h = "100%";

	//set the size
	resizeSWF(swf_id,w,h);

}

function toggleDisplay (obj_id, display_mode) {
	if (document.getElementById) {
		var obj = document.getElementById(obj_id);
		if (display_mode != null) obj.style.display = display_mode;
		else {
			if (obj.style.display == 'none') obj.style.display = '';
			else obj.style.display = 'none';
		}
		return obj.style.display;
	}
}

function ymsngr (msg) {
	var user = prompt('Type the Yahoo! ID of the user you would like to share with:');
	if (user != null) {
		if (user.length)
			window.location = 'ymsgr:sendim?'+user+"&m="+msg;
	}
	return false;
}