// JavaScript Document
var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
var numericExpression = /^[0-9]+$/;


function insert_default_value(id, value) {
	if (document.getElementById(id).value.length==0) {
		document.getElementById(id).value=value
	}
}
function clear_default_value(id, value) {
	if (document.getElementById(id).value==value) {
		document.getElementById(id).value=""
	}
}
function mv_validate_topform() {
	if (document.getElementById('validate_which').value == 'email') {
		return mv_validate_news_email()
	} else {
		return mv_validate_search_text()
	}
}
function  mv_validate_news_email() {
		if(document.getElementById('news_email').value.length < 1){
			alert('Please enter your email address.');
			document.getElementById('news_email').focus();
			return false;
		}
		if (emailfilter.test(document.getElementById('news_email').value)==false) {
			alert("Please enter a valid email address.")
			document.getElementById('news_email').select()
			return false;
		}
}
function  mv_validate_search_text() {
		if(document.getElementById('search').value.length < 1){
			alert('Please enter a keyword to search for.');
			document.getElementById('search').focus();
			return false;
		}
		if(document.getElementById('search').value == document.getElementById('search_default').value){
			alert('Please enter a keyword to search for.');
			document.getElementById('search').focus();
			return false;
		}
}
function  mv_validate_donation() {
		if(document.getElementById('donation_amount_ot').checked){
			if(document.getElementById('donation_amount_other').value.length < 1){
				alert('Please enter a value for your donation.');
				document.getElementById('donation_amount_other').focus();
				return false;
			}
			if (numericExpression.test(document.getElementById('donation_amount_other').value)==false) {
				alert("Please enter a value in whole numbers only.")
				document.getElementById('donation_amount_other').select()
				return false;
			}
			if (document.getElementById('donation_amount_other').value < 1) {
				alert("Please enter a value greater than 0.")
				document.getElementById('donation_amount_other').select()
				return false;
			}
		}
		if(document.getElementById('first_name').value.length < 1){
			alert('Please enter your first name.');
			document.getElementById('first_name').focus();
			return false;
		}
		if(document.getElementById('last_name').value.length < 1){
			alert('Please enter your last name.');
			document.getElementById('last_name').focus();
			return false;
		}
		if(document.getElementById('email').value.length < 1){
			alert('Please enter your email address.');
			document.getElementById('email').focus();
			return false;
		}
		if (emailfilter.test(document.getElementById('email').value)==false) {
			alert("Please enter a valid email address.")
			document.getElementById('email').select()
			return false;
		}
		if(document.getElementById('telephone').value.length < 1){
			alert('Please enter your telephone number.');
			document.getElementById('telephone').focus();
			return false;
		}
		if(document.getElementById('address_1').value.length < 1){
			alert('Please enter the first line of your address.');
			document.getElementById('address_1').focus();
			return false;
		}
		if(document.getElementById('town').value.length < 1){
			alert('Please enter your town.');
			document.getElementById('town').focus();
			return false;
		}
		if(document.getElementById('county').value.length < 1){
			alert('Please enter your county.');
			document.getElementById('county').focus();
			return false;
		}
		if(document.getElementById('postcode').value.length < 1){
			alert('Please enter your postcode.');
			document.getElementById('postcode').focus();
			return false;
		}
}

function mv_validate_registration() {
	if(document.register_form.first_name.value.length < 1){
		alert('Please enter your first name.');
		document.register_form.first_name.focus();
		return false;
	}
	if(document.register_form.last_name.value.length < 1){
		alert('Please enter your last name.');
		document.register_form.last_name.focus();
		return false;
	}
	if (emailfilter.test(document.getElementById('email').value)==false) {
		alert("Please enter a valid email address.")
		document.getElementById('email').select()
		return false;
	}
	if(document.register_form.password.value.length < 1){
		alert('Please enter a password.');
		document.register_form.password.focus();
		return false;
	}
	if(document.register_form.password.value != document.register_form.confirm_password.value){
		alert('The passwords you entered do not match.');
		document.register_form.confirm_password.select();
		return false;
	}
	if(document.register_form.security_checkword.value.length < 1){
		alert('Please enter a security checkword.');
		document.register_form.security_checkword.focus();
		return false;
	}
	if (document.register_form.agreed_terms) {
		if(document.register_form.agreed_terms.checked == false){
			alert('You must have read and agreed to the catch22 Terms & Conditions before registration can be completed.');
			document.register_form.agreed_terms.focus();
			return false;
		}
	}
}

function set_sub_menu_height(margin,height,div,list) {
	document.getElementById(list).style.top = '-'+margin+'px'
	document.getElementById(div).style.height = height+'px'
	if (margin > 0) {
		margin = margin-2
		height = height+2
		if (margin == -1) {
			margin = 0
			height = height+1
		}
	setTimeout("set_sub_menu_height("+margin+","+height+",'"+div+"','"+list+"');",1)
	}
}
function expand_sub_menu(div,list) {
	if (navigator.userAgent.indexOf("MSIE 5") == -1) { // dont run if IE5
		//if (document.getElementById(list) && document.getElementById(div) {
		off_height = document.getElementById(list).offsetHeight
		//alert(off_height)
		//alert(document.getElementById(list).style.position)
		document.getElementById(list).style.position = 'absolute'
		document.getElementById(list).style.top = '-'+off_height+'px'
		
		set_sub_menu_height(off_height,0,div,list)
		//}
	}
}
function findPosition( oElement ) {
  if( typeof( oElement.offsetParent ) != 'undefined' ) {
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
   // return [ posX, posY ];
    return [ posY ];
  } else {
   // return [ oElement.x, oElement.y ];
    return [ oElement.y ];
  }
}
function float_footer() {
	//alert(document.getElementById('footer_p').screenX)
	if (navigator.userAgent.indexOf("MSIE 6") == -1) { // dont run if IE6
		body_height = document.body.offsetHeight
		footer_offset = findPosition( document.getElementById('footer') )
		document.getElementById('footer_p').style.position = 'absolute'
		//alert(body_height)
		//alert(footer_offset)
		if (body_height > footer_offset) {
			//alert('move');
			//alert(document.getElementById('footer_p').style.top)
			new_top = body_height-footer_offset-30
			if (new_top>=0) {
			document.getElementById('footer_p').style.top = body_height-footer_offset-30+'px';
			} else
			{
			document.getElementById('footer_p').style.top = '0px';
			}
		}
	}
	//alert(window.scrollTop)
}
function float_tab() {
	if (navigator.userAgent.indexOf("MSIE 6") == -1) { // dont run if IE6
		body_height = document.body.offsetHeight
		footer_offset = findPosition( document.getElementById('corner_tab') )
		alert(body_height)
		alert(footer_offset)
		if (body_height > footer_offset) {
			//alert('move');
			//alert(document.getElementById('footer_p').style.top)
			new_top = body_height-footer_offset-30
			if (new_top>=0) {
			document.getElementById('footer_p').style.top = body_height-footer_offset-30+'px';
			} else
			{
			document.getElementById('footer_p').style.top = '0px';
			}
		}
	}
	//alert(window.scrollTop)
}
function close_window() {
	if (!OpenWindow.closed) {
		OpenWindow.window.close()
		setTimeout("close_window()",250);
	}
}

function download(url) {
	
//check for safari on windows
if (navigator.userAgent.indexOf("Safari")>-1&&navigator.userAgent.indexOf("Windows")>-1) {
  	safari_windows = true
  	w = 600;
  	h = 400;
} else {
	safari_windows = false
  	w = 400;
  	h = 200;
}
  w += 32;
  h += 96;
  wleft = (screen.width - w) / 2;
  wtop = (screen.height - h) / 2;
  // IE5 and other old browsers might allow a window that is
  // partially offscreen or wider than the screen. Fix that.
  // (Newer browsers fix this for us, but let's be thorough.)
  if (wleft < 0) {
    w = screen.width;
    wleft = 0;
  }
  if (wtop < 0) {
    h = screen.height;
    wtop = 0;
  }

OpenWindow=window.open(url,'download','width=' + w + ', height=' + h + ', ' + 'left=' + wleft + ', top=' + wtop + ',status=no,toolbar=no,menubar=yes,location=no,resizable=yes');
//

if (!safari_windows) {
setTimeout("close_window()",2000);
}
//return false;
}

