j = jQuery.noConflict();

j('document').ready(function(){
	if(j('#VoteYes').length != 0 && j('#VoteNo').length != 0){
		Magazine.setPollResults();
	}
});


Magazine = {
voteAction: '/magazine/Vote',
pollResultsAction: '/magazine/PollResults',
enterCompetitionAction: '/magazine/EnterCompetition',
sendMessageAction: '/magazine/SendMessage',
addNewsletterRecipientActioin:'/magazine/AddNewsletterRecipient',
submitSurveyAction:'/magazine/SubmitSurvey',
sendCommunityMessageAction:'/magazine/SendMessage',
sendContactMessageAction:'/magazine/SendContactMessage',
sendCareersMessageAction:'/magazine/SendCareersMessage',
magazineSubscriptionAction:'/magazine/MagazineSubscription',
redirectHash: [],
Redirect : function (redirectLocation,redirect){    this.redirectLocation = redirectLocation;  this.redirect = redirect},
checkText : function (objSel,msg){
	if(objSel.value.length == 0 ){
	  alert(msg);
	  objSel.focus();
	  return true;
	}
	return false;
},
checkCheckBox : function (objSel,msg){
	
	if(objSel.checked != true){
	  alert(msg);
	  objSel.focus();
	  return true;
	}
	return false;
},
checkEmail : function(objSel,msg){

var re = /^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

   if(objSel.value == "" || !objSel.value.match(re)){
	  alert(msg);
	  objSel.focus();
	  return true;
	}
	return false;
	

},
checkCheckBox :function(objSel,msg){
	
	if(objSel.checked != true){
	  alert(msg);
	  objSel.focus();
	  return true;
	}
	return false;
},
checkRadio :function (objSel,msg){
	var checked = false;
	for(i=0 ; i < objSel.length; i++){
		if(objSel[i].checked){ checked = true; break;}
	}
	if(checked != true){
	  alert(msg);
	  objSel[0].focus();
	  return true;
	}
	return false;
},
sendContactMessage: function(redirectLocation,redirect) {
	if(Magazine.checkText(document.MagazineContactMesssagefm.FirstName,"Please enter value for first name")) return false;
	if(Magazine.checkText(document.MagazineContactMesssagefm.LastName,"Please enter value for last name")) return false;
	if(Magazine.checkEmail(document.MagazineContactMesssagefm.Email,"Please enter a valid email")) return false;
	if(Magazine.checkText(document.MagazineContactMesssagefm.Message,"Please enter your message")) return false;
	if(Magazine.checkText(document.MagazineContactMesssagefm.recaptcha_response_field,"Please type the two words")) return false;
	
	//Magazine.redirectHash['sendMessageRedirect'] = new Magazine.RedirectObject(redirectLocation,redirect);
	Magazine.redirectHash["send_contact_message"] = new Magazine.Redirect(redirectLocation,redirect);
	//j('body').css('cursor', 'wait');
	j.post(
		Magazine.sendContactMessageAction,
		j('#MagazineContactMesssagefm').serialize() + '&Domain=' + window.location.host,
		function($data){
			if($data.success == "false" && $data.error == -2) {
				j('body').css('cursor', '');
				Recaptcha.reload ();
				alert($data.message+". Please try again.");
				return;
			}
			if(Magazine.redirectHash["send_contact_message"].redirect){
				window.location = Magazine.redirectHash["send_contact_message"].redirectLocation;
			}
		},
		'json'
	);
	return true;
},
sendCareersMessage: function(redirectLocation,redirect) {
	if(Magazine.checkText(document.MagazineCareersMesssagefm.FirstName,"Please enter value for first name")) return false;
	if(Magazine.checkEmail(document.MagazineCareersMesssagefm.Email,"Please enter a valid email")) return false;
	if(Magazine.checkText(document.MagazineCareersMesssagefm.Telephone,"Please enter a valid telephone")) return false;
	if(Magazine.checkText(document.MagazineCareersMesssagefm.TimeSlot,"Please enter a valid time slot")) return false;
	if(Magazine.checkText(document.MagazineCareersMesssagefm.POS,"Please enter a valid position of interest")) return false;
	
	if(Magazine.checkText(document.MagazineCareersMesssagefm.Message,"Please enter your message")) return false;
	//if(Magazine.checkText(document.MagazineCareersMesssagefm.recaptcha_response_field,"Please type the two words")) return false;
	
	//Magazine.redirectHash['sendMessageRedirect'] = new Magazine.RedirectObject(redirectLocation,redirect);
	Magazine.redirectHash["send_careers_message"] = new Magazine.Redirect(redirectLocation,redirect);
	//j('body').css('cursor', 'wait');
	j.post(
		Magazine.sendCareersMessageAction,
		j('#MagazineCareersMesssagefm').serialize() + '&Domain=' + window.location.host,
		function($data){
			if($data.success == "false" && $data.error == -2) {
				j('body').css('cursor', '');
				//Recaptcha.reload ();
				alert($data.message+". Please try again.");
				return;
			}
			if(Magazine.redirectHash["send_careers_message"].redirect){
				window.location = Magazine.redirectHash["send_careers_message"].redirectLocation;
			}
		},
		'json'
	);
	return true;
},
sendCommunityMessage: function(redirectLocation,redirect){
	if(Magazine.checkText(document.CommunityMesssagefm.FirstName,"Please enter your name")) return false;
	if(Magazine.checkEmail(document.CommunityMesssagefm.Email,"Please enter a valid email")) return false;
	if(Magazine.checkText(document.CommunityMesssagefm.Message,"Please enter your message")) return false;
	
	//Magazine.redirectHash['sendMessageRedirect'] = new Magazine.RedirectObject(redirectLocation,redirect);
	j('body').css('cursor', 'wait');
	Magazine.redirectHash["send_community_message"] = new Magazine.Redirect(redirectLocation,redirect);
	j.post(
		Magazine.sendCommunityMessageAction,
		j('#CommunityMesssagefm').serialize() + '&Domain=' + window.location.host,
		function($data){
			if(Magazine.redirectHash["send_community_message"].redirect){
				window.location = Magazine.redirectHash["send_community_message"].redirectLocation;
			}
		},
		'json'
	);
	return true;
},
addNewsletterRecipient: function(redirectLocation,redirect){
	if(Magazine.checkEmail(document.AddNewsletterRecipientfm.Email,"Please enter a valid email")) return false;
	j('body').css('cursor', 'wait');
	Magazine.redirectHash["add_newsletter_recipient"] = new Magazine.Redirect(redirectLocation,redirect);
	j.post(
		Magazine.addNewsletterRecipientActioin,
		j('#AddNewsletterRecipientfm').serialize() + '&Domain=' + window.location.host,
		function($data){
			if(Magazine.redirectHash["add_newsletter_recipient"].redirect){
				window.location = Magazine.redirectHash["add_newsletter_recipient"].redirectLocation;
			}
		},
		'json'
	);
	return true;
},
submitSurvey: function(redirectLocation,redirect){

	
	if(Magazine.checkRadio(document.Survayfm.Gender,"Please select a value for gender")) return false;
	if(Magazine.checkRadio(document.Survayfm.AgeGroup,"Please select a value for age group")) return false;
	if(Magazine.checkRadio(document.Survayfm.PublishMagazine,"Please publish magazine")) return false;
	if(Magazine.checkText(document.Survayfm.BestPart,"Please enter value for best part")) return false;
	if(document.Survayfm.NotLikePart.value == '') if(Magazine.checkCheckBox(document.Survayfm.NotLikeCheck,"Please enter value for not liked part or check the check box to confirm")) return false;
	if(document.Survayfm.IncludeExtra.value == '') if(Magazine.checkCheckBox(document.Survayfm.IncludeExtraCheck,"Please enter value for extra include or check the check box to confirm")) return false;
		
	if(Magazine.checkRadio(document.Survayfm.Informative,"Please select a value for informativeness")) return false;
	if(Magazine.checkRadio(document.Survayfm.FutureProduct,"Please select one or more future products")) return false;
	if(Magazine.checkRadio(document.Survayfm.LifeInsurance,"Please select a value for life insurance")) return false;
	if(Magazine.checkRadio(document.Survayfm.PetInsurance,"Please select a value for pet insurance")) return false;
    if(Magazine.checkEmail(document.Survayfm.Email,"Please enter value for email")) return false;

    j('body').css('cursor', 'wait');
	
	Magazine.redirectHash["submit_survey"] = new Magazine.Redirect(redirectLocation,redirect);
	
	j.post(
		Magazine.submitSurveyAction,
		j('#Survayfm').serialize() + '&Domain=' + window.location.host,
		function($data){
			if(Magazine.redirectHash["submit_survey"].redirect){
				window.location = Magazine.redirectHash["submit_survey"].redirectLocation;
			}
		},
		'json'
	);
	return true;
	
},

sendMessage: function(redirectLocation,redirect) {
	if(Magazine.checkText(document.MagazineMesssagefm.FirstName,"Please enter value for first name")) return false;
	if(Magazine.checkText(document.MagazineMesssagefm.LastName,"Please enter value for last name")) return false;
	if(Magazine.checkEmail(document.MagazineMesssagefm.Email,"Please enter a valid email")) return false;
	if(Magazine.checkText(document.MagazineMesssagefm.Telephone,"Please enter value for telephone")) return false;
	if(Magazine.checkText(document.MagazineMesssagefm.Message,"Please enter your message")) return false;
	
	//Magazine.redirectHash['sendMessageRedirect'] = new Magazine.RedirectObject(redirectLocation,redirect);
	Magazine.redirectHash["send_message"] = new Magazine.Redirect(redirectLocation,redirect);
	j('body').css('cursor', 'wait');
	j.post(
		Magazine.sendMessageAction,
		j('#MagazineMesssagefm').serialize() + '&Domain=' + window.location.host,
		function($data){
			if(Magazine.redirectHash["send_message"].redirect){
				window.location = Magazine.redirectHash["send_message"].redirectLocation;
			}
		},
		'json'
	);
	return true;
},
RedirectObject : function(redirectLocation,redirect){
	this.redirectLocation = redirectLocation;
	this.redirect = redirect;
},
enterCompetition: function(redirectLocation,redirect) {
	if(Magazine.checkText(document.MagazineCompetitionfm.FirstName,"Please enter value for first name")) return false;
	if(Magazine.checkText(document.MagazineCompetitionfm.LastName,"Please enter value for last name")) return false;
	if(Magazine.checkEmail(document.MagazineCompetitionfm.Email,"Please enter a valid email")) return false;
	if(Magazine.checkText(document.MagazineCompetitionfm.Telephone,"Please enter value for telephone")) return false;
	if(Magazine.checkText(document.MagazineCompetitionfm.Moniker,"Please enter value for moniker")) return false;
	if(Magazine.checkText(document.MagazineCompetitionfm.WhyChoose,"Please enter value for why choose moniker")) return false;
	if(Magazine.checkCheckBox(document.MagazineCompetitionfm.Conditions,"Please check the accept coditions box")) return false;
	

	//var val = new Magazine.RedirectObject(redirectLocation,redirect);
	Magazine.redirectHash["enter_Competition"] = new Magazine.Redirect(redirectLocation,redirect);
	//alert(val.redirectLocation);
	j('body').css('cursor', 'wait');
	j.post(
		Magazine.enterCompetitionAction,
		j('#MagazineCompetitionfm').serialize() + '&Domain=' + window.location.host,
		function($data){
			if(Magazine.redirectHash["enter_Competition"].redirect){
				window.location = Magazine.redirectHash["enter_Competition"].redirectLocation;
			}
		},
		'json'
	);
	return true;
},
setPollResults: function (){
	j.post(
		Magazine.pollResultsAction,
		'',
		function($data){

			j('#YesVoteCount').html($data.type1Votes)
			j('#NoVoteCount').html($data.type2Votes);
			var t1 = j('#VoteYes').width()/100 * $data.type1Percentage;
			var t2 = j('#VoteNo').width()/100 * $data.type2Percentage;
			j('#VoteYes').css('width',t1);
			j('#VoteNo').css('width',t2);
			j('#YesVotePercentage').html($data.type1Percentage)
			j('#NoVotePercentage').html($data.type2Percentage);
		},
		'json'
	);
	
},
magazineSubscribe: function(redirectLocation,redirect) {
	if(Magazine.checkText(document.MagazineMesssagefm.Title,"Please enter value for title")) return false;
	if(Magazine.checkText(document.MagazineMesssagefm.FirstName,"Please enter value for first name")) return false;
	if(Magazine.checkText(document.MagazineMesssagefm.Surname,"Please enter value for last name")) return false;
	if(Magazine.checkText(document.MagazineMesssagefm.HomeTelephone,"Please enter a home telephone")) return false;
	if(Magazine.checkText(document.MagazineMesssagefm.Mobile,"Please enter value for mobile")) return false;
	if(Magazine.checkEmail(document.MagazineMesssagefm.Email,"Please enter your email")) return false;
	if(Magazine.checkEmail(document.MagazineMesssagefm.EmailConfirm,"Please enter value for confirm email")) return false;
	if(Magazine.checkText(document.MagazineMesssagefm.PostCode,"Please enter a valid post code")) return false;
	if(Magazine.checkText(document.MagazineMesssagefm.Address1,"Please enter value for address line 1")) return false;
	if(Magazine.checkText(document.MagazineMesssagefm.Town,"Please enter your town")) return false;
	if(Magazine.checkText(document.MagazineMesssagefm.County,"Please enter value for county")) return false;
	if(Magazine.checkText(document.MagazineMesssagefm.Country,"Please enter your country")) return false;
	
	//Magazine.redirectHash['sendMessageRedirect'] = new Magazine.RedirectObject(redirectLocation,redirect);
	Magazine.redirectHash["magazine_subscribe"] = new Magazine.Redirect(redirectLocation,redirect);
	j.post(
		Magazine.magazineSubscriptionAction,
		j('#MagazineMesssagefm').serialize() + '&Domain=' + window.location.host,
		function($data){
			if(Magazine.redirectHash["magazine_subscribe"].redirect){
				window.location = Magazine.redirectHash["magazine_subscribe"].redirectLocation;
			}
		},
		'json'
	);
	return true;
},
addVote: function (redirectLocation,redirect){
	/*
	var checked = false;
	var value = '';
	for(var i = 0; i < form.vote.length; i++) {
		if(form.vote[i].checked) {
			checked =true;
			value = form.vote[i].value
			break;
		}
	}
	if (!checked){
		alert(msg);
		return false;
	}*/
	var value = j("input[name='voteType']:checked").val(); 
	if (value == null){
		alert('Please select Yes or No');
		return false;		
	}
	j('body').css('cursor', 'wait');
	
	Magazine.redirectHash["add_vote"] = new Magazine.Redirect(redirectLocation,redirect);

	var data = "voteType="+value;
	data += "&domain="+window.location.host;
	data += "&companyId=0"
	data += "&groupId=0"
	data += "&languageId=0";
	data += "&userId=0"

	j.post(
		Magazine.voteAction,
		data,
		function($data){
			//ignore erros
			if(Magazine.redirectHash["add_vote"].redirect){
				window.location = Magazine.redirectHash["add_vote"].redirectLocation;//Magazine.voteResultsPath;
			}
		},
		'json'
	);
}
};


//var tt = new Magazine.Magazine("test",true);

//alert(tt.redirectLocation);
