function externalLinks() {
  if (!document.getElementsByTagName) return;
  var anchors = document.getElementsByTagName("a");
  for (var i=0; i<anchors.length; i++) {
    var anchor = anchors[i];
    if (anchor.getAttribute("href") &&
        anchor.getAttribute("rel") == "external"){
      anchor.target = "_blank";
//      anchor.innerHTML = anchor.innerHTML + " &#xBA;";
    }
    if (anchor.getAttribute("href") &&
        anchor.getAttribute("rel") == "protected"){
      anchor.target = "_blank";
      anchor.innerHTML = anchor.innerHTML + " *";
    }
 }
}


function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}
// Reusable removeEvent function
function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
  } else
    obj.removeEventListener( type, fn, false );
}

function externalform() {
  if (!document.getElementsByTagName) return;
  var anchors = document.getElementsByTagName("form");
  for (var i=0; i<anchors.length; i++) {
    var anchor = anchors[i];
    if (anchor.getAttribute("method") &&
        anchor.getAttribute("rel") == "external"){
      anchor.target = "_blank";
    }
 }
}

addEvent(window,'load',externalLinks);
addEvent(window,'load',externalform);

function submit_search() {
	fcontent=document.getElementById('searchtext').value;
	if(fcontent.length < 4) {
		alert("Please enter full words");
	} else {
		document.searchsite.submit();
	}
}

function title_list(ltr,page) {
	document.getElementById('title').value=ltr;
	document.getElementById('page').value=page;
	return formObj(document.getElementById('titlelist'),'get_org_list');
	return false;
}

function book_topic(topic,page) {
	document.getElementById('topic').value=topic;
	document.getElementById('page').value=page;
	return formObj(document.getElementById('titlelist'),'get_book_topic');
	return false;
}

function formObj(frm, farm) {
var ajax = new sack();
var list=new Array() ;
for (i=0; i<frm.elements.length; i++){

	if(frm.elements[i].type=="checkbox") {
		if(frm.elements[i].checked==true) {
			list[i] = frm.elements[i].name+"="+escape(frm.elements[i].value);
		} 
	} else {
		list[i] = frm.elements[i].name+"="+escape(frm.elements[i].value);
	}
}
	var result1=list.join() //creates the String "Apple,Oranges"
	var result2=list.join("&") //creates the String "Apple*Oranges"
	url ="/linc_inc/ajax.inc.php?type="+farm+"&"+result2;
		ajax.requestFile = url;	// Specifying which file to get
		//alert(url);
		switch(farm) {
			case "title_list":
				ajax.onCompletion = function(){ list_title(ajax.response); }
			break
			case "get_book_topic":
				ajax.onCompletion = function(){ list_title(ajax.response);}
			break
			default: ajax.onCompletion = function(){ list_title(ajax.response); }
		}
		ajax.runAJAX();		// Execute AJAX function
		return false;
}

function list_title(response) {
	document.getElementById('tlist').innerHTML=response;
	return false;
}

function windowpage(obj) {
	displayMessage('../pei_inc/ajax.php?type='+obj);
}

function check_request() {
	var ajax = new sack();
	if(!document.getElementById('name').value) {
		alert("Please enter your name");
		document.getElementById('name').focus();
		return false;
	} else if(!document.getElementById('email').value) {
		alert("Please enter your email");
		document.getElementById('email').focus();
		return false;
	} else {
		var n=document.getElementById('name').value;
		var name = n.replace(/(<([^>]+)>)/ig,""); 
		var e=document.getElementById('email').value;
		var email = e.replace(/(<([^>]+)>)/ig,""); 
		var o=document.getElementById('organization').value;
		var organization = o.replace(/(<([^>]+)>)/ig,""); 
		var a=document.getElementById('address').value;
		var address = a.replace(/(<([^>]+)>)/ig,""); 
		var c=document.getElementById('city').value;
		var city = c.replace(/(<([^>]+)>)/ig,""); 
		var s=document.getElementById('state').value;
		var state = s.replace(/(<([^>]+)>)/ig,""); 
		var z=document.getElementById('zipcode').value;
		var zipcode = z.replace(/(<([^>]+)>)/ig,""); 
		var p=document.getElementById('phone').value;
		var phone = p.replace(/(<([^>]+)>)/ig,""); 
		var cm=document.getElementById('comments').value;
		var comments = cm.replace(/(<([^>]+)>)/ig,""); 
	
		url ="../linc_inc/ajax.inc.php?type=add_org&email="+email+"&name="+name+"&organization="+organization+
		"&address="+address+"&city="+city+"&zipcode="+zipcode+"&state="+state+"&phone="+phone+"&comments="+comments;
		ajax.requestFile = url;	// Specifying which file to get
		ajax.onCompletion = function(){
			if(ajax.response=='ok') {
				window.location='/resources/thank_you';	
			} else {
				alert(ajax.response);
			}
		}
		ajax.runAJAX();		// Execute AJAX function
		return false;
	}
}

function add_org(id) {
	var ajax = new sack();
	url ="../linc_inc/ajax.inc.php?type=pdf_org&org="+id;
	ajax.requestFile = url;	// Specifying which file to get
	ajax.onCompletion = function(){
		var data=ajax.response.split('@');
			alert(data[0]);
			document.getElementById('top-nav').innerHTML=data[1];
		}
	ajax.runAJAX();		// Execute AJAX function
	return false;
}

function remove_resource(id) {
	var ajax = new sack();
	var x=confirm('Are you sure you want to remove this information?');
	if(x) {
		url ="../linc_inc/ajax.inc.php?type=remove_resource&org="+id;
		ajax.requestFile = url;	// Specifying which file to get
		ajax.onCompletion = function(){window.location=ajax.response;}
		ajax.runAJAX();		// Execute AJAX function
	}
	return false;
}

function check_contact(frm) {
	var ajax = new sack();
	var list=new Array() ;
	for (i=0; i<frm.elements.length; i++){
	
		if(frm.elements[i].type=="checkbox") {
			if(frm.elements[i].checked==true) {
				list[i] = frm.elements[i].name+"="+escape(frm.elements[i].value);
			} 
		} else {
			list[i] = frm.elements[i].name+"="+escape(frm.elements[i].value);
		}
	}
	var result1=list.join() //creates the String "Apple,Oranges"
	var result2=list.join("&") //creates the String "Apple*Oranges"
		url ="../linc_inc/ajax.inc.php?type=sub_mailer&"+result2;
		ajax.requestFile = url;	// Specifying which file to get
		ajax.onCompletion = function(){
			var data=ajax.response.split("@");
			if(data[0]==1) {
				alert(data[1]);
			} else {
				reply='<center><h5>Thank you for joining the PFA\'s mailing list.</h5>\r';
				reply+='<p>Your information has been recorded</p>';
				reply+='<a href="javascript:void(0);" onclick="closeMessage();">[Close]</a></center>';
				document.getElementById('f1').innerHTML=reply;
			}
			}
		ajax.runAJAX();		// Execute AJAX function
		return false;
}

function email_syntax(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
		return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}
	if (str.indexOf(at,(lat+1))!=-1){
		return false
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	}
	if (str.indexOf(dot,(lat+2))==-1){
		return false
	}
	if (str.indexOf(" ")!=-1){
		return false
	}
 return true;				
}

function open_mailer() {
	messageObj.setSource('../linc_inc/ajax.inc.php?type=mailer');
	messageObj.setCssClassMessageBox(false);
	messageObj.setSize(450,300);
	messageObj.setShadowDivVisible(true);	// Enable shadow for these boxes
	messageObj.display();
}

/** MODAL MESSAGE **/

DHTML_modalMessage = function(){
	var url;								// url of modal message
	var htmlOfModalMessage;					// html of modal message
	
	var divs_transparentDiv;				// Transparent div covering page content
	var divs_content;						// Modal message div.
	var iframe;								// Iframe used in ie
	var layoutCss;							// Name of css file;
	var width;								// Width of message box
	var height;								// Height of message box
	
	var existingBodyOverFlowStyle;			// Existing body overflow css
	var dynContentObj;						// Reference to dynamic content object
	var cssClassOfMessageBox;				// Alternative css class of message box - in case you want a different appearance on one of them
	var shadowDivVisible;					// Shadow div visible ? 
	var shadowOffset; 						// X and Y offset of shadow(pixels from content box)
	var MSIE;
		
	this.url = '';							// Default url is blank
	this.htmlOfModalMessage = '';			// Default message is blank
	this.layoutCss = '';	// Default CSS file
	this.height = 550;						// Default height of modal message
	this.width = 550;						// Default width of modal message
	this.cssClassOfMessageBox = false;		// Default alternative css class for the message box
	this.shadowDivVisible = true;			// Shadow div is visible by default
	this.shadowOffset = 5;					// Default shadow offset.
	this.MSIE = false;
	if(navigator.userAgent.indexOf('MSIE')>=0) this.MSIE = true;
	

}

DHTML_modalMessage.prototype = {
	setSource : function(urlOfSource){this.url = urlOfSource;},
	setHtmlContent : function(newHtmlContent){
		this.htmlOfModalMessage = newHtmlContent;
	},
	setSize : function(width,height)	{
		if(width)this.width = width;
		if(height)this.height = height;		
	},		
	setCssClassMessageBox : function(newCssClass){
		this.cssClassOfMessageBox = newCssClass;
		if(this.divs_content){
			if(this.cssClassOfMessageBox)
				this.divs_content.className=this.cssClassOfMessageBox;
			else
				this.divs_content.className='modalDialog_contentDiv';	
		}
					
	},	
	setShadowOffset : function(newShadowOffset){this.shadowOffset = newShadowOffset},	
	display : function()	{
		if(!this.divs_transparentDiv){
			this.__createDivs();
		}	
		this.divs_transparentDiv.style.display='block';
		this.divs_content.style.display='block';
		this.divs_shadow.style.display='block';		
		if(this.MSIE)this.iframe.style.display='block';	
		this.__resizeDivs();
		window.refToThisModalBoxObj = this;		
		setTimeout('window.refToThisModalBoxObj.__resizeDivs()',150);
		this.__insertContent();	},
		
	setShadowDivVisible : function(visible){this.shadowDivVisible = visible;},
	close : function(){
		/* Hiding divs */
		this.divs_transparentDiv.style.display='none';
		this.divs_content.style.display='none';
		this.divs_shadow.style.display='none';
		if(this.MSIE)this.iframe.style.display='none';},
		
	addEvent : function(whichObject,eventType,functionName,suffix){ 
	  if(!suffix)suffix = '';
	  if(whichObject.attachEvent){ 
	    whichObject['e'+eventType+functionName+suffix] = functionName; 
	    whichObject[eventType+functionName+suffix] = function(){whichObject['e'+eventType+functionName+suffix]( window.event );} 
	    whichObject.attachEvent( 'on'+eventType, whichObject[eventType+functionName+suffix] ); 
	  } else 
	    whichObject.addEventListener(eventType,functionName,false); 	    
	},
	__createDivs : function(){
		// Creating transparent div
		this.divs_transparentDiv = document.createElement('DIV');
		this.divs_transparentDiv.className='modalDialog_transparentDivs';
		this.divs_transparentDiv.style.left = '0px';
		this.divs_transparentDiv.style.top = '0px';
		
		document.body.appendChild(this.divs_transparentDiv);
		// Creating content div
		this.divs_content = document.createElement('DIV');
		this.divs_content.className = 'modalDialog_contentDiv';
		this.divs_content.id = 'DHTMLSuite_modalBox_contentDiv';
		this.divs_content.style.zIndex = 100000;
		
		if(this.MSIE){
			this.iframe = document.createElement('<IFRAME src="about:blank" frameborder=0>');

			this.iframe.style.zIndex = 90000;
			this.iframe.style.position = 'absolute';
			document.body.appendChild(this.iframe);	
		}
			
		document.body.appendChild(this.divs_content);
		// Creating shadow div
		this.divs_shadow = document.createElement('DIV');
		this.divs_shadow.className = 'modalDialog_contentDiv_shadow';
		this.divs_shadow.style.zIndex = 95000;
		document.body.appendChild(this.divs_shadow);
		window.refToModMessage = this;
		this.addEvent(window,'scroll',function(e){ window.refToModMessage.__repositionTransparentDiv() });
		this.addEvent(window,'resize',function(e){ window.refToModMessage.__repositionTransparentDiv() });
		

	},
	__getBrowserSize : function(){
    	var bodyWidth = document.documentElement.clientWidth;
    	var bodyHeight = document.documentElement.clientHeight;
    	
		var bodyWidth, bodyHeight; 
		if (self.innerHeight){ // all except Explorer 
		 
		   bodyWidth = self.innerWidth; 
		   bodyHeight = self.innerHeight; 
		}  else if (document.documentElement && document.documentElement.clientHeight) {
		   // Explorer 6 Strict Mode 		 
		   bodyWidth = document.documentElement.clientWidth; 
		   bodyHeight = document.documentElement.clientHeight; 
		} else if (document.body) {// other Explorers 		 
		   bodyWidth = document.body.clientWidth; 
		   bodyHeight = document.body.clientHeight; 
		} 
		return [bodyWidth,bodyHeight];		
		
	},
    __resizeDivs : function(){
    	
    	var topOffset = Math.max(document.body.scrollTop,document.documentElement.scrollTop);

		if(this.cssClassOfMessageBox)
			this.divs_content.className=this.cssClassOfMessageBox;
		else
			this.divs_content.className='modalDialog_contentDiv';	
			    	
    	if(!this.divs_transparentDiv)return;
    	
    	// Preserve scroll position
    	var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
    	var sl = Math.max(document.body.scrollLeft,document.documentElement.scrollLeft);
    	
    	window.scrollTo(sl,st);
    	setTimeout('window.scrollTo(' + sl + ',' + st + ');',10);

    	this.__repositionTransparentDiv();
    	

		var brSize = this.__getBrowserSize();
		var bodyWidth = brSize[0];
		var bodyHeight = brSize[1];
    	
    	// Setting width and height of content div
      	this.divs_content.style.width = this.width + 'px';
    	this.divs_content.style.height= this.height + 'px';  	
    	
    	// Creating temporary width variables since the actual width of the content div could be larger than this.width and this.height(i.e. padding and border)
    	var tmpWidth = this.divs_content.offsetWidth;	
    	var tmpHeight = this.divs_content.offsetHeight;
    	
    	this.divs_content.style.left = Math.ceil((bodyWidth - tmpWidth) / 2) + 'px';;
    	this.divs_content.style.top = (Math.ceil((bodyHeight - tmpHeight) / 2) +  topOffset) + 'px';
    	
 		if(this.MSIE){
 			this.iframe.style.left = this.divs_content.style.left;
 			this.iframe.style.top = this.divs_content.style.top;
 			this.iframe.style.width = this.divs_content.style.width;
 			this.iframe.style.height = this.divs_content.style.height;
 		}
 		
    	this.divs_shadow.style.left = (this.divs_content.style.left.replace('px','')/1 + this.shadowOffset) + 'px';
    	this.divs_shadow.style.top = (this.divs_content.style.top.replace('px','')/1 + this.shadowOffset) + 'px';
    	this.divs_shadow.style.height = tmpHeight + 'px';
    	this.divs_shadow.style.width = tmpWidth + 'px';
    	
    	if(!this.shadowDivVisible)this.divs_shadow.style.display='none';},
		
    __repositionTransparentDiv : function(){
    	this.divs_transparentDiv.style.top = Math.max(document.body.scrollTop,document.documentElement.scrollTop) + 'px';
    	this.divs_transparentDiv.style.left = Math.max(document.body.scrollLeft,document.documentElement.scrollLeft) + 'px';
		var brSize = this.__getBrowserSize();
		var bodyWidth = brSize[0];
		var bodyHeight = brSize[1];
    	this.divs_transparentDiv.style.width = bodyWidth + 'px';
    	this.divs_transparentDiv.style.height = bodyHeight + 'px';},
		
    __insertContent : function(){
		if(this.url){	// url specified - load content dynamically
			ajax_loadContent('DHTMLSuite_modalBox_contentDiv',this.url);
		}else{	// no url set, put static content inside the message box
			this.divs_content.innerHTML = this.htmlOfModalMessage;	
		}
    }		
}
messageObj = new DHTML_modalMessage();	// We only create one object of this class
messageObj.setShadowOffset(5);	// Large shadow


function displayMessage(url)
{
	
	messageObj.setSource(url);
	messageObj.setCssClassMessageBox(false);
	messageObj.setSize(450,300);
	messageObj.setShadowDivVisible(true);	// Enable shadow for these boxes
	messageObj.display();
}

function displayStaticMessage(messageContent,cssClass)
{
	messageObj.setHtmlContent(messageContent);
	messageObj.setSize(500,500);
	messageObj.setCssClassMessageBox(cssClass);
	messageObj.setSource(false);	// no html source since we want to use a static message here.
	messageObj.setShadowDivVisible(false);	// Disable shadow for these boxes	
	messageObj.display();
	
	
}

function closeMessage()
{
	messageObj.close();	
}

