// JavaScript Document
function newsletterSignup() {
    email = document.getElementById('newsletter_email').value;
    if(email!='') {
        ajax_sendAndLoad('actions/newsletter_signup.php', 'email='+email, null, null);    
    }
}

// Generic AJAX and other functions

function getContainer(id) {
    return document.getElementById(id);
}

function ajax_sendAndLoad(link, query, parentElement, placeholder) {
    Ajax_fields = newAjax()
    if (Ajax_fields==null) {
        return
    } 
    var url=link        
    url=url+"?"+query
    url=url+"&sid="+Math.random()            
    Ajax_fields.onreadystatechange=onStateChanged 
    Ajax_fields.open("GET",url,true)
    Ajax_fields.send(null) 
    
    if(parentElement!==null) {
        obj = getContainer(parentElement);
        pos = findPos(obj);    
    }
     if(placeholder!=null)  { 
    	container = getContainer(placeholder);    
    	moveContainer(container, (pos[1]+15), pos[0]);
    }
}

function onStateChanged() {  
    if (Ajax_fields.readyState==4 || Ajax_fields.readyState=="complete") {        
        response = Ajax_fields.responseText;
        
        container_id = response.substring(0, response.indexOf(':'));
        if(container_id.indexOf('\n')>-1)
            container_id = container_id.substring(1, container_id.length);
        
        container = getContainer(container_id);        
        response = response.substr(response.indexOf(':')+1);
        
        hide = response.substring(0, response.indexOf(':'));
        response = response.substr(response.indexOf(':')+1);
        
        toHide = response.substring(0, response.indexOf(':'));
        
        if(document.getElementById(toHide)!=null) {
            if(hide == "hide")
                document.getElementById(toHide).style.display = "none";
            else
                document.getElementById(toHide).style.display = "block";
        }           
        
        response = response.substr(response.indexOf(':')+1);
        
        closeIt = '';
        
        /*
        if(getContainer(container_id).style.position=="absolute")
            closeIt = "<br /><hr /><a href='#' onclick=\"getContainer('"+container_id+"').style.display='none'\" >close</a>";
        */
                
        container.innerHTML = response+closeIt;
    }
} 


function newAjax(handler) { 
    var XMLHttp=null
    try {
        XMLHttp=new ActiveXObject("Msxml2.XMLHTTP")
    } catch(e) {
        try {
            XMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
        } catch (e) {}
    }
    if (XMLHttp==null) {
        XMLHttp=new XMLHttpRequest()
    }
    return XMLHttp
}

function findPos(obj) {
	var curleft = curtop = 0;
	if(obj!==null) {
	   if (obj.offsetParent) {
	   	curleft = obj.offsetLeft
	   	curtop = obj.offsetTop
	   	while (obj = obj.offsetParent) {
	   		curleft += obj.offsetLeft
	   		curtop += obj.offsetTop
	   	}
	   }
	}
	if(curleft==0 && curleft==0) {
	   if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        curleft = window.innerWidth/2;
        curtop = window.innerHeight/2;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
            //IE 6+ in 'standards compliant mode'
            curleft = document.documentElement.clientWidth/2;
            curtop = document.documentElement.clientHeight/2;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
            //IE 4 compatible
            curleft = document.body.clientWidth/2;
            curtop = document.body.clientHeight/2;
        }
        curleft-=100;
        curtop-=50;
	}
	return [curleft,curtop];
}

function moveContainer(obj, top, left) {
    obj.style.top = top+'px'
    obj.style.left = left+'px'
}
