// JavaScript Document

//globalValues


function firstTime(){

var first_time, recover, anonymous

first_time	=	$("#new").attr("checked")
recover		=	$("#old").attr("checked")
donor_id_value		=	$("#donor_id").val()
anonymous	=	$("#anon").attr("checked")

if(first_time){
window.location="https://donate.uwt.org/donate_now.asp?ref=n"
}

if(recover){
if (donor_id_value!==""){
window.location="https://donate.uwt.org/donor_verify.asp?id="+donor_id_value
}else{alert("Please enter your donor ID")}
}

if(anonymous){
window.location="https://donate.uwt.org/donate_now.asp?anon=1"
}




}




function selectTab(index){
	
		var $tabs = $('#example').tabs(); // first tab selected
		$tabs.tabs('select', index); 
		return false
}

function setCurrency(){

c	=	$("#cur_id").val()
window.location	=	'default.asp?set_c='+c
	
}


function signUp(fieldVal){

email	=	$("#email_subscribe").val()

if (email==""){
alert("Please enter an email address")
}else if(echeck(email)==false){
	
}else{
	$.ajax({
                 type: "POST",
                 url: "subscribe.asp",
  				 data: "email=" + email,
                 success: function() {
 						alert("Thank you for subscribing")
					$("#email_subscribe,#signup_button").attr('disabled', true)
					 
                 }
             }); //close $.ajax(

}

}

	function signUpText(fieldVal){

textAlert	=	$("#"+fieldVal).val()

if (textAlert==""){
alert("Please enter valid mobile number")	
}else{
	$.ajax({
                 type: "POST",
                 url: "subscribe.asp",
  				 data: "t=1&text=" + textAlert,
                 success: function() {
 						alert("Thank you for subscribing")
					
					 
                 }
             }); //close $.ajax(

}

}

function processDonation(){

var amount	=	$("#Donate_Amount").val()
var project_id	=	$("#project_id").val()
var country_id	=	$("#country_id").val()
var intention	=	$("input[@name='donation_type_id']:checked").val()
var site_location	=	$('#site_location').html()
var currency	=	$("#currencySelect").val()
source = "appeal"
id=""

if(!intention)
alert("Please select an intention i.e. Lillah, Sadaqah, Zakat")

else if(!amount)
alert("Please enter an amount")

else{
url  = 'https://'+site_location+'/inc/actions.asp'
				data = 'action=addToBasket&project_id=' +  project_id + '&country_id=' + country_id + '&donation_type_id=' + intention + '&amount=' +  amount  + '&currency_id=' +  currency + '&referrer=' + source + '&referrerId=' + id

window.location = url + '?' + data	
}
//window.location= "https://donate.uwt.org/front/add_donate.asp?page_redirect=appeal&action=addToBasket&donation_type_id="+intention+"&project_id="+project_id+"&country_id="+country_id+"&amount="+amount+"&currency_id="+currency_id

	   	


	
}


function addToBasket(amount, project, country, intention, quantity, source, id){
var site_location	=	$('#site_location').html()
var currency	=	$('#currency_id_global').html()

source=''
id = ''
if(!intention)
alert("Please select an intention i.e. Lillah, Sadaqah, Zakat")

else if(!amount)
alert("Please enter an amount")

else


	   	url  = 'https://'+site_location+'/inc/actions.asp'
				data = 'action=addToBasket&project_id=' +  project + '&country_id=' + country + '&donation_type_id=' + intention + '&amount=' +  amount  + '&currency_id=' +  currency + '&referrer=' + source + '&referrerId=' + id
//window.location = url + '?' + data

var $iframe	=	$('<iframe></iframe>')

$iframe.attr('src',  url + '?' + data)
$iframe.attr('style', 'width:500; height:400')
$('BODY').append($iframe)
$iframe.load(function(){$iframe.remove()});

var appealThumb = $('#appeal_thumb').text()

var addedHTML = '<table width="90%" class="basket-added-table" align="center"><tr><td rowspan="2" width="20%" valign="top"><img width="72" height="72" src="' + appealThumb + '"></td><td><h1>'+ project  +' in ' + country + '</h1></td></tr><tr><td valign="bottom" style="font-size:14px; padding-top:10px">' + formatAsMoney(amount, currency) +'</td></tr><tr><td  colspan="2" style="border-bottom:1px solid #ccc; height:4px"></td></tr><tr><td><a href="" class="jqmClose"><img src="img/continue.gif" /></a></td> <td align="right"><a href="http://donate.uwt.org/"><img src="img/go-to-payment.gif" /></a></td></tr></table>'

var $modalWindow	= $('<div><div class="topModalWindow"></div><div class="modalContent">' + addedHTML + '</div><div class="bottomModalWindow"></div></div>').addClass('modalWindow').attr('id', 'basketSuccess')





$modalWindow.appendTo('BODY').jqm({overlay: 88, modal: true, trigger: true }).jqmShow().queue(function(){ 
  setTimeout(function(){ 
    $modalWindow.dequeue(); 
  }, 5000 ); 
}).fadeOut('slow', function(){$modalWindow.jqmHide().remove()})



}

function showMoreIntention(){

$(".shown").slideToggle("slow")
$(".hidden").slideToggle("slow")

}


function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}


function getIntValue(strValue, strAction){
	
	
	$.ajax({
		   url: 'actions.asp',
		   type: 'POST',
		   data: {action: strAction, strValue: strValue},
		   success: function(xml){
			   
			   var status	= $(xml).find('Status').text()
			   
			   if(status="Success")
			  	returnValue =  $(xml).find('Value').text()	
			   
		   },
		   error: function(xhr){
				alert(xhr.responseText)   
		   }
		   })
	
	return returnValue
			
}

function formatAsMoney(mnt, currency_id) {
	var currency	=	""
	switch(currency_id){
		case "1":
		currency	=	"&pound;"
	}
	switch(currency_id){
		case "2":
		currency	=	"&#36;"
	}
	switch(currency_id){
		case "3":
		currency	=	"&euro;"
	}
	
			
    mnt -= 0;
    mnt = currency + (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
			  
			  
}

function listProjects(){
	
	var	country_id		=	$("#country_id_select").val();
	var	inputType		=	$("#project_id_select").attr('type');
	var SelectedProject =   $("#project_id_select").attr('s');
	
	$("#project_id_select").attr('s', '');
	if(inputType!=="hidden"){	
	$.ajax({
		type: "POST",
		dataType: "xml", 
		url: "actions.asp",
		data: {action: "listProjectsByCountry", country_id: country_id, SelectedProject: SelectedProject},
		beforeSend: function(){
			$("#country_id_select, #project_id_select, #donation_type_id_select").attr("disabled", true);
		},
		success: function(xml){
			var optionHtml	=	$(xml).find('projectsByCountryHtml').text();
			$("#project_id_select").html(optionHtml);
			$("#country_id_select, #project_id_select, #donation_type_id_select").attr("disabled", false);
			listDonationTypes()
		}
	})
	}else{
			listDonationTypes()
	}
		
}

function listDonationTypes(){
	
	var	project_id		=	$("#project_id_select").val();
	
	$.ajax({
		type: "POST",
		dataType: "xml", 
		url: "actions.asp",
		data: {action: "listDonationTypesByProject", project_id: project_id},
		beforeSend: function(){
			$("#project_id_select, #donation_type_id_select").attr("disabled", true);
		},
		success: function(xml){
			var optionHtml	=	$(xml).find("donationTypesByProjectHtml").text()
			$("#donation_type_id_select").html(optionHtml)
			$("#project_id_select, #donation_type_id_select").attr("disabled", false);
		}
	})
		$("#donation_type_id_select").attr("disabled", false);
}
