function stripedTable(){
	if (document.getElementById && document.getElementsByTagName) {
		var allTables = document.getElementsByTagName('table');
		if (!allTables) { return; }

		for (var i = 0; i < allTables.length; i++) {
			if (allTables[i].className.match(/[\w\s ]*scrollTable[\w\s ]*/)) {
				var trs = allTables[i].getElementsByTagName("tr");
				for (var j = 0; j < trs.length; j++) {
					removeClassName(trs[j], 'alternateRow');
					addCSSClass(trs[j], 'normalRow');
				}
				for (var k = 0; k < trs.length; k += 2) {
					removeClassName(trs[k], 'normalRow');
					addCSSClass(trs[k], 'alternateRow');
				}
			}
		}
	}
}
function removeClassName(elem, className){
	elem.className = elem.className.replace(className, "");
}

function addCSSClass(elem, className){
	removeClassName(elem, className);
	elem.className = (elem.className + " " + className);
}

$(function(){
	$(".captcha").hide();
});

var theForm;
var theFormID;
var prefix;
function checkform(theForm){
// Test if DOM is available and there is an element called required
	if(!document.getElementById || !document.createTextNode){return;}
	if(!theForm.required){return;}

	theFormID	= $(theForm).attr("id");
	prefix		= theFormID.split("_")[1] != "" ? "_"+theFormID.split("_")[1] : "";

// Define error messages and split the required fields
	var errExists = false;
	var errorID='errormsg';
	var errorClass='error'
	var errorMsg='Please enter or fix the fields marked with ';
	var errorImg='http://users.mediastove.com/icns/alert.gif';
	var errorAlt='Error';
	var errorTitle='This field has an error!';

	var reqfields=theForm.required.value.split(',');
// Cleanup old mess
	// if there is an old errormessage field, delete it
	if(document.getElementById(errorID))
	{
		var em=document.getElementById(errorID);
		em.parentNode.removeChild(em);
	}
	// remove old images and classes from the required fields
	for(var i=0;i<reqfields.length;i++)
	{
		var f = eval("theForm."+reqfields[i]);
		if(!f){continue;}
		if(f.previousSibling && /img/i.test(f.previousSibling.nodeName))
		{
			f.parentNode.removeChild(f.previousSibling);
		}
		f.className='';
	}
// loop over required fields
	for(var i=0;i<reqfields.length;i++)
	{
// check if required field is there
		var f = eval("theForm."+reqfields[i]);
		if(!f){continue;}
// test if the required field has an error,
// according to its type
		switch(f.type.toLowerCase())
		{
			case 'text':
				if(f.value=='' && f.id!='email'){cf_adderr(f)}
// email is a special field and needs checking
				if(f.id=='email' && !cf_isEmailAddr(f.value)){cf_adderr(f)}
			break;
			case 'textarea':
				if(f.value==''){cf_adderr(f)}
			break;
			case 'checkbox':
				if(!f.checked){cf_adderr(f)}
			break;
			case 'select-one':
				if(!f.selectedIndex && f.selectedIndex==0){cf_adderr(f)}
			break;
		}
	}
	if(!errExists){
		sendForm(prefix);
	}
	return false;

	/* Tool methods */
	function cf_adderr(o)
	{
		// create image, add to and colourise the error fields
		var errorIndicator=document.createElement('img');
		errorIndicator.alt=errorAlt;
		errorIndicator.src=errorImg;
		errorIndicator.title=errorTitle;
		o.className=errorClass;
		o.parentNode.insertBefore(errorIndicator,o);

	// Check if there is no error message
		if(!document.getElementById(errorID))
		{
		// create errormessage and insert before submit button
			var em=document.createElement('div');
			em.id=errorID;
			var newp=document.createElement('p');
			newp.appendChild(document.createTextNode(errorMsg))
			// clone and insert the error image
			newp.appendChild(errorIndicator.cloneNode(true));
			em.appendChild(newp);
			// find the submit button
			for(var i=0;i<theForm.getElementsByTagName('input').length;i++)
			{
				if(/submit/i.test(theForm.getElementsByTagName('input')[i].type))
				{
					var sb=theForm.getElementsByTagName('input')[i];
					break;
				}
			}
			if(sb)
			{
				sb.parentNode.insertBefore(em,sb);
			}
			errExists = true;
		}
	}
	function cf_isEmailAddr(str)
	{
		return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
	}
}

function sendForm(prefix){
	if($("#myForm"+prefix).find("input[@name=more_info1]").val()==""){
		var randVal = Math.random(1000000);
		var formData = $("#myForm"+prefix).fastSerialize();
		formData.push({name: "btnSend", value: "Send"});
		formData.push({name: "pageMenu", value: document.URL.split("/")[3]});
		formData.push({name: "p", value: document.URL.split("/")[4]});
		formData.push({name: "isAjax", value: 1});
		formData.push({name: "randVal", value: randVal});
		$("#btnSend"+prefix).attr("disabled","disabled");
		$.post(
			"/index.php",
			formData,
			function(txt){
				if(txt!="ERROR"){
					$('#form_holder'+prefix).html(txt);
					$('#form_holder'+prefix).show();
					var name_value = $('#name_value').html();
					if(name_value!=""){
						$('#form_holder'+prefix).hide();
						var amount_value = $('#amount_value').html();
						var evTitle_value = $('#evTitle_value').html().replace(/\\'/,"'");
						var paypal_holder = $('#paypal_holder').html();
						paypal_holder = paypal_holder.replace(/\[name\]/,name_value);
						$('#paypal_holder').html(paypal_holder);
						$('#item_name').val(evTitle_value);
						$('#amount').val(amount_value);
						$('#paypal_holder').show();
					}
					$('#form_container'+prefix).hide();
				}else{
					$('#reqOptions').background('#FFCCCC');
					if($('#errormsg')==null){
						$("#myForm"+prefix).after('<div id="errormsg"></div>');
					}
					$('#errormsg').html('Please choose an option.');
				}
				$("#btnSend"+prefix).attr("disabled","");
				txt = null;
			}
		)
		return false;
	}
}
var ScrollLinks = {
    currentHash: false,
    start: function(){
        this.scroll = new fx.Scroll({duration: 800, onComplete: function(){ScrollLinks.end();}});
        this.allinks = document.getElementsByTagName('a');
        for (i=0; i<this.allinks.length; i++){
            var lnk = this.allinks[i];
            if ((lnk.href && lnk.href.indexOf('#') != -1) && ( (lnk.pathname == location.pathname) || ('/'+lnk.pathname == location.pathname) ) && (lnk.search == location.search)) {
                lnk.onclick = function(){
                    ScrollLinks.scroll.clearTimer();
                    this.initialHref = this.href;
                    this.initialHash = this.hash;
                    this.href = "javascript:void(0)";
                    setTimeout(function(){this.href = this.initialHref;}.bind(this), 200);
                    ScrollLinks.click(this);
                }
            }
        }
    },

    click: function(link){
        this.currentHash = link.initialHash.substr(1);
        if (this.currentHash) {
            for (j=0; j<this.allinks.length; j++){
                if (this.allinks[j].id == this.currentHash){
                    if (!window.opera) this.scroll.scrollTo(this.allinks[j]);
                    else this.scroll.scrollTo(this.allinks[j].parentNode);
                    break;
                }
            }
        }
    },

    end: function(){
        window.location.href = "#"+this.currentHash;
        this.currentHash = false;
    }
}

window.onload = function() {
	stripedTable();
	if(typeof(fx)!="undefined"){
		ScrollLinks.start();
	}
}