// JavaScript Document
/*===============================================================================================================
    Disc:        General Javascript/JQuery for EMEA Hillspet sites.
    Date:        7/2011
    Created by:   Thomas Woroniak 
	Dependencies:	Jquery must load first.   
===============================================================================================================*/
var timeStamp = (new Date()).getTime(); //unique timestamp to stop IE from caching ajax requests
var url = window.location.pathname;
var errorCode;
$(document).ready(function(){
	//places doc ready functions here
	
});

/*===============================================================================================================
   ---------------------------- EMEA SSO AND PROFILES FUNCTIONALITY ---------------------------- 
===============================================================================================================*/

//==== Start: Function to fix template layout ====================================//
function initSSOTemplateFix(){ //places header, navigation and footer elements into the #page container
	$('#footer').appendTo('#page'); 
	$('#header').appendTo('#page'); 
	$('#navigation').appendTo('#page'); 
}

//==== Start: Function to setup the Login Page ====================================//
function initLoginPage(){
	$('#reg_form_wrapper').appendTo('#reg_form_container');
	initSSOTemplateFix(); 
	showLoginError();
}

//==== Start: Function to setup the Forgot Password Page ====================================//
function initForgotPasswordPage(){
	$('#reg_form_wrapper').appendTo('#reg_form_container');
	initSSOTemplateFix(); 
		
}

//==== Start: Function to setup the Update Password Page ====================================//
function initUpdatePasswordPage(){
	$('#reg_form_wrapper').appendTo('#reg_form_container');
	initSSOTemplateFix(); 	
}

//==== Start: Function to setup the Reset Password Page ====================================//
function initResetPasswordPage(){
	$('#reg_form_wrapper').appendTo('#reg_form_container');
	initSSOTemplateFix();
	showConfirmPasswordError(); 	
}

//==== Start: Function to setup the Register Page ====================================//
function initRegisterPage(){
	$('#reg_form_wrapper').appendTo('#reg_form_container');
	initSSOTemplateFix();
	initDatepicker();
	initOther();
	//checkEmail();
	switchRegistrationImage();
	//initRegistrationSubmit();
	//initRegistrationValidation(); //do client-side jquery validation on registration form; comment out if using Ajax;
}

//==== Start: Function to setup the Edit Profiles Page ====================================//
function initEditProfilesPage(){
	$('#reg_form_wrapper').appendTo('#reg_form_container');
	initSSOTemplateFix();
	initDatepicker();
}

//==== Start: Function to setup the Profiles Page ====================================//
function initProfilesPage(){
	initSSOTemplateFix();
	$('#profile-container').appendTo('#reg_form_container');
	$('div.pet-container').appendTo('#main-pet-container');
}

//==== Start: Function to setup the Pet Registration Page ====================================//
function initPetRegisterPage(){
	initPetDatepicker();
	initCatDog();
	checkSelectedSpecies();
	//$('#petSterile').hide();
	$( "div.radio-wrapper1" ).buttonset();
	$( "div.radio-wrapper2" ).buttonset();
	$('#reg_form_container').appendTo('#form_container');
}

//==== Start: Function to setup the Pet Update Profile Page ====================================//
function initPetProfileUpdatePage(){
	initPetDatepicker();
	initCatDogUpdate();
	checkCatDogRadio();

	$( "div.radio-wrapper1" ).buttonset();
	$( "div.radio-wrapper2" ).buttonset();
	$('#photo-upload-form-container').appendTo('#photo-upload-right-column-container');
	$('#reg_form_container').appendTo('#form_container');
}

//==== Start: Function to initialize datepicker widget ====================================//
function initDatepicker(){ //datepicker widget is modified for European sites
	$("#selectDate").datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'dd/mm/yy',
		firstDay: 1,
		yearRange: '1900:+nn'
	});
}

//==== Start: Function to initialize pet datepicker widget ====================================//
function initPetDatepicker(){ //pet datepicker widget is modified for Europe sites
	$("#selectDate").datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'mm/yy',
		firstDay: 1,
		yearRange: '1970:+nn'
	});
}

function initTermsFix(){ //hides all header, footer and navigation
	$('#footer').hide();
	$('#header').hide();
	$('#navigation').hide();
	$('#pet-menu').hide();
	$('#container').css('margin-top', '10px');
}

//==== Start: Function to show Login error ====================================//
function showLoginError(){
	var url = $.url(); //get current URL
	var errorCode = url.param('code');
	if (errorCode != ''){
			showError();
		} else {
			$('#error').hide();
		}
		function showError(){
			if (errorCode == '401'){
				//alert(errorCode);
				$('#error').show();
			}else{
				$('#error').hide();
			}
		}
}

//==== Start: Function showConfirmPasswordError ====================================//
function showConfirmPasswordError(){
	var url = $.url(); //get current URL
	var errorCode = url.param('error');

//alert(errorCode);

	if (errorCode == '1'){
		$('#resetError').show();
	} else if (errorCode == '2'){
		$('#confirmError').show();
	} else {
		//alert('blah');
		$('#confirmError').hide();
		$('#resetError').hide();
	}
}

//==== Start: Function to detect Cat or Dog on pet registration form page load ====================================//
function checkSelectedSpecies(){
	var speciesSelected = $("div.petSpecies input[type='radio']:checked").val();
	//alert(speciesSelected);
	if (speciesSelected === 'Cat' ){
		$('#speciesBoth').hide();
		$('#speciesCat').show();
		$('#speciesDog').hide();
		$('#petBreedCat').show();
		$('#petBreedDog').hide();	
		$('#petSterile').show(); //show SterileYESNO if val ==='Cat'
	} else if (speciesSelected === 'Dog' ){
		$('#speciesBoth').hide();
		$('#speciesCat').hide();
		$('#speciesDog').show();
		$('#petBreedDog').show();
		$('#petBreedCat').hide();
		$('#petSterile').hide();
	} else {
		$('#speciesBoth').show();
		$('#speciesCat').hide();
		$('#speciesDog').hide();
		$('#petBreedDog').hide();
		$('#petBreedCat').hide();
		$('#petSterile').hide();
	}	
}


//==== Start: Function to detect Cat or Dog on pet registration form ====================================//
function initCatDog(){
	$("div.petSpecies input[type='radio']").change( function() {
		//alert('initCatDog');
			if ($(this).val() === 'Cat' ){
				//alert('Cat');
				$('#speciesBoth').hide();
				$('#speciesCat').show();
				$('#speciesDog').hide();
				$('#petBreedCat').show();
				$('#petBreedDog').hide();
				$('#petSterile').show(); //show SterileYESNO if val ==='Cat'				
			} else if ($(this).val() === 'Dog' ){
				//alert('Dog');
				$('#speciesBoth').hide();
				$('#speciesCat').hide();
				$('#speciesDog').show();
				$('#petBreedDog').show();
				$('#petBreedCat').hide();
				$('#petSterile').hide();
			} else {
				//alert('Else?');
				$('#speciesBoth').show();
				$('#speciesCat').hide();
				$('#speciesDog').hide();
				$('#petBreedDog').hide();
				$('#petBreedCat').hide();
				$('#petSterile').hide();
			}
	});	
}

//==== Start: Function to detect Cat or Dog on pet registration form ====================================//
function initCatDogUpdate(){
	$("div.petSpecies input[type='radio']").change( function() {
		//alert('initCatDog');
			if ($(this).val() === 'Cat' ){
				//alert('Cat');
				$('#petBreedDog').hide();
				$('#petBreedCat').show();
				$('#petSterile').show(); //show SterileYESNO if val ==='Cat'				
			} else if ($(this).val() === 'Dog' ){
				//alert('Dog');
				$('#petBreedDog').show();
				$('#petBreedCat').hide();
				$('#petSterile').hide();
			} else {
				//alert('Else?');
				$('#petBreedDog').hide();
				$('#petBreedCat').hide();
				$('#petSterile').hide();
			}
	});	
}

function checkCatDogRadio(){
	var url = $.url(); //get current URL
	var species = url.param('species');
	if (species == 'Cat'){
		$('#petBreedDog').hide();
		$('#petBreedCat').show();
	} else if (species == 'Dog'){
		$('#petBreedDog').show();
		$('#petBreedCat').hide();
		$('#petSterile').hide();
	}
}


function getCurrentLocale(){ //get currect locale 
	var url = $.url(); //get current URL
	var locale = url.segment(1); // get locale from parsing URL
	//alert(locale);
	var imagepath = '/images/'+locale+'/reg-promo.jpg';
	switch(locale) {
        //case 'showEMEALogin': showEMEALogin(); break;
		//case 'en-gb': switchRegistrationImage(imagepath); break;
		//case 'de-de': switchRegistrationImage(imagepath); break;
    }
}

function switchRegistrationImage(){ //switch image based on locale
	var url = $.url(); //get current URL
	var locale = url.segment(1); // get locale from parsing URL
	//alert(locale);
	var imagesource = $('#promo_image img').attr('src');
	var imagepath = '../../images/'+locale+'/reg-promo-'+locale+'.jpg';
		if(imagesource == ''){
			$('#promo_image img').attr('src',imagepath);
		}else{	
			//alert('image source is ' +imagesource);	
		};
	
}

//==== Start: Function to disable/enable "Other" input field ====================================//
function initOther(){
	var $select = $('#source'),
		$textarea = $('#other'),
		status;	
	$select.bind('change', function() {	
		$textarea.attr('disabled');
		// If the value of the select box matches "Other"
		// remove attribute "disabled", else set status to 'disabled'
		//alert('change!');
		if ($(this).val() === 'Other' ){
			$textarea.removeAttr('disabled');
		} else {
			$textarea.attr('disabled');
		}
	});
}


