function setListeners(){
	inputList = document.getElementByTagName("INPUT");
	for(i=0;i<inputList.length;i++){
		inputList[i].attachEvent("onpropertychange", restoreStyles);
		inputList[i].style.backgroundColor = "";
	}
	selectList = document.getElementByTagName("SELECT");
	for(i=0;i<selectList.length;i++){
		selectList[i].attachEvent("onpropertychange", restoreStyles);
		selectList[i].style.backgroundColor = "";
	}
}
function restoreStyles(){
	if(event.srcElement.style.background != ""){
		event.srcElement.style.background = "";
	}
}
if(window.attachEvent){
	window.attachEvent("onload", setListeners);
}

//Browser Detection
function browseDetect(){
	var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
	if(isInternetExplorer == false){
		cn = 'notIE';
	}else{
		cn = 'IE';
	}
	return cn;
}
browser = browseDetect();

//Common Form Validation
function validate(form){
	r = requiredFields(form) ? true : false;
	if(r == false){ return r;}
	r = confirmFields(form) ? r :false;
	return r;
}
function requiredFields(form){
	for(i=0;i<form.elements.length;i++){
		if((form.elements[i].className.indexOf('req') != -1) && (form.elements[i].value == "")){
			alert(form.elements[i].title + " is required");
			form.elements[i].focus();
			return false;
		}
	}
	return true;
}
function confirmFields(form){
	for(i=0;i<form.elements.length;i++){
		if(form.elements[i].name.indexOf('confirm_') != -1 && form.elements[i].value != form.elements[form.elements[i].name.substr(8)].value){
			alert(form.elements[form.elements[i].name.substr(8)].title + " do not match!");
			return false;
		}
	}
	return true;
}
function data_check(field){
}

//Delete Confirmation Check
function confirmDelete(title, page){
	var con = confirm('Are you sure you want to delete this '+title+', this action can NOT be undone!');
	if(con){
		window.location = page;
	}
}

//Clear/Restore Form Field Values
function clearDefault(el){
	if(el.defaultValue == el.value) el.value = "";
}
function restoreDefault(el){
	if(el.value == "") el.value = el.defaultValue;
}

//Accordian Script for Admin Pages
accordian = function(id, speed){
	this.obj = document.getElementById(id);
	this.previous = null;
	this.current = null;
	this.links = this.obj.childNodes;
	speed ? this.speed =  speed : this.speed = 5;
	me = this;
	for(i=0;i<this.links.length;i++){
		if(this.links[i].firstChild){
			if(this.links[i].getElementsByTagName('ul')[0]){
				this.links[i].firstChild.position = i;
				this.links[i].firstChild.onclick = function(){
					me.check(this);
				}
			}
		}
	}
	this.date = new Date();
	this.date.setYear(this.date.getFullYear()+1)
	this.expires = this.date.toGMTString();
	if(document.cookie){
		current = null;
		cookie = document.cookie;
		cValues = cookie.split(';');
		for(i=0;i<cValues.length;i++){
			vPrev = cValues[i].search('previous');	
			vCurr = cValues[i].search('current');
			if(vPrev != -1){
				previous = cValues[i].split('=')[1];
			}
			if(vCurr != -1){
				current = cValues[i].split('=')[1];
			}
		}		
		if(!current){
			document.cookie = "current=1; expires="+this.expires+";path=/";
			this.check(this.links[1].firstChild);
		}else{
			this.check(this.links[current].firstChild);
		}
	}
}
accordian.prototype = {
	check:function(t){
		if(me.current != t){		
			if(me.current != null){				
				me.previous = me.current;
				tarList2 = me.previous.parentNode.getElementsByTagName('ul')[0];
				if(browser == "IE"){				//fix easing issue
					tarList2.interval2 = setInterval('me.close(tarList2)',20);		
				}else{
					tarList2.interval2 = setInterval('me.close(tarList2)',30);	
				}
				document.cookie = "previous="+me.previous.position+"; expires="+me.expires+";path=/";		
			}						
			me.current = t;
			document.cookie = "current="+me.current.position+"; expires="+me.expires+";path=/";
			tarList = t.parentNode.getElementsByTagName('ul')[0];
			tarList.interval = setInterval("me.open(tarList)",20);			
		}
	},
	open:function(t){
		me.closeAll();
		targetH = t.scrollHeight;
		t.style.height ? currentH = parseInt(t.style.height) :	currentH = 0;
		i = Math.ceil((targetH - currentH)/me.speed) + currentH;
		t.style.height = i + 'px';
		if(i>=targetH){
			clearInterval(t.interval);			
		}
	},
	close:function(t){
		clearInterval(me.previous.parentNode.getElementsByTagName('ul')[0].interval);
		t.style.height ? currentH = parseInt(t.style.height) :	currentH = 0;
		i = Math.floor((0-currentH)/(me.speed-2))+currentH;
		t.style.height = i + 'px';
		if(i<=0){
			clearInterval(t.interval2);	
		}
	},
	closeAll:function(){
		for(i=0;i<this.links.length;i++){
			if((this.links[i].firstChild) && (this.links[i].firstChild != me.current && this.links[i].firstChild != me.previous)){
				if(this.links[i].getElementsByTagName('ul')[0]){
					this.links[i].getElementsByTagName('ul')[0].style.height = 0;
				}
			}
		}
	}
}

function addPollOption()
{
	var options_table = document.getElementById('option_table').getElementsByTagName('tbody')[0];
	var children = options_table.getElementsByTagName('tr');
	var opt = document.createElement('tr');
	var td1 = document.createElement('td');
	var td2 = document.createElement('td');
	var input = navigator.appName.indexOf("Microsoft") == -1 ? document.createElement('input') : document.createElement('<input name="option[]"/>');
	td1.innerHTML = (children.length + 1) + ".";
	input.type = 'text';
	input.name = 'upload[]';
	input.size = 30;
	input.maxlength = 100;
	td2.appendChild(input);
	opt.appendChild(td1);
	opt.appendChild(td2);
	options_table.appendChild(opt);
}
function addPic()
{
	var options_table = document.getElementById('option_table').getElementsByTagName('tbody')[0];
	var children = options_table.getElementsByTagName('tr');
	var opt = document.createElement('tr');
	var td1 = document.createElement('td');
	var td2 = document.createElement('td');
	var input = navigator.appName.indexOf("Microsoft") == -1 ? document.createElement('input') : document.createElement('<input name="upload[]"/>');
	td1.innerHTML = (children.length + 1) + ".";
	input.type = 'file';
	input.name = 'upload[]';
	input.size = 30;
	td2.appendChild(input);
	opt.appendChild(td1);
	opt.appendChild(td2);
	options_table.appendChild(opt);
}


