function clearField(fld) {
	if(fld.value == 'Your email address') {
		fld.value="";
	}
	return true;
}

function checkForm(f) {
	if(f.elements['name'].value == null || f.elements['name'].value == '') {
		alert("Please enter your name!");
		return false;
	} else if (f.elements['email'].value == null || f.elements['email'].value == '') {
		alert("Please enter your email address!");
		return false;
	} else if (f.elements['message'].value == null || f.elements['message'].value == '') {
		alert("Please type the message!");
		return false;		
	} else {
		if( checkEmail(f.elements['email']) != false) {
			if(jcap() == true) {
				f.submit();
				return true;
			}
		}
	}
	return false;
}

function isEmailAddress (string) {
  var addressPattern =
    /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
  return addressPattern.test(string);
}

function checkEmail (field) {
  if (!isEmailAddress(field.value)) {
    alert('Please enter correct email address!');
    field.focus();
    field.select();
 	return (false);
  }
}

function doSignUp(f) {
	var email = f.elements['email'];
	if(email.value == "" || email.value== null || email.value == 'undefined') {
		return false;
	}	
	else if(checkEmail(email) == false) {
		return false;
	}else{
		f.submit();
		return true;
	}
}

function getArgs() {
	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split("&");
	for(var i = 0; i < pairs.length; i++) {
		var pos = pairs[i].indexOf('=');
		if (pos == -1) continue;
		var argname = pairs[i].substring(0,pos);
		var value = pairs[i].substring(pos+1);
		args[argname] = unescape(value);
	}
	return args;
}

function showThankYou() {
	var args = getArgs();
	var a = args.showThankYou;

	if(args.showThankYou == 'Y') {	
		if (args.type == "contact") {
			document.getElementById('showThankYou').innerHTML = "Thank you for your interest! <br />Your message has been successfully sent!";
		}
	}
	if(args.resp =="Y"){
		if(args.type == "confirmRequired") {
			document.getElementById('showThankYou2').innerHTML = "You have successfully subscribed to Mailing List. You will receive an email with confirmation link shortly.";
		}
		else if(args.type == "emailNotFound") {
			document.getElementById('showThankYou2').innerHTML = "Your email address is not found.";
		}
		else if(args.type == "confirmSuccess") {
			document.getElementById('showThankYou2').innerHTML = "Thank you! Your subscribtion to Mailing List has been confirmed!";
		}
		else if(args.type == "alreadyConfirmed") {
			document.getElementById('showThankYou2').innerHTML = "You have already confirmed your subscription.";
		}
		else if(args.type == "alreadySubscribed") {
			document.getElementById('showThankYou2').innerHTML = "You have already subscribed to our Mailing list.";
		} else {
			document.getElementById('showThankYou2').innerHTML = "";
		}
	}
}

function popUp(page,PWidth,PHeight,id) {
	eval("source"+id+"=window.open('"+page+"','fineline0','toolbar=0,scrollbars=no,location=0,status=0,menubars=0,resizable=yes,width="+PWidth+",height="+PHeight+"')")
	eval("source"+id+".window.moveTo((screen.width/2)-(PWidth/2),(screen.height/2)-(PHeight/2))")
}

function displayImageInPopupDiv(imgId,imgSrc,width, height,caption) {
	
	var html = '<img src="'+imgSrc+'" border="0" />';
	html += '<div style="padding-top:3px;text-align:center;font-size:8pt;font-family:verdana"> '+caption+' </div>';
	html += '<div style="padding:5px; background:#FFFFFF;font-weight:bold;text-align:right;">'+
	'<span style="cursor:pointer;background:red;color:white" onClick="closePopupDiv('+imgId+')">&nbsp;close&nbsp;</span></div>';
	var div = drawCenteredPopup(imgId,width,height+55,html);
	div.style.border = 2+'px solid #9FA8AE';
	div.style.background = 'white';
	div.style.padding = '0.9em';
	
}

function closePopupDiv(id){
	var elm = document.getElementById(id);
	elm.style.display = 'none';
	elm.style.visibility = 'hidden';
}

function drawCenteredPopup(id,width,height,html) {  
	if (!document.getElementById(id)) {
    	var newNode = document.createElement("div");
    	newNode.setAttribute("id", id);
    	newNode.setAttribute("class", "Div");
    	newNode.setAttribute("style", "visibility: hidden");
    	newNode.setAttribute("style", "display: none");
    	document.body.appendChild(newNode);
	}
	
  	var popupDiv = document.getElementById(id);

	popupWidth = (width =='auto') ? popupDiv.offsetWidth  : width ;
	popupHeight = (height == 'auto') ? popupDiv.offsetHeight : height;
	var innerW = window.innerWidth;
	var innerH = window.innerHeight;
	
	if (document.all) {
		innerW = document.body.clientWidth;
		innerH = document.body.clientHeight;
	}

  	popupDiv.style.position = "absolute";
  	popupDiv.style.width = popupWidth;
  	popupDiv.style.height = popupHeight+'px';
  	var x = (innerW / 2) - (popupWidth / 2);
  	var y = (innerH / 2) - (popupHeight / 2); 
	  	           
  	popupDiv.style.left = x + "px";
  	popupDiv.style.top = y + "px";
  	popupDiv.style.visibility = (popupDiv.style.visibility == "visible" ? "hidden" : "visible"); //hid /vis
  	popupDiv.style.display = (popupDiv.style.display == "block" ? "none" : "block"); //none /block
  	//popupDiv.style.border = 1+'px solid black';
   	//popupDiv.style.opacity=  '.9'; //mozilla
	//popupDiv.style.filter = 'alpha(opacity=90)'; //IE
  	popupDiv.style.zIndex = 1000;
  	popupDiv.innerHTML = html;
	
 	return popupDiv;
}

function showHideQuizAnswers(){
	var link = document.getElementById("quizAnswersLink");
	var answers = document.getElementById("quizAnswers");
	if(answers.style.visibility == 'hidden'){
		answers.style.visibility = 'visible';
		link.innerHTML = '<span"><span class="pointer likeALink">Click to close answers</span></span>';
	}else {
		answers.style.visibility = 'hidden';
		link.innerHTML = 'Click <span class="pointer likeALink">here</span> for answers';
	}
}

function showSailorHumor(){
	var link = document.getElementById("sailorHumorLink");
	var answers = document.getElementById("sailorHumor");
	if(answers.style.display == 'none'){
		answers.style.display = 'block';
		//link.innerHTML = 'Sailor Humor (close)';
	}else {
		answers.style.display = 'none';
		//link.innerHTML = 'Sailor Humor';
	}
}
//onclick="showReadersGuideHints(this,\'q2Hints\'
function showReadersGuideHints(linkId,elmId){
	var link = document.getElementById(linkId);
	var answers = document.getElementById(elmId);
	if(answers.style.display == 'none'){
		answers.style.display = 'block';
		link.innerHTML = '<span"><span class="pointer likeALink">Click to close answers</span></span>';
	}else {
		answers.style.display = 'none';
		link.innerHTML = 'Click <span class="pointer likeALink">here</span> for answers';
	}
}

function submitContactForm(f){	
	//busy(true);
	/*
	$j.post(  
			".php",  
			$j("#submitMessage").serialize(),  
			function(data){
				//busy(false);
				if(data.result == 'success'){
					alert("Message has been successfully sent");
				}else{
					alert("There was an error sending your message: \n" + data.result);
				}
			},"json"  
		);
	*/
	f.submit();
	return true;
}

$j(function(){
	$j('#submitBtn').button();
	$j('#resetBtn').button();
	$j('#emailSignupBtn').button();
});

$j(function() {
	$j("#proposedDate").datepicker();
});

$j(function() {
	$j("#submitMessage").validate({
		  rules: {
		    email: {
				required:true,
				email:true
		  	},
		  	name:"required",
		  	address :"required",
		  	city : "required",
		  	state : "required",
		  	zipCode : "required",
		  	country : "required",
		  	phone : "required",
		  	numberOfPeopleInGroup : "required",
		  	proposedDate : "required",
		  	captcha :{
		  		required:true,
		  		minlength: 3
		  	}
		},
		  messages:{
			email : " Invalid email.",
			captcha : {
				minlength : "Enter 3 black symbols"
			}
		  },
		  submitHandler: function(form) {
			  submitContactForm(form);
			  return true;
		  }
	});
	return false;
});

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
};


function clearInitFieldValue(elm,value,isOnFocus){
	var val = elm.value.trim();
	if(isOnFocus){
		if(val == value){
			elm.value = '';
		}
	}else{
		if(val == ''){
			elm.value = value;
		}
	}
}

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 } 
}


function checkNewsletterSignUpForm(f){
	var name = f.elements['fullName'];
	var email = f.elements['email'];
	var zipCode = f.elements['zipCode'];
	if(email.value.trim() == '' || email.value.trim() == 'Enter your email address'){
		alert('Please provide a valid email address!');
		return false;
	}
	if(!isEmailAddress(email.value.trim())){
		alert('Please provide a valid email address!');
		return false;
	}	
	if(name.value.trim() == '' || name.value.trim() == 'First and last name') {
		alert('Please type in your First and last name!');
		return false;
	}
	if(zipCode.value.trim() == '' || zipCode.value.trim() == 'Zip code') {
		alert('Please type in your zip code!');
		return false;
	}
	f.submit();
	return true;
}
