/** 
 * @author Raymond Manalo
 * 06.30.09
 * module: Donations Form
 * description: This module will be the new donations form functions.
 * */
 
/**
* Global
* */
var GLB = {};

/**
* Main
* */

var DTE = {
	
	buildDropDowns:function(Obj){
		var optValue = Obj.value;
		DTE.clearOptions('damt');
		DTE.buildOptions('damt',optValue);
	},
	buildOptions:function(ObjId,ObjVal){
		var recurrDiv = Ext.getDom('recurr');
			recurrDiv.style.display = 'none';
			
		var giftDiv = Ext.getDom('gifts');
			giftDiv.style.display = 'none'
		
		var giftInfDiv = Ext.getDom('giftinfo');
			giftInfDiv.style.display = 'none';
		
		var giftAddressDiv = Ext.getDom('giftaddress');
			giftAddressDiv.style.display = 'none';
		
		var SlcBox = Ext.getDom(ObjId);
		var donArr = new Array();
		switch(ObjVal){
			case 'Friend':
				donArr.push('$60','60');
				donArr.push('$5/month','5')
				giftDiv.style.display = 'block'
				//giftInfDiv.style.display = 'block'
				//giftAddressDiv.style.display = 'block'
			break;
			case 'Advocate':
				donArr.push('$120','120')
				donArr.push('$10/month','10')
				giftDiv.style.display = 'block'
				//giftInfDiv.style.display = 'block'
				//giftAddressDiv.style.display = 'block'
			break;
			case 'Colleague':
				donArr.push('$240','240')
				donArr.push('$20/month','20')
				giftDiv.style.display = 'block'
				//giftInfDiv.style.display = 'block'
				//giftAddressDiv.style.display = 'block'
			break;
			case 'Supporter':
				donArr.push('$480','480')
				donArr.push('$40/month','40')
				giftDiv.style.display = 'block'
				//giftInfDiv.style.display = 'block'
				//giftAddressDiv.style.display = 'block'
			break;
			case 'Sponsor':
				donArr.push('$960','960')
				donArr.push('$80/month','80')
				giftDiv.style.display = 'block'
				//giftInfDiv.style.display = 'block'
				//giftAddressDiv.style.display = 'block'
			break;
			case 'Major':
				var tmp = new Option('$1,500 or more','1500')
				giftDiv.style.display = 'block'
				//giftInfDiv.style.display = 'block'
				//giftAddressDiv.style.display = 'block'
			break;
			case 'No Benefits':
				donArr.push('$5','5');
				donArr.push('$10','10');
				donArr.push('$20','20');
				donArr.push('$40','40');
				donArr.push('$60','60');
				donArr.push('$80','80');
				donArr.push('$120','120');
				donArr.push('$240','240');
				donArr.push('$480','480');
				donArr.push('$960','960');
				donArr.push('$1500','1500');
				donArr.push('$5000','5000');
				
				recurrDiv.style.display = 'block'
			break;
			default:
				var tmp = new Option('- Donation Amount -','')
		}
		
		if(ObjVal == 'Major' || ObjVal == ''){
			tmp.selected = true;
			SlcBox.options.add(tmp);
		} else{
			for(i=0;i<donArr.length;i++){
				if(i != donArr.length-1){
					var tmp = new Option(donArr[i],donArr[i+1])
					i++
					SlcBox.options.add(tmp)
				}
			}

		}
		
		DTE.updateFormValues();
	},
	clearOptions:function(ObjId){
		var SlcBox = Ext.getDom(ObjId);
		for(var x=SlcBox.options.length-1;x>=0;x--){
			SlcBox.remove(x);
		}
	},
	checkAmt:function(obj){
		var isNotValidAmt = !/^-?\d+$/.test(obj.value);
		
		if (isNotValidAmt){
			obj.style.border = '1px solid Red';
			error = 'Please use numbers only.\n'
			obj.title = error;
			alert(error)
		} else{
			obj.style.border = '';
			obj.title = '';
		}
	},
	checkEmail:function(obj){
		var normalEmail = new RegExp("^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$");
		var validButRare = new RegExp("^[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,})$");
		
		if (obj.value.match(normalEmail)) {
	    	//valid email
			obj.style.border = '';
			obj.title = '';
	  	} else {
			obj.style.border = '1px solid Red';
			error = 'Please enter a valid email address.'
			obj.title = error
			obj.focus()
	    	alert(error);
	  	}
		
	},
	updateFormValues:function(obj){
		alert(obj.name+'|hello');
		
		var oDIV = Ext.getDom('googlebtn');
		var isNotValid = false;
		var amtObj = Ext.getDom('donationAmt');
		var isanonymous = false;
		var employermatch = false;
		var aboutus = "Internet";
		var favstore = "Chelsea";
		
		if(obj.name == 'donationAmt'){
			var	isNotValid = !/^-?\d+$/.test(obj.value);
		}
		
		//loop the input values
		var chbxcells = Ext.DomQuery.select('input[type="checkbox"]');
		var selcells = Ext.DomQuery.select('select');

		//loop checkboxes
		for(i=0;i<chbxcells.length;i++){
			var tmp = chbxcells[i];
			if(tmp.id == 'isanonymous' && tmp.checked == true){
				isanonymous = tmp.checked;
			}else if(tmp.id == 'employermatch' && tmp.checked == true){
				employermatch = tmp.checked;
			}
		}
		
		//loop selectboxes
		for(i=0;i<selcells.length;i++){
			var tmp = selcells[i];
			if(tmp.id == 'aboutus'){
				aboutus = tmp.value;
			}else if(tmp.id == 'favstore'){
				favstore = tmp.value;
			}
		}
		
		//get the honor div
		var honorname = Ext.getDom('honorname');
		var honormessage = Ext.getDom('honormsgtxt');
		var hname = honorname.value;
		var hmsg = honormessage.value;
		 
		var amount = amtObj.value;
		
		if (isNotValid){
			amtObj.style.background = 'Red';
			error = 'Please use numbers only.\n'
			amtObj.title = error;
			alert(error)
		}else{
			
			oDIV.innerHTML = '<img src="/i/screen/loading.gif" />'
			amtObj.style.background = '';
		
			Ext.Ajax.request({
				params:{
					donationAmount:amount,
					isanonymous:isanonymous,
					employermatch:employermatch,
					aboutus:aboutus,
					favstore:favstore,
					honoree:hname,
					honoreemsg:hmsg
				},
				url:'/donate/includes/JSONGateway.cfm?comp=checkoutClient&method=getDonationAmount',
				success: function(r){
					var resp = Ext.decode(r.responseText);
					console.log(resp)
					var ptext = '<p>Housing Works uses google checkout to securely process your online donation.<br/>Click on the button below to complete your transaction.</p>';
					oDIV.innerHTML = ptext + resp;
				}
			});
			
		}
		
	},
	updateSessionValues:function(obj){
		var isObject = typeof(obj);
		var objName = "";
		var objValue = "";

		if(isObject.indexOf('object')>-1){
			objName = obj.name;
			objValue = obj.value;
		}
		
		//console.log(objName+'|'+objValue)
		/*Ext.Ajax.request({
			params:{
				objname:objName,
				objvalue:objValue
			},
			url:'/donate/includes/JSONGateway.cfm?comp=checkoutClient&method=getCartItems',
			success: function(r){
				console.log(r.responseText)
				var resp = Ext.decode(r.responseText);
				console.log(resp)
			}
		});	*/

		
	},
	getThankYouGift:function(obj){
		//console.log(obj.value)
		
		var giftInfDiv = Ext.getDom('giftinfo');
			giftInfDiv.style.display = 'none';
		
		var giftAddressDiv = Ext.getDom('giftaddress');
			giftAddressDiv.style.display = 'none';
		
		if(obj.value != 'No Gift'){
				giftInfDiv.style.display = 'block'
				giftAddressDiv.style.display = 'block'		
		}

	},
	getRadioValue:function(){
		var formObj = document.donationform;	
		var radioLength = formObj.recur.length;	
		//console.log(formObj.recur.length)
		
		for (var i=0; i < radioLength; i++){
		   if (formObj.recur[i].checked){
		      var rad_val = formObj.recur[i].value;
		      }
		   }
		return rad_val
	},
	changeRadioVal:function(radioObj){
		//console.log(radioObj.value)
	},
	viewMore:function(obj){
		//Ext.select('.click_div').on('click',function(evt,el){

			
			var slideMe = Ext.get(obj.id).next();
			
			var opt = {
					easing: 'easeOut',
					duration: .5,
					remove: false,
					useDisplay: true,
					block: true
			};
			
			if(slideMe.isVisible()){
				slideMe.stopFx().slideOut('t',opt);
			}else{
				slideMe.stopFx().slideIn('t',opt);
			}
					
		//});
	},
	viewInHonor:function(obj){
		var slideMe = Ext.get('honormsg');
		var opt = {
					easing: 'easeOut',
					duration: .5,
					remove: false,
					useDisplay: true,
					block: true
			};
			
			if(slideMe.isVisible()){
				slideMe.stopFx().slideOut('t',opt);
			}else{
				slideMe.stopFx().slideIn('t',opt);
			}
	},
	viewNewsletters:function(obj){
		var slideMe = Ext.get('newsletters');
		var opt = {
					easing: 'easeOut',
					duration: .5,
					remove: false,
					useDisplay: true,
					block: true
			};
			
			if(slideMe.isVisible()){
				slideMe.stopFx().slideOut('t',opt);
			}else{
				slideMe.stopFx().slideIn('t',opt);
			}
	}
	


	
}

var MEB = {
	toggleSelectedLevel:function(listId){
		//get the li from the DOM returns an array
		var cells = Ext.DomQuery.select('li[class="listing"]');
		
		//loop the cells
		for(i=0;i<cells.length;i++){
			var tmp = cells[i];
			if((tmp.style.background == 'white none repeat scroll 0% 0%' || tmp.style.background == 'white') && tmp.id != listId){
				tmp.style.background = '';
			}else if(tmp.id == listId){
				tmp.style.background = 'white';
			}
		}
	},
	getCartLevel:function(obj,listId){
		var isObject = typeof(obj);
		var objName = "";
		var objValue = "";
		var otherRadioGroupName = "";
		var otherRadioGroupValue = "";
		var oDIV = Ext.getDom('googlebtn');
		var oLoading = Ext.getDom('loading');
					
		//toggle the background color of the selected level
		MEB.toggleSelectedLevel(listId);
		
		if(isObject.indexOf('object')>-1){
			objName = obj.name;
			objValue = obj.value;
		}
		
		//check which radio group was selected
		if(objName.indexOf('recur')>-1){
			//get the gift radio group
			var grp = document.getElementsByName('gift');
			var len = document.getElementsByName('gift').length;
			
		} else{
			//get the membership radio group
			var grp = document.getElementsByName('recur');
			var len = document.getElementsByName('recur').length;
		}
		
		//loop to get the other radio group name/value
		for(i=0;i<len;i++){
			if(grp[i].checked){
				otherRadioGroupName = grp[i].name;
				otherRadioGroupValue = grp[i].value;
			}
		}
		
		oDIV.innerHTML = '<img src="/i/screen/loading.gif" />'

		Ext.Ajax.request({
			params:{
				objname:objName,
				objvalue:objValue,
				objnameother:otherRadioGroupName,
				objvalueother:otherRadioGroupValue
			},
			url:'/donate/includes/JSONGateway.cfm?comp=checkoutClient&method=getCartLevel',
			success: function(r){
				var resp = Ext.decode(r.responseText);
				var ptext = '<p>Housing Works uses google checkout to securely process your online donation.<br/>Click on the button below to complete your transaction.</p>';
					oDIV.innerHTML = ptext + resp;
			}
		});	
	
		
	},
	updateSessionData:function(obj){
		var objName = obj.name;
		var objValue = obj.value;
		
		Ext.Ajax.request({
			params:{
				objname:objName,
				objvalue:objValue
			},
			url:'/donate/includes/JSONGateway.cfm?comp=checkoutClient&method=updateSessionData',
			success: function(r){
				//console.log(r.responseText)
				var resp = Ext.decode(r.responseText);
				//console.log(resp)
			}
		});	
	}
}