
jQuery.noConflict();
jQuery(document).ready(function($){
	
		
		$(document).ajaxError(function(){
			if (window.console && window.console.error) {
				console.error(arguments);
			}
		});
		/*** Global Variables ***/
			var selectedTripCost;
			var selectedTripTitle;
			
			$('input[type="text"]').focus(function() {
					$(this).addClass("focus");
			});

			$('input[type="text"]').blur(function() {
					$(this).removeClass("focus");
			});
				
			/*** Toggle Trip Category's 
				*On Click of TripCategory List box load the content of the next box from XML file
			**/
			$("#tripCategory").change(function(){
			//alert('ping');
				var str = $("#tripCategory option:selected").val();
				//alert(str);	
				$("#tripInfo").hide();
				$('#pbInfo').css("display","none");
				if (str=="ShortTrips") {
					$("#shortTrips > option").remove();
					/**** Short Trips ***/		
					$.ajax({
					type: "GET",
					url: "/scripts/shorttrips.xml",
					dataType: "xml",
					success: function(xml) {
						var select = $('#shortTrips');
						$(xml).find('trip').each(function(){
							var id = $(this).attr('id');
							var title = $(this).find('title').text();						
							select.append("<option value='"+ id +"'>"+title+"</option>");
							});
						}, //Success Funtion ENds				
						error:function(xhr,err,er){alert("Error Getting ShortTrips: " + err.description);}	
					});
					/**** Short Trips ENds **/
					$('#shortTrips').show();
				}
				else{$('#shortTrips').hide();}
				if (str=="LongTrips"){
					$("#longTrips > option").remove();
					/**** Long Trips ***/			
					$.ajax({
						type: "GET",
						url: "scripts/longtrips.xml",
						dataType: "xml",
						success: function(xml) {
							var select = $('#longTrips');
							$(xml).find('trip').each(function(){
								var id = $(this).attr('id');
								var title = $(this).find('title').text();
								var cost = $(this).find('cost').text();
								select.append("<option value='"+ id +"'>"+title+"</option>");
								});
							}
						});
					/**** Long Trips Ends **/					
					$('#longTrips').show();	
				}
				else{$('#longTrips').hide();}
				if (str=="Paintball"){
					$("#paintball > option").remove();
					/**** Paintball Trips ***/
					$.ajax({
						type: "GET",
						url: "/scripts/paintball.xml",
						dataType: "xml",
						success: function(xml) {
							var select = $('#paintball');
							$(xml).find('trip').each(function(){
								var id = $(this).attr('id');
								var title = $(this).find('title').text();
								var cost = $(this).find('cost').text();
								select.append("<option value='"+ id +"'>"+title+"</option>");
								});
							}
					});
					/**** Paintball Trips Ends **/
					$('#paintball').show();	
				}
				else{$('#paintball').hide();}
				/*** Gear ***/
					if (str=="Gear"){
					$("#gear > option").remove();					
					$.ajax({
						type: "GET",
						url: "scripts/gear.xml",
						dataType: "xml",
						success: function(xml) {
							var select = $('#gear');
							$(xml).find('gear').each(function(){
								var id = $(this).attr('id');
								var title = $(this).find('name').text();
								var cost = $(this).find('cost').text();
								select.append("<option value='"+ id +"'>"+title+"</option>");
								});
							}
						});
									
					$('#gear').show();	
				}
				else{$('#gear').hide();}
				
			});
			
			
		/*** Toggle Trip Category Ends **/

		/** Fetch Shot Trip information from the XML file on the selection of a short trip **/
		/**** Toggle Short Trip Information ***/
			$("#shortTrips").change(function(){
				var shorttripurl = "/scripts/shorttrips.xml";
				$.ajax({
				type: "GET",
				url: shorttripurl,
				dataType: "xml",
				success: function(xml) {				
				var str =$("#shortTrips option:selected").val();
				
				$(xml).find("trip[id="+ parseInt(str) +"]").each(function(){
					var title = $(this).find('title').text();
					var cost = $(this).find('cost').text();
					selectedTripCost = $(this).find('cost').text();
					selectedTripTitle = $(this).find('title').text();
					var desc = $(this).find('long').text();
					//alert(cost);	
					var desclabel = $('#tripDesc');					
					var costlabel = $('#tripCost');					
					desclabel.html(title);
					costlabel.html("Rs. "+ cost + "/-(per pax)");
					$("#tripInfo").show();
					$('#pbInfo').css("disabled","disabled");
					$('#pbInfo').css("display","none");	
				});					
				}, //Success Funtion ENds				
				error:function(xhr,err,er){alert("Short Trips Error: " + err);}
				});			
			});
			//Long trips
			$("#longTrips").change(function(){
				var paintballurl = "/scripts/longtrips.xml";
				$.ajax({
				type: "GET",
				url: paintballurl,
				dataType: "xml",
				success: function(xml) {				
				var str =$("#longTrips option:selected").val();
				$(xml).find("trip[id="+ parseInt(str) +"]").each(function(){
					var title = $(this).find('brief').text();
					var cost = $(this).find('cost').text();
					selectedTripCost = $(this).find('cost').text();
					selectedTripTitle = $(this).find('title').text();
					var desc = $(this).find('brief').text();
					//alert(cost);	
					var desclabel = $('#tripDesc');					
					var costlabel = $('#tripCost');					
					desclabel.html(title);
					costlabel.html("Rs. "+ cost + "/-(per pax)");
					$("#tripInfo").show();
					$('#pbInfo').css("disabled","disabled");
					$('#pbInfo').css("display","none");	
				});					
				}, //Success Funtion ENds				
				error:function(xhr,err,er){alert("Paintball Error: " + err);}
				});			
			});
			//Paintball 
			$("#paintball").change(function(){
				var paintballurl = "/scripts/paintball.xml";
				$.ajax({
				type: "GET",
				url: paintballurl,
				dataType: "xml",
				success: function(xml) {				
				var str =$("#paintball option:selected").val();
				$(xml).find("trip[id="+ parseInt(str) +"]").each(function(){
					var title = $(this).find('long').text();					
					selectedTripTitle = $(this).find('title').text();
					var desc = $(this).find('long').text();
					//alert(cost);	
					var desclabel = $('#tripDesc');					
					var costlabel = $('#tripCost');				
					desclabel.html(title);					
					$("#tripInfo").show();
					$('#customPayment').css("disabled","disabled");
					$('#customPayment').css("display","none");
					$('#pbInfo').css("display","none");
					$('#pbInfo').css("display","block");
					if (str == "0"){ //Yeur
 						$('#YeurpaintballPackage').css("display","");
						$('#paintballPackage').css("disabled","disabled");
						$('#paintballPackage').css("display","none");
						$('#totalCost').html("Rs " + $('#YeurpaintballPackage option:selected').val() + " /-");
						selectedTripCost = $('#YeurpaintballPackage option:selected').val();
					}
					else{
						$("#YeurpaintballPackage").css("disabled","disabled");
						$('#YeurpaintballPackage').css("display","none");
						$('#paintballPackage').css("display","");
						$('#totalCost').html("Rs " + $('#paintballPackage option:selected').val() +"* /-");
						selectedTripCost = $('#paintballPackage option:selected').val();
						}
					$('#pbInfo').css("visibility","");
					$('#pbInfo').css("border","1 solid");
					$('#txtPersons').val("");
					
				});					
				}, //Success Funtion ENds				
				error:function(xhr,err,er){alert("Paintball Error: " + err);}
				});			
			});
			/** Outdoor Gear ***/
			$("#gear").change(function(){
				var paintballurl = "/scripts/gear.xml";
				$.ajax({
				type: "GET",
				url: paintballurl,
				dataType: "xml",
				success: function(xml) {				
				var str =$("#gear option:selected").val();
				$(xml).find("gear[id="+ parseInt(str) +"]").each(function(){
					var title = $(this).find('name').text();
					var cost = $(this).find('cost').text();
					selectedTripCost = $(this).find('cost').text();
					selectedTripTitle = $(this).find('title').text();
					var desc = $(this).find('name').text();
					//alert(cost);	
					var desclabel = $('#tripDesc');					
					var costlabel = $('#tripCost');					
					desclabel.html(title);
					costlabel.html("Rs. "+ cost + "*/-(per pax)");
					$("#tripInfo").show();
					$('#pbInfo').css("disabled","disabled");
					});					
				}, //Success Funtion ENds				
				error:function(xhr,err,er){alert("Gear Error: " + err);}
				});			
			});
/**** Toggle Trip Information Ends***/
/*** Custom Payment Form ***/	
	$("#tripCategory").change(function(){
		var str = $("#tripCategory option:selected").val();
		$("#tripInfo").hide();
		//alert('Hello');
		if (str=="custom") {
			$("#shortTrip.")
			//alert("Houston im in");
			$('#customPayment').css("display","block");
			var desclabel = $('#tripDesc');var costlabel = $('#tripCost');	
			desclabel.html("");costlabel.html("");
			desclabel.html("Please Enter an Amount that you want to Pay and Mention the Service:");
			$("#tripInfo").show();			
			$('#pbInfo').css("disabled","disabled");
			$('#pbInfo').css("display","none");			
		}else{$('#customPayment').css("display","none");}
	});


	$("#paintballPackage").change(function(){
		$('#txtPersons').val("");
		$('#totalCost').html("Rs " + $('#paintballPackage option:selected').val() + "* /-");
		selectedTripCost = $('#paintballPackage option:selected').val();
	});
	$("#YeurpaintballPackage").change(function(){
		$('#txtPersons').val("");
		$('#totalCost').html("Rs " + $('#YeurpaintballPackage option:selected').val() + " /-");
		selectedTripCost = $('#YeurpaintballPackage option:selected').val();
	});
	
	/*** calculate total cost **/
	$('#txtPersons').blur(function(){
		var str =$("#paintball option:selected").val();
		var noOfPersons = $('#txtPersons').val();
		var cost1 = $("#paintballPackage option:selected").val();
		var cost2 = $("#YeurpaintballPackage option:selected").val();
		if (str == "0"){ var totalcost = parseInt(noOfPersons) * parseInt(cost2);}
		else{var totalcost = parseInt(noOfPersons) * parseInt(cost1);}
		//alert(totalcost);
		$('#totalCost').html("Rs "+totalcost+"* /-");
		selectedTripCost = totalcost;
	});
	
	/**** Enable Submit Button ***/
	$("#agreed").change(function(){		

		var isChecked = $("#agreed:checked").val();

		if(isChecked=="on"){
			$("#subBtn").removeAttr("disabled");
		}		
		else {
			$("#subBtn").attr("disabled","disabled");
		}

	});

/**** Change Action attribute of Form, for Payment Gateway ***/

$('input[name=paymentGroup]').change(function(){

		var paymentOption = $('input[name=paymentGroup]:checked').val();
		if (paymentOption == "PaymentOpt1")
			{
				//alert("Changing");
				$('#regForm').attr("action","http://www.timesofmoney.com/direcpay/secure/dpMerchantTransaction.jsp");				
				$('#regForm').attr("method","post");
			}
		else {$('#regForm').attr("action","http://natura.co.in/");}
});
/**** Validation ***/

$("#subBtn").click(function(){		
	var boolEmpty=false;
	$('input, textarea').each(function(){
		$(this).val(jQuery.trim($(this).val()));
	});
	var str = $("#tripCategory option:selected").val();
	if(str=="custom")
	{$('#custAmt').addClass('required');$('#custAmtComment').addClass('required');}
	
	$('.required').each(function isempty(){
		if ($(this).val()==""){
			//alert("empty");
			$(this).addClass("error");
			$(this).focus();
			//alert("error");
			boolEmpty=true;
			return false;
			
		}			
	});
	if (!(boolEmpty)){
		$('#deliveryName').val($('#custName').val());
		$('#deliveryAddress').val($('#custAddress').val());
		$('#deliveryCity').val($('#custCity').val());
		$('#deliveryState').val($('#custState').val());
		$('#deliveryPinCode').val($('#custPinCode').val());
		$('#deliveryCountry').val($('#custCountry').val());
		$('#deliveryPhNo1').val($('#custPhoneNo1').val());
		$('#deliveryPhNo2').val($('#custPhoneNo2').val());
		$('#deliveryPhNo3').val($('#custPhoneNo3').val());
		$('#deliveryMobileNo').val($('#custMobileNo').val());
		$('#txttripCost').val(selectedTripCost);
		$('#txttripDesc').val(selectedTripTitle);
		if (! ($('#custAmt').val()) == ""){
				selectedTripCost = $('#custAmt').val();
				//alert(selectedTripCost);
		}
		var  strOrderNo = "Pgr-0987" ;
		//alert(selectedTripCost);
		var param ="200908111000001|DOM|IND|INR|" + selectedTripCost + "|" + strOrderNo + "|" +  $('#tripDesc').val() + "|http://natura.co.in/index.php?option=com_content&view=article&id=58|http://natura.co.in/index.php?option=com_content&view=article&id=59|DirecPay";
		$('#requestparameter').val(param);
		var strParam = encodeValue($('#requestparameter').val());
		$('#requestparameter').val(strParam);
		//alert("disabling");
		$('.delivery').attr("disabled","disabled");
		$('.disabled').attr("disabled","disabled");
		$('.db').removeAttr("disabled");
		//alert("posting");
		/*** Send Frm Data to db ***/	
			$.ajax({
					type: "POST",
					url: "/forms/reg-new.php",
					data: $("#regForm :input[value]").serialize(),
					success:function(msg){$('#reply').html(msg);}
					//error:function(xhr,err,er){$('#reply').html(err);}				
			});				
		
		//alert("posted");
		$('.delivery').removeAttr("disabled");
		$('.db').attr("disabled","disabled");
		//alert("Redirecting");
		return true;
	}
	else{
	alert("There was Some error in Registration, Please check whether you have filled in all the fields.");
	return false;}
});
/*** Validation Ends ***/
}); //Main Function Ends



function mycarousel_itemAddCallback(carousel, first, last, xml)
{
	//alert("in add");
    // Set the size of the carousel
    carousel.size(parseInt(jQuery('total', xml).text()));

    jQuery('image', xml).each(function(i) {
        carousel.add(first + i, mycarousel_getItemHTML(jQuery(this).text()));
    });
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(url)
{
    return '<a href="http://natura.co.in/shorttrips/"><img src="images/thumbs/' + url + '" width="75" height="75" alt="" /></a>';
};
function mycarousel_itemLoadCallback(carousel, state,trip)
{
	// Check if the requested items already exist
    if (carousel.has(carousel.first, carousel.last)) {
        return;
    }
    jQuery.get(
        '/scripts/getPics.php',
        {
            first: carousel.first,
            last: carousel.last,
			trip: 'longtrips'
        },
        function(xml) {
            mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, xml);
        },
        'xml'
    );
	//alert("after jQuery get");
};