// JavaScript Document
var currentdoc=null;
var ajaxObject=function(){
           
        this.createRequestObject=function() {
                var tmpXmlHttpObject;
             
                //depending on what the browser supports, use the right way to create the XMLHttpRequest object
                if (window.XMLHttpRequest) {
                    // Mozilla, Safari would use this method ...
                    tmpXmlHttpObject = new XMLHttpRequest();
                   
                } else if (window.ActiveXObject) {
                // IE would use this method ...
                tmpXmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
            }
            return tmpXmlHttpObject;
        };
        var http=this.createRequestObject();
		/**
		dual login
		*/
		
		this.dualLogin=function(url){
			http.open('get',url);
			
            //assign a handler for the response
            http.onreadystatechange = this.dualLoginRs;   
            //actually send the request to the server
            http.send(null);
		}
		this.dualLoginRs=function(){
			 if(http.readyState == 4){
          		var response = http.responseText;
				callSubmit();
			 }else{
			 }
		}
		/***
			get music
		*/
		this.httGetMusic=function(url){
            http.open('get',url);
            //assign a handler for the response
            http.onreadystatechange = this.showMusic;   
            //actually send the request to the server
            http.send(null);

		};
		this.showMusic=function(){
		 if(http.readyState == 4){
          	var response = http.responseText;
		 	obj=eval("("+response+")");
			for(i=0;i<obj.length;i++){
				item=obj[i];
				
			}
			showMusicBox(obj);
       	   }else{
			// document.getElementById("progress-load").style.display='block';
		  }
		}
		this.httpPreviewCard=function (url){
		   //make a connection to the server ... specifying that you intend to make a GET request
            //to the server. Specifiy the page name and the URL parameters to send
            http.open('get',url);
            //assign a handler for the response
            http.onreadystatechange = this.previewCard;   
            //actually send the request to the server
            http.send(null);
		}
		this.previewCard=function(){
		 if(http.readyState == 4){
           	 var response = http.responseText;
		 	//	alert(response);
 
			start=response.indexOf("<object");
			end=response.indexOf("</body>");

			x_flashcard1=response.substring(start,end);
			//alert(x_flashcard1);
			//document.getElementById('cardcontent').innerHTML=x_flashcard1;
			//centerdiv(1000,1000,'ui-dialog-overlay');		
			document.getElementById('print_fullsize').innerHTML=x_flashcard1;
			ShowDivCenterPage('view_fullsize_table',1);
			//centerdiv(500,500,'flashcard1');
		   // document.getElementById("progress-load").style.display='none';
       	   }else{
		//	 document.getElementById("progress-load").style.display='block';
		  }
		}
		this.httpGetData=function(url){
			   //make a connection to the server ... specifying that you intend to make a GET request
            //to the server. Specifiy the page name and the URL parameters to send
        
            http.open('get',url);
            //assign a handler for the response
            http.onreadystatechange = this.returnXMLData;   
            //actually send the request to the server
            http.send(null);
		}
	 this.returnXMLData=function(){
        if(http.readyState == 4){
            var response = http.responseText;
		//	alert(response);
            xmldoc=xmlUtil.load(response);
//			alert(xmldoc.getElementsByTagName("organization").length);
			currentdoc=xmldoc;
		    showCharitesListXML(xmldoc);
		   document.getElementById("progress-load").style.display='none';
        }else{
			document.getElementById("progress-load").style.display='block';
		}

	}
	this.httpGetDetail=function(url){
			   //make a connection to the server ... specifying that you intend to make a GET request
            //to the server. Specifiy the page name and the URL parameters to send
        
            http.open('get',url);
            //assign a handler for the response
            http.onreadystatechange = this.returnDetailJsonData;   
            //actually send the request to the server
            http.send(null);
		}
	this.returnDetailJsonData=function(){
        if(http.readyState == 4){
            var response = http.responseText;
            obj=eval("("+response+")");
			showCharityDetail(obj);
			//detailp=new floatPopup();
			//detailp.init(0,0,true,"Charity Detail","Hello",100,100,true);
   			 //detailp.display();
		 //  showCharitesList(obj);
		  //document.getElementById("progress-load").style.display='none';
        }else{
			//document.getElementById("progress-load").style.display='block';
		}

	}
	
};
showCharitesListXML=function(xmldoc){
	html="";
	links="";
	charities=xmldoc.getElementsByTagName("organization");
	
	count=(xmldoc.getElementsByTagName("totalResults")[0].childNodes[0].nodeValue)*1;
	document.getElementById('rs_count').innerHTML=count;
	if(count>0){
	resultPosition=(xmldoc.getElementsByTagName("resultPosition")[0].childNodes[0].nodeValue)*1;
	currentpage=Math.round(resultPosition/20)+1;
	numofpage=Math.round(count/20);
	links=pagerUtil.get_page_links(currentpage,numofpage);
	
	for(i=0;i<charities.length;i++){
		charity=charities[i];
		generalInformation=charity.getElementsByTagName("generalInformation")[0];
		orgName=generalInformation.getElementsByTagName("orgName")[0];
		ein1=charity.getElementsByTagName("ein")[0].childNodes[0].nodeValue;
		
		html+="<div class='charity-item')\"><a href=\"javascript:pickupCharity('"+ein1+"')\" class=\"charity-link\">"+orgName.childNodes[0].nodeValue+"</a></div>";
	}
		//alert(html);
	}
	document.getElementById("charities-list").innerHTML=html;
	document.getElementById("pages-links").innerHTML=links;


}
showCharitesList=function(obj){
	html='';
	charities=obj[0];
	links=obj[1];
	for(i=0;i<charities.length;i++){
		html+="<div class='charity-item')\"><a href=\"javascript:pickupCharity('"+charities[i].charity_id+"')\" class=\"charity-link\">"+charities[i].charity_name+"</a></div>";
	}
	document.getElementById("charities-list").innerHTML=html;
	document.getElementById("pages-links").innerHTML=links;
}
goToPage=function(page){
	state=document.getElementById("cs_state").value;
	//cType=document.getElementById("cType").value;
	
	keyword=document.getElementById("searchCharity").value;
	city1=document.getElementById("city").value;
	ein1=document.getElementById("ein").value;
	organization1=document.getElementById("organization").value;
	category1=document.getElementById("category").value;
//	url="index.php?step=get_charity_ajax&state="+state+"&ctype="+cType+"&searchString="+searchString;
	url="index.php?step=get_charity_ajax_webs&state="+state+"&city="+city1+"&keyword="+keyword+"&ein="+ein1+"&organization="+organization1+"&category="+category1+"&page="+page;
//	alert(url);
	jax=new ajaxObject();
	jax.httpGetData(url);
	
	
}
var parseCharityXML={
	check:function(xmldoc,ein1){
		
		values=Array();
		charities=xmldoc.getElementsByTagName("organization");	
		count=(xmldoc.getElementsByTagName("totalResults")[0].childNodes[0].nodeValue)*1;
		
		for(i=0;i<charities.length;i++){
			charity=charities[i];
			missionAndPrograms=charity.getElementsByTagName("missionAndPrograms")[0];
			generalInformation=charity.getElementsByTagName("generalInformation")[0];
			pein=generalInformation.getElementsByTagName("ein")[0].childNodes[0].nodeValue;
			if(ein1==pein){
				
				generalInformation=charity.getElementsByTagName("generalInformation")[0];
				orgName=generalInformation.getElementsByTagName("orgName")[0];
				
				city1=generalInformation.getElementsByTagName("city")[0];
				state1=generalInformation.getElementsByTagName("state")[0];
				zip1=generalInformation.getElementsByTagName("zip")[0];
				
				deductibility=generalInformation.getElementsByTagName("deductibility")[0];
				mission=missionAndPrograms.getElementsByTagName("mission")[0];
				
				values[0]=orgName.childNodes[0].nodeValue;
				if(city1.childNodes[0]!=null){
					values[1]=city1.childNodes[0].nodeValue;
				}
				if(state1.childNodes[0]!=null){
					values[2]=state1.childNodes[0].nodeValue;
				}
				if(zip1.childNodes[0]){
				values[3]=zip1.childNodes[0].nodeValue;
				}
				if(mission.childNodes[0]!=null){
					values[4]=mission.childNodes[0].nodeValue;
				}
				break;
			}
		}
		return values;
	}
}
pickupCharity=function(id){
	//ajax=new ajaxObject();
	//url="index.php?step=get_charity_detail&id="+id;
	//ajax.httpGetDetail(url);
	detail=parseCharityXML.check(xmldoc,id);

	if(detail.length>0){
		
		html="<table>";
		html+="<tr><th align='left'>Name</th><td>"+detail[0]+"</td></tr>";
		html+="<tr><th align='left'>EIN</th><td>"+id+"</td></tr>";
		html+="<tr><th align='left'>City</th><td>"+detail[1]+"</td></tr>";
		html+="<tr><th align='left'>State</th><td>"+detail[2]+"</td></tr>";
		html+="<tr><th align='left'>Zip</th><td>"+detail[3]+"</td></tr>";
		html+="<tr><th align='left'>Mission</th><td>"+detail[4]+"</td></tr>";
		html+="<tr><th align='left'>NOC URL</th><td>"+''+"</td></tr>";
		html+="<tr><td colspan='2' align='right'><a href=\"javascript:pickupThisCharity('"+id+"')\">Select This Charity</a></td></tr>";
		html+="<tr><th></th><td></td></tr>";
		html+="</table>";
		

		detailp.init(0,0,true,"Charity Detail",html,100,100,true);
    	detailp.display();
	}
}
showCharityDetail=function(obj){
	html="<table>";
	html+="<tr><th align='left'>Name</th><td>"+obj.charity_name+"</td></tr>";
	html+="<tr><th align='left'>Address</th><td>"+obj.charity_address+" " +obj.charity_city+" "+obj.charity_state+"</td></tr>";
	html+="<tr><th align='left'>Phone</th><td>"+obj.charity_phone+"</td></tr>";
	html+="<tr><th align='left'>Website</th><td>"+obj.charity_url+"</td></tr>";
	html+="<tr><th align='left'>NOC URL</th><td>"+obj.charity_noc_url+"</td></tr>";

	if(obj.charity_verified==0){
		html+="<tr><td colspan='2' align='right'><font color='red'><i>This charity is unverified, to edit charity information, select </i></font><a href=\"javascript:editThisCharity('"+escape(obj.data)+"')\">Edit<br></a>  <a href=\"javascript:pickupThisCharity("+obj.charity_id+")\" >Pick up</a></td></tr>";
		
	}else{
		html+="<tr><td colspan='2' align='right'><a href=\"javascript:pickupThisCharity("+obj.charity_id+")\">Pick up</a></td></tr>";
	}
	html+="<tr><th></th><td></td></tr>";
	html+="</table>";
	
	detailp.init(0,0,true,"Charity Detail",html,100,100,true);
    detailp.display();
};
pickupThisCharity=function(id){
	//ajax=new ajaxObject();
	//url="index.php?step=get_charity_detail&id="+id;
	//ajax.httpGetDetail(url);
	detail=parseCharityXML.check(xmldoc,id);
	html="";
	if(detail.length>0){

		html+=detail[0]+"|";
		html+=id+"|";
		html+=detail[1]+"|";
		html+=detail[2]+"|";
		html+=detail[3]+"|";
		html+=detail[4];
	}
	document.getElementById("charity_data").value=html;
	document.getElementById("charity_id").value=id;
	document.ecardmax_join.submit();
	
};
editThisCharity=function(data){
	
	
	obj=eval("("+data+")");
	/*
	detailp.hide();
	pop.hide();
	html="<form name='editpop' id='editpop'>";
	html+="<table>";
	html+="</table>";
	editPop.init(0,0,true,"Charity Detail",html,100,100,true);
    editPop.display();
	*/
	location.href="index.php?step=edit_charity&id="+obj.charity_id;
}
selectLater=function(){

		document.getElementById("charity_id").value='';
		document.ecardmax_join.submit();
	
};
function centerdiv(Xwidth,Yheight,divid) {
// First, determine how much the visitor has scrolled
var scrolledX, scrolledY;
if( self.pageYOffset ) {
scrolledX = self.pageXOffset;
scrolledY = self.pageYOffset;
} else if( document.documentElement && document.documentElement.scrollTop ) {
scrolledX = document.documentElement.scrollLeft;
scrolledY = document.documentElement.scrollTop;
} else if( document.body ) {
scrolledX = document.body.scrollLeft;
scrolledY = document.body.scrollTop;
}

// Next, determine the coordinates of the center of browser's window

var centerX, centerY;
if( self.innerHeight ) {
centerX = self.innerWidth;
centerY = self.innerHeight;
} else if( document.documentElement && document.documentElement.clientHeight ) {
centerX = document.documentElement.clientWidth;
centerY = document.documentElement.clientHeight;
} else if( document.body ) {
centerX = document.body.clientWidth;
centerY = document.body.clientHeight;
}

// Xwidth is the width of the div, Yheight is the height of the
// div passed as arguments to the function:
var leftOffset = scrolledX + (centerX - Xwidth) / 2;
var topOffset = scrolledY + (centerY - Yheight) / 2;
// The initial width and height of the div can be set in the
// style sheet with display:none; divid is passed as an argument to // the function
var o=document.getElementById(divid);
var r=o.style;
r.position='absolute';
r.top = topOffset + 'px';
r.left = leftOffset + 'px';
r.display = "block";
} 
var xmlUtil={
	load:function(xml){
		if(window.ActiveXObject){
		  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  		  xmlDoc.async="false";
		  xmlDoc.loadXML(xml);
		  return xmlDoc;
		}else{
		  parser=new DOMParser();
          xmlDoc=parser.parseFromString(xml,"text/xml");
		  return xmlDoc;
		}
	}
};
var pagerUtil={
	get_page_links:function(page,b){
		count_number='';
		y=0;
		if (b <10){
			for(a_num=1; a_num<=b; a_num++) {
				y++;
				if (a_num == page) {
					count_number +="<span style=\"cursor: default;\" class='page_active'>&nbsp;"+a_num+"&nbsp;</span>&nbsp;";					
				}
				else {
					count_number +="<a class=\"page_other\" href=\"javascript:goToPage("+a_num+")\">&nbsp;"+a_num+"&nbsp;</a>&nbsp;";
				}
			}
		}else if((page > 3) && (page < (b-3))){
			for(a_num=1; a_num<=3; a_num++) {
				y++;
				count_number +="<a class=\"page_other\" href=\"javascript:goToPage("+a_num+")\">&nbsp;"+a_num+"&nbsp;</a>&nbsp;";
			}
			count_number +="...";
			for(a_num = page-1; a_num<=page+1; a_num++) {
				y++;
				if (a_num == page) {
					count_number +="&nbsp;<span style=\"cursor: default;\" class='page_active'>&nbsp;"+a_num+"&nbsp;</span>&nbsp;";
				}
				else {		
					count_number +="<a class=\"page_other\" href=\"javascript:goToPage("+a_num+")\">&nbsp;"+a_num+"&nbsp;</a>&nbsp;";
				}
			}
			count_number +="...";
			for(a_num = b-2; a_num<=b; a_num++) {
				y++;
				count_number +="<a class=\"page_other\" href=\"javascript:goToPage("+a_num+")\">&nbsp;"+a_num+"&nbsp;</a>&nbsp;";
			}
		}
		else{
			for(a_num=1; a_num<=4; a_num++) {
				y++;
				if (a_num == page) {
					count_number +="&nbsp;<span style=\"cursor: default;\" class='page_active'>&nbsp;"+a_num+"&nbsp;</span>&nbsp;";
				}
				else {
					count_number +="<a class=\"page_other\" href=\"javascript:goToPage("+a_num+")\">&nbsp;"+a_num+"&nbsp;</a>&nbsp;";
				}
			}
			count_number +="...";			
			for(a_num=b-3; a_num<=b; a_num++) {
				y++;
				if (a_num == page) {
					count_number +="&nbsp;<span style=\"cursor: default;\" class='page_active'>&nbsp;"+a_num+"&nbsp;</span>&nbsp;";
				}
				else {	
					count_number +="<a class=\"page_other\" href=\"javascript:goToPage("+a_num+")\">&nbsp;"+a_num+"&nbsp;</a>&nbsp;";
				}
			}
		}
		return count_number;
	}
};
function showMusicBox(data){
		if(parent.document.getElementById('music-box')!=null){
				obj=parent.document.getElementById('music-box');
				p=obj.parentNode;
				p.innerHTML='';
		}
			html="<div id=\"music-box'\" class=\"overlay_id1\" style='width:180px'>";
				html+="<div class='pop-header1'>";
				html+="<div class='pop-header-title'>";
				//html+=this.title;
				html+="</div>";
				html+="<div class='pop-header-icon'>";
				html+="<img src='icon/icon_button_close.gif' onclick='alert(\"shit\")'/>";
				html+="</div>"
				html+="</div>";

			html+="	<div class=\"game-box-float\" tabindex=\"1000\">";
			html+="music box";
			html+="</div>";
			html+="</div>";
			/*
			if(this.overlay){
				html+="<div class=\"ui-dialog-overlay\" id=\""+this.ui_dialog_id+"\"/>";
				html+="</div>";
			}
			*/
			var div=parent.document.createElement('div');
			div.innerHTML=html;
			
			parent.document.getElementsByTagName("body")[0].appendChild(div);
		//}else{
		//	document.getElementById(this.overlay_id).style.display='block';
		//	document.getElementById(this.ui_dialog_id).style.display='block';
		//}
	
		var winwidth= parent.document.body.clientWidth;
		var winheight = parent.document.body.clientHeight;
		
		//document.getElementById(this.ui_dialog_id).style.width=this.pwidth+'px';
		//document.getElementById(this.ui_dialog_id).style.height=this.pheight+'px';
		
		gameLayer=parent.document.getElementById('music-box');
		var layerheight = gameLayer.clientHeight;
		var layerwidth = gameLayer.clientWidth;
		gameLayer.style.left =0;//Math.round((winwidth - layerwidth)/2)+170 + "px";
		gameLayer.style.top = 0;//Math.round((winheight - layerheight)/2) + "px";
}
function getPoem(id){
		if(document.getElementById(id).style.display=='none'){
			document.getElementById(id).style.display='block';
		}else{
			document.getElementById(id).style.display='none';
		}
	}
function ShowFullsizePX(id,url){
	/*
	html=document.getElementById(id).innerHTML;
	html=html.substring(html.indexOf("<embed"),html.length-1);
	div="<div style='width:560px;height:315px'></div>";
	document.getElementById('print_fullsize').innerHTML=div;
	ShowDivCenterPage('view_fullsize_table',1);
	document.getElementById(id).style.top=document.getElementById('print_fullsize').style.top;
	document.getElementById(id).style.left=document.getElementById('print_fullsize').style.left;
	document.getElementById(id).style.display="block";
	*/
	var ua = navigator.userAgent.toLowerCase();
	isIE = ((ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1)); 
	if(isIE){
		var topStr = (screen.availHeight/2)-157;
		var leftStr = (screen.availWidth/2)-280;
		win=window.open("index.php?step=photodex_preview&url="+url,"photodex_preview","status=0,toolbar=0,location=0,menubar=0,resizable=1,scrollbars=0,height=315,width=560 top="+topStr+",left="+leftStr);
//		win.moveTo(100,200);
	}else{
		ShowDivCenterPage(id,1);
	}
}
function HideMe(id){
	//showid2(id,"none");
	HideItAll();
	document.getElementById(id).style.display='none';
}
