﻿/**
 * RSS feed viewing
 */

var formObj
var indicator
var success
function submitForm ( form ) {
	formObj		= form
	formObj.email.focus();

	indicator	= getChildByAttribute(form.parentNode, 'id', 'indicator')
	formObj.style.display	= 'none'
	indicator.style.display	= 'block'

	url		= form.action
	params	= 'email='+form.email.value+'&name='+form.name.value+'&message='+form.message.value
	var myAjax = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: params,
				onComplete: parseResponse
			}
	);
}

function parseResponse ( response ) {
	rStat	= (response.responseText == 'OK') ?'success' :'failure'

	formObj.email.setAttribute("id", '');
	formObj.name.setAttribute("id", '');
	formObj.message.setAttribute("id", '');

	success	= getChildByAttribute(indicator.parentNode, 'id', rStat)
	indicator.style.display	= 'none'
	success.style.display	= 'block'

	if(rStat=='success') {
		window.setTimeout("fade(success.parentNode.parentNode.parentNode, 100)", 1000)
	} else {
		handleError(formObj, response.responseXML)
		window.setTimeout("success.style.display='none';formObj.style.display='block'", 2000)
	}
}

function handleError ( o, xml ) {
	if(!xml) return false
	data	= getChildByNodeName(xml.firstChild, 'data')
	arr		= getChildByNodeName(data, 'array')

	for(i=0; i<arr.childNodes.length; i++) {
		n	= arr.childNodes[i]
		if(n.nodeType!=1) continue
		name	= n.firstChild.nodeValue
		o[name].setAttribute("id", "error")
	}
	return true
}

var fadeObj
function fade ( o, alpha ) {
	fadeObj	= o
	if(!setAlpha(fadeObj, alpha-=20)) {
		fadeObj.style.visibility	= 'hidden'
		setAlpha(fadeObj, 100)
		success.style.display	= 'none'
		formObj.style.display	= 'block'
		return true
	}
	window.setTimeout("fade(fadeObj, " + alpha + ")", 100)
}

function setAlpha ( o, alpha ) {
	if(alpha < 0 || alpha > 100) return false
	if(typeof o.style.filters != 'undefined') o.style.filters.alpha.opacity	= alpha
	if(typeof o.style.MozOpacity != 'undefined') o.style.MozOpacity	= alpha/100
	if(typeof o.style.Opacity != 'undefined') o.style.Opacity	= alpha/100
	return true
}

function getChildByAttribute ( o, a, v ) {
	if(!o.hasChildNodes()) return false
	for(i=0; i<o.childNodes.length; i++) {
		c	= o.childNodes[i]
		if(c.nodeType!=1) continue
		if(c.getAttribute(a) == v) return c
	}
	return false
}

function getChildByNodeName ( o, t ) {
	if(!o.hasChildNodes()) return false
	for(i=0; i<o.childNodes.length; i++) {
		c	= o.childNodes[i]
		if(c.nodeType!=1) continue
		if(c.nodeName.toLowerCase() == t.toLowerCase()) return c
	}
	return false
}

