/* functions for rollovers, form validations and ajax calls */

//VALIDATION
function validEmail(email) {
			var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
			return re.test(email); 
		}
		
//VALIDATION
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
		
//VALIDATION
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
		
//VALIDATION
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}
		
//VALIDATION
function isFilled(elm) {
	//alert(elm.type);
	//Last Updated by Sol - 6/14/07
	switch(elm.type){
	case "text":
		if (trim(elm.value) == "" || elm.value == null){
		return false;}
		else {return true;}
		break;
	case "password":
		if (trim(elm.value) == "" || elm.value == null){
		return false;}
		else {return true;}
		break;
	case "textarea":
		if (trim(elm.value) == "" || elm.value == null){
		return false;}
		else {return true;}
		break;
	case "select-one":
		var myindex=elm.selectedIndex;
		if (myindex==0){
		return false;}
		//document.getElementById('numberofplayers1').style.display = 'block';
		else {return true;}
		break;
	}
}

//VALIDATION
function validateform(formname,color1,color2){
	//last updated on 9/27/07
	//index of is mo betta
	var x = document.forms[formname];
	var allgood = true;
	for (var i=0;i<x.length;i++){
		if (x.elements[i].className.indexOf('required')>-1) {
		 	var thisformname = x.elements[i].name;
			var thisformid = x.elements[i].id;
			if (!isFilled(x.elements[i])) {
				document.getElementById(thisformid).style.background = color1;
				allgood = false;
				} else {
				  document.getElementById(thisformid).style.background = color2; 
				}
			}
		}
		if (allgood) {
			return true;
			} else {
			return false;
			}
	}
	
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function arethesame(condition1,condition2){
	if (condition1 == condition2) {
			return true;
			} else {
			return false;
			}
}

function spiderform(formname,validateid,successid,indicatorid,ajaxpage) {
	//VALIDATE REQUIRED
	if(validateform(formname,'#FAEB44','#FFFFFF')){	
	
		//EMAIL IS VALID?
		var email = document.getElementById("email").value;
		if(!validEmail(email)){
			document.getElementById("email").style.background = "white"; 
			document.getElementById(validateid).innerHTML = 'Invalid Email';
			if (document.getElementById(validateid).style.display == "none"){
					//Effect.Appear(messageid,'blind');
					document.getElementById(validateid).style.display = "block";
					} else {
					//Effect.Shake(messageid);
					document.getElementById(validateid).style.display = "block";
					}
			} else {
			document.getElementById(validateid).style.display = "none";
			document.getElementById(indicatorid).style.display = "block";
			//alert('got here')
			//AJAX POST
            $.post(ajaxpage, $('#' + formname).serialize(), function(data) {
			document.getElementById(successid).innerHTML = data;
			document.getElementById(indicatorid).style.display = "none";
            })	
			}
	}
}

function spiderlogin(formname,validateid,successid,indicatorid,ajaxpage) {
	//alert('got here');
	//VALIDATE REQUIRED
	if(validateform(formname,'#FAEB44','#FFFFFF')){	
		//AJAX POST
		$.post(ajaxpage, $('#' + formname).serialize(), function(data) {
			document.getElementById(successid).innerHTML = data;
			document.getElementById(indicatorid).style.display = "none";
		})	
	} else {
		document.getElementById(validateid).innerHTML = 'Please fill in required fields';	
	}
}

function spiderlogout(validateid,successid,indicatorid) {
		$.get('sc_logout.ashx', function() {
			$('.logout').hide();
			$('.login').show();
			document.getElementById(successid).style.display = 'block';
			document.getElementById(successid).innerHTML = 'You are logged out';
		})	
}


//IMAGE ROLLOVER
function rolloverInit(){
	for (var i=0; i<document.images.length; i++) {
		if (document.images[i].getAttribute('class') == "rollover") {			
			setupRollover(document.images[i]);
		}
	}
}

//IMAGE ROLLOVER
//Last Update August 1, 2008
function setupRollover(thisImage) {
	thisImage.outImage = new Image();
	thisImage.outImage.src = thisImage.src;
	thisImage.onmouseout = rollOut;
	thisImage.overImage = new Image();
	thisImage.overImage.src = thisImage.src.replace('/off/','/on/')
	thisImage.onmouseover = rollOver;	
}

//IMAGE ROLLOVER
function rollOver() {
	this.src = this.overImage.src;
}

//IMAGE ROLLOVER
function rollOut() {
	this.src = this.outImage.src;
}


/* misc functions */
function FormPost(formname,color1,color2) {
	//VALIDATE REQUIRED
	if(validateform(formname,color1,color2)){
		document.forms[formname].submit();
	}
}

function loaddiv(div1,div2){
	document.getElementById(div1).innerHTML = document.getElementById(div2).innerHTML
}

function ajaxload(divtoload,ajaxfile){
	new Ajax.Request(ajaxfile, {
	method: "get",
	onComplete: function(transport) { 
		
		document.getElementById(divtoload).innerHTML = transport.responseText;
		
		}//onComplete
		
		}//-request arguments
		);// new ajax request	
}

function togglediv(divtoshow){
	if (document.getElementById(divtoshow).style.display == 'block'){
	document.getElementById(divtoshow).style.display = 'none';
	} else {
	document.getElementById(divtoshow).style.display = 'block';
	}
}


window.onload = masterloader;

function masterloader(){
	rolloverInit();
	
}
