$E = document.getElement.bind(document);

window.addEvent('domready', function() {	
	
	if ($('emailInput') != null) $('emailInput').clearFocusResetBlur('value');  
	
	//Age Verify
	if ($('verify') !=null) {
		addOption_list();
		var d = new Date();
		var li = $('writeYear');
		new FormCheck('avform');
	}
		
	//Sign Up 
	if ($('home') !=null) new FormCheck('signUp');
	//Set up accordion
	if ($('accordion') != null) {
		//create our Accordion instance
		var myAccordion = new Accordion($('accordion'), 'h3.toggler', 'ul.element', {
			display: -1,
			opacity: false,
			onActive: function(toggler, element){
				toggler.setStyle('background-position', '0 -25px');
			},
			onBackground: function(toggler, element){
				toggler.setStyle('background-position', '0 0');
			}
		});
	};
	
	//Build popup links
	if ($$('.pop') != null) {
		//console.info('inside');
		$$('.pop').each(function(element) {
			var popLinks = $$('.pop')
			if (!popLinks) return false;	
					popLinks.each(function(link,i){					
					link.addEvent('click',function(e){
						e.preventDefault();
						popUp(link);
					});
			 });			
		});
	}
	
	//Contact us ini
	if ($('contactUs') != null) {
		//console.info($('contactUs'));
		new FormCheck('contactForm1');
		new FormCheck('contactForm2');
		
		$$('.tips').each(function(element,index) {
			var content = element.get('title').split('::');
			element.store('tip:title', content[0]);
			element.store('tip:text', content[1]);
		});
		var myTips = new Tips('.tips', {
			className: 'tipz'
		});		
	}

});

function popUp(url){
	window.open(url,"popup","toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,height=525,width=510");
}

function customCheck(el){
	if ($('first-number').value + $('second-number').value != el.value) {
		el.errors.push("The entered value doesn't match the sum of first and second input");
		return false;
	}
	return true;
}


// Generic Random Number for NoCache vars //
function rand ( n ){
  return ( Math.floor ( Math.random ( ) * n + 1 ) );
}

/* Auto Tab Functionality */
var autoTab = {
	init:function(){
		var form = document.getElementById("avform");
		autoTab.fields = form.getElementsByTagName("input");
		if (!autoTab.fields) return false;
		autoTab.fields[0].focus();
		autoTab.fields[0].select();
		autoTab.fields[0].style.cursor='text';
		for (var i = 0;i < autoTab.fields.length;i++){
			autoTab.fields[i].onkeyup = function(e){
				if (this.value.length == this.getAttribute("maxlength")) {
					
					if(!window.event){
						this.myKeyCode = e.keyCode;
					}else{
						this.myKeyCode =window.event.keyCode;
					}
					if(this.myKeyCode!=9 && this.myKeyCode!=16){
					autoTab.goToNextField(this);
					}
				
				};
			}
		}
	},
	goToNextField:function(field){
		var tabindex = field.getAttribute("tabindex");
		autoTab.fields[tabindex].focus();
		autoTab.fields[tabindex].select();
		autoTab.fields[tabindex].style.cursor='text';
		autoTab.fields[tabindex].setAttribute("value","");
	}
}
function inputTxtReplace() {
	var input = $('emailInput');
	var current_val = input.value;
	input.onlblur() = function(){
		if(input.value=='')this.value='enter email address'
	}
}

Element.implement({
    clearFocusResetBlur: function(attr){
        var valueString = this.get(attr);
		this.addEvents({
            'focus': function(){
                if( this.get('value') == valueString ) this.set('value','');
            },
            'blur': function(){
                if( this.get('value') == "" ) this.set('value',valueString);
            }
        });
    }

});

//Populate the drop downs
 function addOption(selectbox,text,value ){
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    selectbox.options.add(optn);
};

function addOption_list(selectbox){
	var d = new Date();
	var curYear = d.getFullYear()
	var negYears = curYear - 101;
	
    for (var i = 1; i < 32; i++) {   
        addOption(document.avform.ageDD,i,i); 
    };	
	for (var j = curYear ; j > negYears; j--) {   
	    addOption(document.avform.ageYYYY,j,j);
	};
	
	// Set Defaults to Current Day
	
	document.avform.ageYYYY.selectedIndex = 1;
	document.avform.ageDD.selectedIndex = d.getDate();
	document.avform.ageMM.selectedIndex = d.getMonth()+1;
	
};

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 