var popupwin;

var PopUp = {
	defaults:{width:700,height:600,scrollbars:1,winName:'popupwin'},
	open:function(obj) {
		var opts = [];
		url = obj.url;
		winName = obj.name || PopUp.defaults.winName;
		delete obj.url,obj.winName;
		for( i in PopUp.defaults ) {
			opts.push(""+i+"="+PopUp.defaults[i] );
		}
		for( i in obj ) {
			opts.push(""+i+"="+obj[i] );
		}
		options = opts.join(",");
		popupwin = window.open(url,winName,options);
	}

}

// Field Validator Functions
var validators = {
		text:function(n){ 		return !/^\s*$/g.test(n.value) },
		telephone:function(n){ 	return /^\s*[\d]{2,8}\s*(([\d]{2,})\s*)+\s*$/.test(n.value) },
		email:function(n){ 		return /^[A-Za-z0-9._%-]{3,}@[A-Za-z0-9.-]{3,}\.[A-Za-z]{2,6}$/.test(n.value)},
		number:function(n) { 	return /^\s*\d+\s*$/.test(n.value) },
		noblank:function(n) { 	return !/^\s*$/.test(n.value) },
		checked:function(n) {	return n.checked==true;	}
}
// Form validator
var Validate = function(){}
Validate.prototype = {

	fields:{},
	alerts:[],
	valid:true,
	attempt:function(formName)
	{
		formName = formName || document.forms[0];
		var me = this;
		//alert(typeof(formName));
		var f = typeof(formName) == 'string' ? document.forms[formName] : formName;
		var elements = f.elements;
		//alert(elements.length)
		for( var a = 0 ; a < elements.length; a++ ) {

			if( elements[a].type != 'submit'  ) {
				//alert("current:"+elements[a].type + " " + elements[a].id);
				var el = elements[a];

				try {
					if( this.fields[el.id] ) {
							if( !validators[this.fields[el.id][0]](el) ) {
								//alert(el.id+ " " + validators[this.fields[el.id][0]](el));
								this.valid = false;
								this.hint(el,true);
								if ( this.fields[el.id][1] != '') this.alerts.push(this.fields[el.id][1]);
							} else {
								this.hint(el,false);
							}


					}
				} catch(e) {
					//alert('error:'+e);
					continue;
				}

			}
		}

		return this.valid;
	},
	alertString:function(){
		return (this.alerts.length>0?'\n '+this.alerts.join('\n '):'')
	},
	hint:function(el,state){
		if( state == true ) {
			if( !/hint/.test(el.className) ) {
				el.className += ' hint';
			}
		}
		else {
			el.className = el.className.replace(/hint/,'');
		}
	}
}


function onSubmitEnter(){
	var f = new Validate();
	f.fields = {
		accept:['checked','']
	}
	if( !f.attempt('enter') )
		alert('Please accept the terms & conditions');
	else {
		Element.hide('enter-container');
		new Effect.Appear('competition-container');
	}

	return false;
}

// Form specific validators
function onSubmitCompetition(){

	var f = new Validate();
	f.fields = {
		name:['text',''],
		company_name:['text',''],
		address:['text',''],
		phone:['telephone',''],
		email:['email',''],
		code:['number','']
	}

	if( !f.attempt('competition') )
		alert('Please correct the relevant fields' + (f.alerts.length>0?'\n '+f.alerts.join('\n '):'') );
	else {
		var data = Form.serialize('competition');
		Element.update('competition-container','<img src="/images/loader_bar.gif" />')
		var ajax = new Ajax.Updater('competition-container','/competition/enter/',
									{
										method:'post',
										parameters:data,
										evalScripts:true

									}
									);

	}
	return false;
}


var Competition = {
	winner:function(){
		$('winner-flash').show();
		$('competition-image').hide();
	},
	loser:function(){
		$('winner-flash').hide();
		$('competition-image').show();
	}
}