// 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_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 mv_validate_details() {
	if(document.details_form.first_name.value.length < 1){
		alert('Please enter your first name.');
		document.details_form.first_name.focus();
		return false;
	}
	if(document.details_form.last_name.value.length < 1){
		alert('Please enter your last name.');
		document.details_form.last_name.focus();
		return false;
	}
	if(document.details_form.Job_Title.value.length < 1){
		alert('Please enter your job title.');
		document.details_form.Job_Title.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.details_form.profile.value.length > 500){
		alert('Sorry, your profile must be less than 500 characters.\n\nYou have entered '+document.details_form.profile.value.length+' characters.');
		document.details_form.profile.focus();
		return false;
	}
	if(document.details_form.service_idno.value.length < 1){
		alert('Please select your service.');
		document.details_form.service_idno.focus();
		return false;
	}
	if(document.details_form.password.value.length > 0){
		if(document.details_form.password.value.length < 6){
			alert('Please enter a password at least 6 characters long.');
			document.details_form.password.focus();
			return false;
		}
		if(document.details_form.password.value != document.details_form.password_confirm.value){
			alert('The passwords you entered do not match.');
			document.details_form.password_confirm.select();
			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;
}


function external_links() {
var anchors = document.getElementsByTagName('a');
	for(var i = 0; i < anchors.length; i++) {
		if (anchors[i].target == '_blank') {
			anchors[i].innerHTML = anchors[i].innerHTML+'<img src="/images/new_window.gif" alt="Link opens in new window" />';
		}
	}
}
window.onload = external_links;

function modalDialogShow_IE(url,width,height) //IE
	{
	return window.showModalDialog(url,window,
		"dialogWidth:"+width+"px;dialogHeight:"+height+"px;edge:Raised;center:Yes;help:No;Resizable:Yes;Maximize:Yes");
	}
function modalDialogShow_Moz(url,width,height) //Moz
    {
    var left = screen.availWidth/2 - width/2;
    var top = screen.availHeight/2 - height/2;
    activeModalWin = window.open(url, "", "width="+width+"px,height="+height+",left="+left+",top="+top);
    window.onfocus = function(){if (activeModalWin.closed == false){activeModalWin.focus();};};
    }


function exp_dd(id,action) {
//alert(navigator.appVersion)
	if(navigator.appName.indexOf('Microsoft')!=-1) {
		if(navigator.appVersion.indexOf('MSIE 6')!=-1 && action == 'over') {
			if (document.getElementById(id).style.width!='auto') {
			document.getElementById(id).style.position='absolute';
			document.getElementById(id).style.width='auto';
			document.getElementById(id).focus();
			}
		}
		if(!navigator.appVersion.indexOf('MSIE 6')!=-1 && action == 'focus') {
			document.getElementById(id).style.position='absolute';
			document.getElementById(id).style.width='auto';
		}
	}
}

function bookmark(bookmarkurl,bookmarktitle)
{
	if (document.all)
		window.external.AddFavorite(bookmarkurl,bookmarktitle)
	else if (window.sidebar) // firefox
		window.sidebar.addPanel(bookmarktitle, bookmarkurl, "")
	else
		alert('Please press Ctrl+D to bookmark this page.');
}
function mv_validate_award_details(){
	if(document.volunteer_awards_form.frm_volunteer_who.value.length < 1){
		alert('Please enter who you are nominating?');
		document.volunteer_awards_form.frm_volunteer_who.focus();
		return false;
	}
	if(document.volunteer_awards_form.frm_volunteer_role.value.length < 1){
		alert('Please enter what is their role?');
		document.volunteer_awards_form.frm_volunteer_role.focus();
		return false;
	}
	if(document.volunteer_awards_form.frm_nomination.value.length < 1){
		alert('Please enter why are you nominating them?');
		document.volunteer_awards_form.frm_nomination.focus();
		return false;
	}
	
	if(document.volunteer_awards_form.frm_name.value.length < 1){
		alert('Please enter your name.');
		document.volunteer_awards_form.frm_name.focus();
		return false;
	}
	
	if (emailfilter.test(document.getElementById('frm_email').value)==false) {
		alert("Please enter a valid email address.")
		document.getElementById('frm_email').select()
		return false;
	}
	
}

function mv_validate_card_message(){
	
	
	if(document.messages.message1.value == 'Please click to enter your message here' || document.messages.message1.value.length < 1){
		alert('Please enter a message.');
		document.messages.message1.select();
		return false;
	}
/*	if(document.messages.message2.value == 'Please click to enter your second message here'){
		alert('Please enter a second message.');
		document.messages.message2.select();
		return false;
	}
	if(document.messages.message2.value.length < 1){
		alert('Please enter a second message, at least your name.');
		document.messages.message2.focus();
		return false;
	}*/
	
}

function mv_validate_card_emails(){
	if (document.form2.name_to.length) { // check if there is more than on element with that name
		for (i = 0; i<document.form2.name_to.length; ++ i)
			{
				if (document.form2.name_to[i].value.length < 1) {
					alert('Please enter a name.');
					document.form2.name_to[i].focus();
					return false;
				}
				if (emailfilter.test(document.form2.email_to[i].value)==false) {
					alert("Please enter a valid email address.")
					document.form2.email_to[i].select()
					return false;
				}
			}
	} else {
		if (document.form2.name_to.value.length < 1) {
			alert('Please enter a name.');
			document.form2.name_to.focus();
			return false;
		}
		if (emailfilter.test(document.form2.email_to.value)==false) {
			alert("Please enter a valid email address.")
			document.form2.email_to.select()
			return false;
		}
	}
	
	document.getElementById('send_cards').value = 'Sending, please wait...'
	document.getElementById('send_cards').disabled = true;
}

function mv_validate_card_upload(){
		if (document.form3.upload_file.value.length < 1) {
			alert('Please select a spreadsheet to upload.');
			document.form3.upload_file.focus();
			return false;
		}
		if (document.form3.upload_file.value.lastIndexOf(".xls") != document.form3.upload_file.value.length-4) {
			alert('The file must be an Excel spreadsheet (.xls).');
			document.form3.upload_file.focus();
			return false;
		}
	
	document.getElementById('upload_submit').value = 'Uploading file, please wait...'
	document.getElementById('upload_submit').disabled = true;
}


