﻿// JavaScript Document
// @charset "utf-8";

 function closedialog(){
		$('#darkenBackground').hide();
		$('#dialog').hide();
	}
	
	function changeStatus(id){
		var cur = $("#ignore"+id).val();
		if(cur == "true"){
			$("#ignore"+id).val('false');
			$("#igbutton"+id).val('✔');
		}
		else{
			$("#ignore"+id).val('true');
			$("#igbutton"+id).val('✘');	
		}
	}
	
	function addList(){
		var amount = $('#overall').val();
		var html = "";
		for(i=0;i<=amount;i++){
			ignore = $('#ignore'+i).val();
			if(ignore == "false"){
				type = $('#type'+i).val();	
				group = $('#group'+i).val();
				genus = $('#genus'+i).val();
				species = $('#species'+i).val();
				common = $('#common'+i).val();
				quant = $('#quant'+i).val();
				price = $('#price'+i).val();
				size = $('#size'+i).val();
				
				$.post('../admin/addlistsql.php', {type:type, group:group, genus:genus, species:species, common:common, quant:quant, price:price, size:size}, function(data){html += data});
			}
		}
		$('#puthere').html(html);
	}
	
	function newType(id){
		var type = $('#type'+id).val();
		$("#group"+id).removeOption(/./);
		if(type == "fish"){
			var myOptions = {
				"Freshwater" : "Freshwater",
				"Marine" : "Marine"
			}
			$("#group"+id).addOption(myOptions, false);
		}
		else if(type == "reptile"){
			var myOptions = {
				"Snake" : "Snake",
				"Lizard" : "Lizard",
				"Turtle" : "Turtle"
			}
			$("#group"+id).addOption(myOptions, false);
		}
		else if(type == "other"){
			var myOptions = {
				"Amphibian" : "Amphibian",
				"Spider" : "Spider",
				"Mammal" : "Mammal",
				"Invertibrate" : "Invertibrate"
			}
			$("#group"+id).addOption(myOptions, false);
		}
	}
	
	function setAll(type){
		var overall = $('#overall').val();
		for(i=0;i<=overall;i++){
			if(type=="Snake" || type=="Lizard" || type=="Turtle"){
				$('#type'+i).selectOptions("reptile");
				newType(i);
				$('#group'+i).selectOptions(type);
			}
			else if(type=="Marine" || type=="Freshwater"){
				$('#type'+i).selectOptions("fish");
				newType(i);
				$('#group'+i).selectOptions(type);
			}
			else if(type=="Invertibrate" || type=="Mammal" || type=="Spider" || type=="Amphibian"){
				
				$('#type'+i).selectOptions("other");
				newType(i);
				$('#group'+i).selectOptions(type);
			}
		}
	}
	
	function loadpage(page){
		$('#content').html('<div class="ajaxloader"><img src="ajax-loader.gif" /></div>');
		
		
		if(page.indexOf("animal") != -1){
			var animal = "";
			animal = page;
			var stocks;
			var stockid;
			for(i=0;i<animal.length;i++){
				var num = new Number(animal.charAt(i));
				if(!isNaN(num)){
					stocks = i;
					break;
				}
			}
			stockid = animal.substr(stocks);
		}
		if(page.indexOf("news") != -1){
			
			var news = "";
			news = page;
			var newss;
			var newsid;
			for(i=0;i<news.length;i++){
				var num = new Number(news.charAt(i));
				if(!isNaN(num)){
					newss = i;
					break;
				}
			}
			newsid = news.substr(newss);
		}
		switch(page){
			case "home":
				$('#content').load('../mobile/pages/home.php');
				break;
			case "fish":
				$('#content').load('../mobile/pages/getanimal.php?animal=fish');
				break;
			case "lizard":
				$('#content').load('../mobile/pages/getanimal.php?animal=lizard');
				break;
			case "snake":
				$('#content').load('../mobile/pages/getanimal.php?animal=snake');
				break;
			case "turtle":
				$('#content').load('../mobile/pages/getanimal.php?animal=turtle');
				break;
			case "info":
				$('#content').load('../mobile/pages/info.php');
				break;
			case "marine":
				$('#content').load('../mobile/pages/getanimal.php?animal=marine');
				break;
			case "freshwater":
				$('#content').load('../mobile/pages/getanimal.php?animal=freshwater');
				break;
			case animal:
				$('#content').load('../mobile/pages/animalinfo.php?animal='+stockid);
				break;
			case news:
				$('#content').load('../mobile/pages/news.php?news='+newsid);
				break;
			default:
				$('#content').load('../mobile/pages/home.php');
				break;
		}
	}
	
	function loadanimal(animal){
		$('#content').load('../mobile/pages/animalinfo.php?animal='+animal);	
	}
	function openenq(){
		$('#mail').show();
	}
	function sendenqmail(){
		var name = $('#name').val();
		var email = $('#email').val();
		var re = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		var emailcheck = re.test(email);
		var emailbody = $('#emailbody').val();
		var subject = $('#subject').val();
		$('#emailmessage').fadeOut();
		if(email.length < 1 || emailcheck == false){
			$('#emailmessage').fadeOut(function(){
			$('#emailmessage').html("Please enter a valid email address.<br /><br />");
			$('#emailmessage').fadeIn();
			});
		}
		else if(name.length < 1){
			$('#emailmessage').fadeOut(function(){
			$('#emailmessage').html("Please enter a name.<br /><br />");
			$('#emailmessage').fadeIn();
			});
		}
		else if(subject.length < 1){
			$('#emailmessage').fadeOut(function(){
			$('#emailmessage').html("Please enter a subject.<br /><br />");
			$('#emailmessage').fadeIn();
			});
		}
		else if(emailbody.length < 1){
			$('#emailmessage').fadeOut(function(){
			$('#emailmessage').html("Please enter a message.<br /><br />");
			$('#emailmessage').fadeIn();
			});
		}
		else{
			$('#emailsend').hide();
			$('#loadingbutton').show();
			$.post('../contact/sendmail.php',
			{name:name, email:email, emailbody:emailbody, subject:subject},
			function(data){
				switch(data){ 
					case "sent":
						$('#emailform').fadeOut(function(){
							$('#emailform').html("<center><h1>Email Sent - We will try to reply as soon as possible.</h1></center>");	
							$('#emailform').fadeIn();
						});
						break;
					case "bademail":
							$('#loadingbutton').hide();
							$('#emailsend').show();
							$('#emailmessage').html("Your email address was not valid, please check for errors. If you continue to get this message please email us manually.<br /><br />");$('#emailmessage').fadeIn();
						break;
					case "badmessage":
							$('#loadingbutton').hide();
							$('#emailsend').show();
							$('#emailmessage').html("Your email was not valid, please check for errors. If you continue to get this message please email us manually.<br /><br />");$('#emailmessage').fadeIn();
						break;
					default:
							$('#loadingbutton').hide();
							$('#emailsend').show();
							$('#emailmessage').html("There was an issue with your email, please try again. If the problem persists please email us manually.<br /><br />");$('#emailmessage').fadeIn();
				}
			});
		}
	}	
	function sendanimalenqmail(subject){
		var name = $('#enqname').val();
		var email = $('#enqemail').val();
		var emailbody = $('#enqanimal').val();
		$('#emailmessage').fadeOut();
		if(email.length < 1){
			$('#emailmessage').fadeOut(function(){
			$('#emailmessage').html("Please enter an email address.<br /><br />");
			$('#emailmessage').fadeIn();
			});
		}
		else if(name.length < 1){
			$('#emailmessage').fadeOut(function(){
			$('#emailmessage').html("Please enter a name.<br /><br />");
			$('#emailmessage').fadeIn();
			});
		}
		else if(emailbody.length < 1){
			$('#emailmessage').fadeOut(function(){
			$('#emailmessage').html("Please enter a message.<br /><br />");
			$('#emailmessage').fadeIn();
			});
		}
		else{
			$('#enqsend').hide();
			$('#loadingbutton').show();
			$.post('../contact/sendmail.php',
			{name:name, email:email, emailbody:emailbody, subject:subject},
			function(data){
				switch(data){ 
					case "sent":
							$('#animalenquiry').fadeOut(function(){
							$('#animalenquiry').html("<center><h1>Email Sent - We will try to reply as soon as possible.</h1></center>");	
							$('#animalenquiry').fadeIn();
						});
						break;
					case "bademail":
							$('#enqsend').show();
							$('#emailmessage').html("Your email address was not valid, please check for errors. If you continue to get this message please email us manually.<br /><br />");$('#emailmessage').fadeIn();
						break;
					case "badmessage":
							$('#enqsend').show();
							$('#emailmessage').html("Your email was not valid, please check for errors. If you continue to get this message please email us manually.<br /><br />");$('#emailmessage').fadeIn();
						break;
					default:
							$('#enqsend').show();
							$('#emailmessage').html("There was an issue with your email, please try again. If the problem persists please email us manually.<br /><br />");$('#emailmessage').fadeIn();
				}
			});
		}
	}
	function loadhash(group){
		loadpage(group);
	}
	
	function showMoreBundles(){
		$('#bundlewrap').animate({'height': '100%'});
	}
	function replaceTopBundle(){
		var top = $('#topvalue').val();
		var bunda = $('#bunda').val();
		$('#bundle'+top).fadeOut(function(){	
			$('#bundlewrap').append($('#bundle'+top));
			$('#bundle'+top).fadeIn();
			bunda++;
			$('#bundle'+top).attr({'id': 'bundle'+bunda});
			top++;
			setTimeout('replaceTopBundle()', 5000);
			$('#topvalue').val(top);
			$('#bunda').val(bunda);
										  });
		
	}
	
	function animalheaders(){
		var headers = new Array();
		root = "http://www.thereptilarium.com";
		headers[0] = root+"/images/animalheader/beardie1.png";
		headers[1] = root+"/images/animalheader/boa1.png";
		headers[2] = root+"/images/animalheader/basilisk1.png";
		headers[3] = root+"/images/animalheader/brownsnake1.png";
		headers[4] = root+"/images/animalheader/sailfin1.png";
		headers[5] = root+"/images/animalheader/uro1.png";
		headers[6] = root+"/images/animalheader/tegu1.png";
		headers[7] = root+"/images/animalheader/cham1.png";
		headers[8] = root+"/images/animalheader/gecko1.png";
		headers[9] = root+"/images/animalheader/cooter1.png";
		headers[10] = root+"/images/animalheader/rainbow1.png";
		headers[11] = root+"/images/animalheader/python1.png";
		headers[12] = root+"/images/animalheader/beardie2.png";
		headers[13] = root+"/images/animalheader/aulo.png";
		headers[14] = root+"/images/animalheader/dragonb1.png";
		headers[15] = root+"/images/animalheader/waterdragon.png";	
		headers[16] = root+"/images/animalheader/frilled1.png";
		
		
		var rand = Math.floor(Math.random()*headers.length);
		var headerback = headers[rand];
		$('#animalheader').css('backgroundImage', "url('"+headerback+"')");
	}
	
	function addItemOrder(stockid, quantity){
		$.post('../order/ordercontroller.php',
			{orderfunc:'additem', stockid: stockid, quantity:quantity}, 
			function(data){
				$('#topinfo').html('<center>Item added to basket.&nbsp;&nbsp;&nbsp;<a href="#" onclick="background();" >Continue browsing</a> or <a href="../order/shoppingcart.php">Go to your basket</a></center>');
				background();
			}
		);	
	}
	function updateItemOrder(stockid, quantity){
		$.post('../order/ordercontroller.php',
			{orderfunc:'additem', stockid: stockid, quantity:quantity}, 
			function(data){
				location.reload(true);
			}
		);	
	}
	function cancelOrder(){
		$.post('../order/ordercontroller.php', {orderfunc: 'cancelorder'},
			function(){
				location.reload(true);	
			}
		);
	}
	function background(){
		$('#topinfo').animate({height: 'toggle', opacity: 'toggle'});
	}
	
	function submitOrder(){
		$('.cleftside input').css({borderColor: '#CCC', borderStyle: 'solid', borderWidth: '1px'});
		var cont = true;
		var name = $('#name').val();
		var address = $('#address').val();
		var town = $('#town').val();
		var county = $('#county').val();
		var country = $('#country').val();
		var postcode = $('#postcode').val();
		var phone = $('#phone').val();
		var email = $('#email').val();
		var notes = $('#notes').val();
		var re = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		var emailcheck = re.test(email);
		var terms = $('#terms').attr("checked");
		var animaltable = $('#animaltable').html();
		if(name.length < 1){ $('#name').css({borderColor: 'red', borderStyle: 'solid', borderWidth: '2px'}); cont = false; }
		if(address.length < 1){ $('#address').css({borderColor: 'red', borderStyle: 'solid', borderWidth: '2px'}); cont = false;}
		if(town.length < 1){ $('#town').css({borderColor: 'red', borderStyle: 'solid', borderWidth: '2px'}); cont = false;}
		if(county.length < 1){ $('#county').css({borderColor: 'red', borderStyle: 'solid', borderWidth: '2px'}); cont = false;}
		if(postcode.length < 1){ $('#postcode').css({borderColor: 'red', borderStyle: 'solid', borderWidth: '2px'}); cont = false;}
		if(phone.length < 1){ $('#phone').css({borderColor: 'red', borderStyle: 'solid', borderWidth: '2px'}); cont = false;}
		if(email.length < 1 || emailcheck == false){ $('#email').css({borderColor: 'red', borderStyle: 'solid', borderWidth: '2px'}); cont = false;}
		if(terms == false){ cont = false; $('#terms').css({borderColor: 'red', borderStyle: 'solid', borderWidth: '2px'}); }
		
		if(cont == true){
			$('#os').html('<img src="../images/layout/ajax-loader.gif" />');
			$.post('../order/ordercontroller.php', {orderfunc: 'ordercomplete', name: name, address:address, town:town, county: county, postcode: postcode, country: country, phone:phone, email:email, notes:notes},
			function(data){
				data = jQuery.trim(data);
					switch(data){
						case "1":
						$('.cleftside input').attr("disabled", "disabled");
						$('#topinfo').html('<center>Order Complete.<br /> Your details have been added to our system and we will contact you (usually within 24 hours) to arrange payment and delivery.<br />Thank you.</center>');
						background();
						$('#os').html('Order complete');
						break;
						case "0":
						$('#os').html('<a href="#" onclick="submitOrder()" class="submitorder">Submit Order</a>');
						break;
						default:	
					}
			});	
		}
			
	}
	function submitBundleOrder(bundle){
		$('.cleftside input').css({borderColor: '#CCC', borderStyle: 'solid', borderWidth: '1px'});
		var cont = true;
		var bundleid = bundle;
		var name = $('#name').val();
		var address = $('#address').val();
		var town = $('#town').val();
		var county = $('#county').val();
		var country = $('#country').val();
		var postcode = $('#postcode').val();
		var phone = $('#phone').val();
		var email = $('#email').val();
		var re = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		var emailcheck = re.test(email);
		var terms = $('#terms').attr("checked");
		var animaltable = $('#animaltable').html();
		if(name.length < 1){ $('#name').css({borderColor: 'red', borderStyle: 'solid', borderWidth: '2px'}); cont = false; }
		if(address.length < 1){ $('#address').css({borderColor: 'red', borderStyle: 'solid', borderWidth: '2px'}); cont = false;}
		if(town.length < 1){ $('#town').css({borderColor: 'red', borderStyle: 'solid', borderWidth: '2px'}); cont = false;}
		if(county.length < 1){ $('#county').css({borderColor: 'red', borderStyle: 'solid', borderWidth: '2px'}); cont = false;}
		if(postcode.length < 1){ $('#postcode').css({borderColor: 'red', borderStyle: 'solid', borderWidth: '2px'}); cont = false;}
		if(phone.length < 1){ $('#phone').css({borderColor: 'red', borderStyle: 'solid', borderWidth: '2px'}); cont = false;}
		if(email.length < 1 || emailcheck == false){ $('#email').css({borderColor: 'red', borderStyle: 'solid', borderWidth: '2px'}); cont = false;}
		if(terms == false){ cont = false; $('#terms').css({borderColor: 'red', borderStyle: 'solid', borderWidth: '2px'}); }
		
		if(cont == true){
			$('#os').html('<img src="../images/layout/ajax-loader.gif" />');
			$.post('../bundles/confirmorder.php', {orderfunc: 'ordercomplete', name: name, address:address, town:town, county: county, postcode: postcode, country: country, phone:phone, email:email, bundleid: bundleid},
			function(data){
				data = jQuery.trim(data);
					switch(data){
						case "1":
						$('.cleftside input').attr("disabled", "disabled");
						$('#topinfo').html('<center>Order Complete.<br /> Your details have been added to our system and we will contact you (usually within 24 hours) to arrange payment and delivery.<br />Thank you.</center>');
						background();
						$('#os').html('Order complete');
						break;
						case "0":
						$('#os').html('<a href="#" onclick="submitOrder()" class="submitorder">Submit Order</a>');
						break;
						default:	
					}
			});	
		}
			
	}
	function adminaddanimal(type){
		$('#adminoptions > div').hide();
		switch(type){
			case "fish":
				$('#type').val('Fish');
				$('#groupsel').html('<select id="group"><option>Freshwater</option><option>Marine</option></select>');
				break;	
			case "reptile":
				$('#type').val('Reptile');
				$('#groupsel').html('<select id="group"><option>Lizard</option><option>Snake</option><option>Turtle</option></select>');
				break;	
			case "other":
				$('#type').val('Other');
				$('#groupsel').html('<select id="group"><option>Amphibian</option><option>Spider</option><option>Invertebrate</option><option>Other</option></select>');
				break;	
			default:
		}
		$('#addanimal').fadeIn();
	}
	
	function addanimal(){
		var type = $('#type').val();
		var group = $('#group').val();
		var genus = $('#genus').val();
		var species = $('#species').val();
		var common = $('#common').val();
		var origin = $('#origin').val();
		var born = $('#born').val();
		var amount = $('#mfu').val(); 
		var re = new RegExp("^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}$");
		if(amount.match(re)){
			
			var price = $('#price').val();
			var desc = $('#desc').val();
			$('#addanimallink').html('<img src="../images/layout/ajax-loader.gif" />');
			$.post('../admin/addanimal.php', {type:type, group:group, genus: genus, species: species, common: common, born:born, origin:origin, price:price, desc: desc, amount:amount},
			function(data){
				data = jQuery.trim(data);
				if(data != 'failed'){
					$('#topinfo').html('Animal Added.&nbsp;&nbsp;&nbsp;<a href="#" onclick="uploadimage(\''+data+'\')">Upload Image for '+genus+' '+species+'</a> | <a href="#" onclick="background();">Close</a>');
				}
				background();
				
			});
		}
		else{
			alert("Your amount (M.F.U) didn't compute. Reminder: It's 'Males.Females.Unknown' in numbers under 99.");
		} 
	}
	function uploadimage(stockid){
			$('#topinfo').html('<form action="http://www.thereptilarium.com/admin/addimage.php" method="post" enctype="multipart/form-data">&nbsp;&nbsp;<input type="hidden" name="stockid" value="'+stockid+'" /><input type="file" name="image" />&nbsp;&nbsp;<input type="submit" value="Upload Image" />');
	}
	function deleteimages(){
		$('.animalimages img').wrap('<a class="delimage" onclick="deleteimage($(this).children().attr(\'id\'))" />)');
	}
	function deleteimage(image){
		$.post('../admin/deleteimage.php', {imageid:image},
		function(data){
			data = jQuery.trim(data);
			if(data=='deleted'){
				$('#'+image).remove();	
			}
			else{
				alert("Error, unable to delete image.");	
			}
		});	 
	}
	  
	function deletenews(){
		$('.newsitem').wrap('<a class="delnews" onclick="deletenewsitem($(this).children().attr(\'id\'))" />)');
	}
	function deletenewsitem(nitem){
		$.post('../admin/deletenews.php', {nitem:nitem},
		function(data){
			data = jQuery.trim(data);
			if(data == 'deleted'){
				$('#'+nitem).remove();
			}
			else{
				alert("Unable to delete news");
			}
		});
	}
	function editanimal(stockid){
		$.post('../admin/editstock.php', {stockid:stockid},
			function(data){
				data = jQuery.trim(data);
				$('#topinfo').html(data);
				background();
			});
	}
	
	function stockcontrol(group){
		$('#sc').html('<img src="../images/layout/ajax-loader.gif" />');
		$.post('../admin/stockcontrol.php', {group:group},
		function(data){
			$('#sc').html(data);
			$('#sc').fadeIn();
		});
	}
	function stockcontrolsearch(){
		$('#sc').html('<img src="../images/layout/ajax-loader.gif" />');
		var searchp = $('#scsearch').val();
		$.post('../admin/stockcontrol.php', {search:searchp},
		function(data){
			$('#sc').html(data);
			$('#sc').fadeIn();
		});
	}
	
	function addComment(){
		$('#addcomment').html('<img src="images/layout/ajax-loader.gif" />');
		cname = $('#cnameinput').val();
		ctext = $('#ctextinput').val();
		bid = $('#blogid').val();
		$.post('../admin/addcomment.php', {cname:cname, ctext:ctext, bid:bid}, function(data){
			data = jQuery.trim(data);
			if(data != "0"){ 
				$('#ci').fadeOut();
				$('#ci').html(data);
				$('#ci').fadeIn();
			}
			else{
				$('#addcomment').html('<a onclick="addComment()" class="addcommentbutton">Add Comment</a>'); 
			}
		});
	}
	function checkval(valp){
		if(valp == "comment"){
			comment = $('#ctextinput').val();
			if(comment == "Comment"){
				$('#ctextinput').val('');
			}
		}
		if(valp == "name"){
			name = $('#cnameinput').val();
			if(name == "Name"){
				$('#cnameinput').val('');
			}
		}
	}
	$(function(){
		$('#searchgo').click(function(){
			var searcht = $('#search').val();
			document.location.href="../animals/animals.php?search="+searcht;
		});
	}); 
	
	$(function(){
		$('#search').keyup(function(e) {
			//alert(e.keyCode);
			if(e.keyCode == 13) {
				var searcht = $('#search').val();
				document.location.href="http://www.thereptilarium.com/animals/animals.php?search="+searcht;
			}
		});
	});
	$(function(){
		$('#scsearch').keyup(function(e) {
			//alert(e.keyCode);
			if(e.keyCode == 13) {
				stockcontrolsearch();
				
			}
		});
	});