//FORUM
function postmessage(formname, color1, color2) {
    if (validateform(formname, color1, color2)) {
        document.getElementById('postmessageindicator').style.display = "block";
        document.forms[formname].submit();
    }
}

//LOG IN
function logina(formname,validateid) {
	//VALIDATE REQUIRED
	if(validateform(formname,'yellow','#EDEDED')){
		//AJAX POST
		new Ajax.Request('/sc_login.ashx', {
		method: "post",
		parameters: Form.serialize(document.forms[formname]),
		onSuccess: function(transport) { 	
			//LOG IN EXISTS?
			if (transport.responseText=='true'){
			    document.forms[formname].submit();
				document.getElementById(validateid).innerHTML = '';
			} else {
				document.getElementById('indicatorid').style.display = "none";
				document.getElementById(validateid).innerHTML = 'Invalid Log In';
				if (document.getElementById(validateid).style.display == "none"){
					Effect.Appear(validateid,'blind');
				} else {
					Effect.Shake(validateid);
					}
				}
			}//-onSuccess
			}//-request arguments
			);// new ajax request		
	} else {
			//FILL IN THE REQUIRED FIELDS
			document.getElementById('indicatorid').style.display = "none";
			if (document.getElementById(validateid).style.display == "none"){
				document.getElementById(validateid).innerHTML = 'Please fill in required fields';
					Effect.Appear(validateid,'blind');
					} else {
					Effect.Shake(validateid);
					}
			
	}
}

//CHANGE PASSWORD
function changepasword(formname,validateid) {

	//VALIDATE REQUIRED
	if(validateform(formname,'yellow','#EDEDED')){
		//AJAX POST
		new Ajax.Request('/sc_changepass.ashx', {
		method: "post",
		parameters: Form.serialize(document.forms[formname]),
		onSuccess: function(transport) { 	
			if (transport.responseText=='true'){
			    document.forms[formname].submit();
				document.getElementById(validateid).innerHTML = 'Your Password Has Been Changed.';
			} else {
				document.getElementById('indicatorid').style.display = "none";
				document.getElementById(validateid).innerHTML = transport.responseText;
				if (document.getElementById(validateid).style.display == "none"){
					Effect.Appear(validateid,'blind');
				} else {
					Effect.Shake(validateid);
					}
				}
			}//-onSuccess
			}//-request arguments
			);// new ajax request		
	} else {
			//FILL IN THE REQUIRED FIELDS
			document.getElementById('indicatorid').style.display = "none";
			if (document.getElementById(validateid).style.display == "none"){
				document.getElementById(validateid).innerHTML = 'Please fill in required fields';
					Effect.Appear(validateid,'blind');
					} else {
					Effect.Shake(validateid);
					}	
	}
}

//EMAIL PASSWORD
function emailpassworda(formname,validateid,color1,color2) {
    
	//VALIDATE REQUIRED
	if(validateform(formname,color1,color2)){
		//EMAIL IS VALID?
		
		var email = document.getElementById("passwordemail").value;
		
		var theid = validateid;
        
		if(validEmail(email)){
		
			//AJAX POST
			new Ajax.Request('sc_emailpassword.ashx', {
			method: "post",
			parameters: Form.serialize(document.forms[formname]),
			onSuccess: function(transport,formname) { 
				var validatemessage
				var theid = validateid;
				//USER IS AVAILABLE?
				if (transport.responseText=='true'){	
					document.getElementById(theid).innerHTML = 'Thank You. <br />Your password has been emailed.';
					Effect.Appear(theid,'blind');
					var t = setTimeout("Effect.toggle('" + validateid + "');",2000);
				} else {
					document.getElementById(theid).innerHTML = transport.responseText;
					if (document.getElementById(theid).style.display == "none"){
					Effect.Appear(theid,'blind');
					
					} else {
					Effect.Shake(theid);
					}
				}
				}
				}
				);// new ajax request
			} else {
				document.getElementById(theid).innerHTML = 'Invalid Email';
				document.getElementById("passwordemail").style.background = "yellow"; 
				if (document.getElementById(theid).style.display == "none"){
					Effect.Appear(theid,'blind');
					} else {
					Effect.Shake(theid);
			}
		}
	}
}

//VALIDATION
function createaccount(formname,validateid,indicatorid,color1,color2) {
	//VALIDATE REQUIRED
	if(validateform(formname,color1,color2)){	 
	    document.forms[formname].__VIEWSTATE.name = 'NOVIEWSTATE';
	    document.forms[formname].__VIEWSTATE.id= 'NOVIEWSTATE';

		new Ajax.Request('sc_createnewaccount.ashx', {
		method: "post",
		parameters: Form.serialize(document.forms[formname]),
		onComplete: function(transport) {
		        var r = transport.responseText; 
		        if (!r.indexOf("KeepGoing")){
		            document.forms[formname].NOVIEWSTATE.name = '__VIEWSTATE';
		            document.forms[formname].NOVIEWSTATE.id = '__VIEWSTATE';
		            document.getElementById(validateid).style.display = "none";
		            document.forms[formname].submit();
		        } else {
		            document.getElementById(indicatorid).style.display = "none";
		            document.getElementById(validateid).innerHTML = r;
		            document.getElementById(validateid).style.display = "block";
                    formfieldbg(r,"Zip","billing_zip",color1,color2);
                    formfieldbg(r,"Email","billing_email",color1,color2);
                    formfieldbg(r,"Username","newuser",color1,color2);
                    formfieldbg(r,"Passwords","newpassword",color1,color2);
                    formfieldbg(r,"Passwords","newpassword2",color1,color2);	           
		            if (document.getElementById(validateid).style.display == "none"){
		                document.getElementById(validateid).style.display = "block";
		            } else {
		                Effect.Shake(validateid);
		            }
		        }
			}
		}
		);
	} else {
	    document.getElementById('indicatorid').style.display = "none";
	    document.getElementById(validateid).innerHTML = 'Please fill in Required Fields.';
	    if (document.getElementById(validateid).style.display == "none"){
			Effect.Appear(validateid,'blind');
		} else {
			Effect.Shake(validateid);
		}
	}
}

function createevent(formname,validateid,indicatorid,color1,color2) {
	//VALIDATE REQUIRED
	if(validateform(formname,color1,color2)){	 
	    document.forms[formname].__VIEWSTATE.name = 'NOVIEWSTATE';
	    document.forms[formname].__VIEWSTATE.id= 'NOVIEWSTATE';
        new Ajax.Request('sc_createnewevent.ashx', {
		//new Ajax.Request('/ajax/createevent.aspx', {
		method: "post",
		parameters: Form.serialize(document.forms[formname]),
		onComplete: function(transport) {
		        var r = transport.responseText; 
		        if (!r.indexOf("KeepGoing")){
		            document.forms[formname].NOVIEWSTATE.name = '__VIEWSTATE';
		            document.forms[formname].NOVIEWSTATE.id = '__VIEWSTATE';
		            document.getElementById(validateid).style.display = "none";
		            document.forms[formname].submit();
		        } else {
		            document.getElementById(indicatorid).style.display = "none";
		            document.getElementById(validateid).innerHTML = r;
		            document.getElementById(validateid).style.display = "block";
                       
		            if (document.getElementById(validateid).style.display == "none"){
		                document.getElementById(validateid).style.display = "block";
		            } else {
		                Effect.Shake(validateid);
		            }
		        }
			}
		}
		);
	} else {
	    document.getElementById('indicatorid').style.display = "none";
	    document.getElementById(validateid).innerHTML = 'Please fill in Required Fields.';
	    if (document.getElementById(validateid).style.display == "none"){
			Effect.Appear(validateid,'blind');
		} else {
			Effect.Shake(validateid);
		}
	}
}

function applyaffiliate(formname,validateid,indicatorid,color1,color2) {
	//VALIDATE REQUIRED
	if(validateform(formname,color1,color2)){	 
	    document.forms[formname].__VIEWSTATE.name = 'NOVIEWSTATE';
	    document.forms[formname].__VIEWSTATE.id= 'NOVIEWSTATE';
        new Ajax.Request('sc_affiliate.ashx', {
		//new Ajax.Request('/ajax/affiliate.aspx', {
		method: "post",
		parameters: Form.serialize(document.forms[formname]),
		onComplete: function(transport) {
		        var r = transport.responseText; 
		        if (!r.indexOf("KeepGoing")){
		            document.forms[formname].NOVIEWSTATE.name = '__VIEWSTATE';
		            document.forms[formname].NOVIEWSTATE.id = '__VIEWSTATE';
		            document.getElementById(validateid).style.display = "none";
		            document.forms[formname].submit();
		        } else {
		            document.getElementById(indicatorid).style.display = "none";
		            document.getElementById(validateid).innerHTML = r;
		            document.getElementById(validateid).style.display = "block";
                       
		            if (document.getElementById(validateid).style.display == "none"){
		                document.getElementById(validateid).style.display = "block";
		            } else {
		                Effect.Shake(validateid);
		            }
		        }
			}
		}
		);
	} else {
	    document.getElementById('indicatorid').style.display = "none";
	    document.getElementById(validateid).innerHTML = 'Please fill in Required Fields.';
	    if (document.getElementById(validateid).style.display == "none"){
			Effect.Appear(validateid,'blind');
		} else {
			Effect.Shake(validateid);
		}
	}
}

function updateaffiliate(formname,validateid,indicatorid,color1,color2) {
	//VALIDATE REQUIRED
	if(validateform(formname,color1,color2)){	 
	    document.forms[formname].__VIEWSTATE.name = 'NOVIEWSTATE';
	    document.forms[formname].__VIEWSTATE.id= 'NOVIEWSTATE';
        new Ajax.Request('sc_affiliate_update.ashx', {
		method: "post",
		parameters: Form.serialize(document.forms[formname]),
		onComplete: function(transport) {
		        var r = transport.responseText; 
		        if (!r.indexOf("KeepGoing")){
		            document.forms[formname].NOVIEWSTATE.name = '__VIEWSTATE';
		            document.forms[formname].NOVIEWSTATE.id = '__VIEWSTATE';
		            document.getElementById(validateid).style.display = "none";
		            document.forms[formname].submit();
		        } else {
		            document.getElementById(indicatorid).style.display = "none";
		            document.getElementById(validateid).innerHTML = r;
		            document.getElementById(validateid).style.display = "block";
                       
		            if (document.getElementById(validateid).style.display == "none"){
		                document.getElementById(validateid).style.display = "block";
		            } else {
		                Effect.Shake(validateid);
		            }
		        }
			}
		}
		);
	} else {
	    document.getElementById('indicatorid').style.display = "none";
	    document.getElementById(validateid).innerHTML = 'Please fill in Required Fields.';
	    if (document.getElementById(validateid).style.display == "none"){
			Effect.Appear(validateid,'blind');
		} else {
			Effect.Shake(validateid);
		}
	}
}

function formfieldbg(search1,search2,elementid,color1,color2){
     if (search1.indexOf(search2)>-1){
        document.getElementById(elementid).style.background = color1;
     } else {
        document.getElementById(elementid).style.background = color2;
     }        
}

function ShowSwap(_image,_div){
    var x = document.getElementById(_div);
    x.src = _image
}

function showmenu(themenu){
	if (document.getElementById(themenu).style.display == "none"){
		document.getElementById(themenu).style.display = "block"
	} else {
		document.getElementById(themenu).style.display = "none"
	}
}

function loaddiv(divid,ajaxpage,tab,handle){
	//TABS
	var x=document.getElementsByTagName("img");
	for (var i=0;i<x.length;i++){
		if (x[i].id.indexOf(handle)>-1){
			var element = document.getElementById(x[i].id);
			if (element.id == tab){
				element.src = element.src.replace('/off/','/on/')
			} else {
				element.onmouseover = tabover;
				element.onclick = tabclick; 
				element.onmouseout = tabout; 
				element.src = element.src.replace('/on/','/off/')
			}
		}
	}
	
	//AJAX REQUEST
	new Ajax.Request(ajaxpage, {
	method: "get",
	onComplete: function(transport) { 
		document.getElementById(divid).innerHTML = transport.responseText;
		}//-onSuccess
		}//-request arguments
		);// new ajax request	
}

function nav(id) {
    var x=document.getElementsByTagName("div");
    for (var i=0;i<x.length;i++){
		if (x[i].id.indexOf(id)>-1){
		    var element = document.getElementById(x[i].id);
		    var theid = element.id.replace(id,'');
		    //var theid = element.id.replace('_drop','');
		    //var theid = theid + "_drop";
		    //alert(theid);
            var dropdiv = document.getElementById(id + theid);
            dropdiv.onmouseover = function() {
				
                var navtoswitch = this.id;
				//alert(navtoswitch);
				if (navtoswitch.indexOf('drop')>-1){
                	document.getElementById(navtoswitch).style.backgroundColor = "#c7e09f";
				}
            }
            dropdiv.onmouseout = function() {
                //alert('hi1');
                var navtoswitch = this.id;
				if (navtoswitch.indexOf('drop')>-1){
                	document.getElementById(navtoswitch).style.backgroundColor = "#e2e1e1";
				}
            }
        }
    }
    
}

function spidermenus(triggerhandle,menuhandle){
	var x=document.getElementsByTagName("div");
	for (var i=0;i<x.length;i++){
		if (x[i].id.indexOf(triggerhandle)>-1){
			//VARIABLES
			var element = document.getElementById(x[i].id);
			var theid = element.id.replace(triggerhandle,'');
            
			//TRIGGERS
			var thetrigger = document.getElementById(triggerhandle + theid);
			thetrigger.onmouseover = function() {      
				var menutoswitch;
				menutoswitch = this.id.replace('trigger','');
				
				document.getElementById(menutoswitch).style.display = "block";
				var navimg = this.id.replace('menutrigger','');
				document.getElementById('nav' + navimg).src = document.getElementById('nav' + navimg).src.replace('/off/','/roll/');
			    if ($('sc_vollink')) {
			         $('sc_vollink').style.position='static';
			    }
			    
			    };
			thetrigger.onmouseout = function() {
				var menutoswitch;
				menutoswitch = this.id.replace('trigger','');
				document.getElementById(menutoswitch).style.display = "none";
                var navimg = this.id.replace('menutrigger','');
                
				document.getElementById('nav' + navimg).src = document.getElementById('nav' + navimg).src.replace('/roll/','/off/');
			    if ($('sc_vollink')) {
			         $('sc_vollink').style.position='relative';
			    }
			};
			
			//MENUS
			var themenu = document.getElementById(menuhandle + theid);
			themenu.onmouseover = function() {
				var menutoswitch;
				menutoswitch = this.id;
				document.getElementById(menutoswitch).style.display = "block";
				if ($('sc_vollink')) {
			         $('sc_vollink').style.position='static';
			    }
				
			};
			themenu.onmouseout = function() {
				var menutoswitch;
				menutoswitch = this.id;
				document.getElementById(menutoswitch).style.display = "none";
				if ($('sc_vollink')) {
			         $('sc_vollink').style.position='relative';
			    }
			};
		} 
	}
}

//UPDATE CART
function updatecart(formname){
    new Ajax.Request('/sc_addtocart.ashx', {
	method: "post",
	parameters: Form.serialize(document.forms[formname]),
    onSuccess: function(transport) {
        var resp = transport.responseText.split("^");
	    document.getElementById("sc_minicart_subtotalwrapper").innerHTML = '$' + resp[1];
			}//-onSuccess
	    }//-request arguments
    );// new ajax request
}

//ADD TO CART
function addtoshoppingcart(formname,messageid){ 
    var x = document.forms[formname];
	var keepgoing = false;
	for (var i=0;i<x.length;i++){
		if (x.elements[i].name.indexOf('item_qty')>-1) {
		 	var thisformname = x.elements[i].name;
			var thisformvalue = x.elements[i].value;
			if (thisformvalue != ''){
			    keepgoing = true;
		    }
	    }
    }
    
    if (!keepgoing){
        $('quantity').style.display = 'block';
        $('quantity').innerHTML = '<br/>Please fill in a quantity<br/>';
        Effect.Shake('quantity');
        return;
    }
    var Digital=new Date()
    var seconds=Digital.getSeconds()
    var hours=Digital.getHours()
    var minutes=Digital.getMinutes()
    new Ajax.Request('/sc_addtocart.ashx?refresh=' + seconds + hours + minutes, {
    method: "post",
    parameters: Form.serialize(document.forms[formname]),
    onSuccess: function(transport,formname) { 	
            $('quantity').style.display = 'none';
            $(messageid).style.display = 'block';
            $(messageid).innerHTML = 'Added!';
            var t = setTimeout("Effect.Fade('" + messageid + "');",1000);	
            refreshcart('refreshcart');
		    }//-onSuccess
        }//-request arguments
    );// new ajax request
}

//REFRESH CART
function refreshcart(divid){
    var Digital=new Date()
    var seconds=Digital.getSeconds()
    var hours=Digital.getHours()
    var minutes=Digital.getMinutes()
    new Ajax.Request('/ajax/loadcart.aspx?refresh=' + new Date().getTime(), {
	    method: "get",
	    onSuccess: function(transport) { 
		    document.getElementById(divid).innerHTML = transport.responseText;
		    }//-onSuccess
		    }//-request arguments
		    );// new ajax request
}

//DELETE FROM CART
function remitem(cartrow,cartitemid){
	Effect.Fade(cartrow);
	
	//AJAX POST
	new Ajax.Request('/sc_deletecartitem.ashx?id=' + cartitemid, {
	method: "get",
	onComplete: function(transport) { 
        var resp = transport.responseText.split("^");
	    document.getElementById("sc_minicart_subtotalwrapper").innerHTML = '$' + resp[1];
		}//-onSuccess
		}//-request arguments
		);// new ajax request
}


//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;}
		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 SpiderWindow(url){
    mywindow = window.open(url, "windowname","location=0,status=0,scrollbars=0,width=300,height=450");
    mywindow.focus(); 
 } 

//IMAGE ROLLOVER
function rolloverInit(){
	for (var i=0; i<document.images.length; i++) {
	if (navigator.appName == "Netscape"){
		if (document.images[i].hasAttribute("name")){ 
				if (document.images[i].getAttribute('name').indexOf("rollover")>-1){			
					setupRollover(document.images[i]);
				}
		}
	} else
		if (document.images[i].getAttribute('name').indexOf("rollover")>-1){			
			setupRollover(document.images[i]);
		}
	}
}

//IMAGE ROLLOVER
//Last Update August 1, 2008
function setupRollover(thisImage) {
	thisImage.outImage = new Image();
	thisImage.outImage.src = thisImage.src;
	//alert(thisImage.id);
	thisImage.outImage.id = thisImage.id;
	thisImage.onmouseout = rollOut;
	thisImage.overImage = new Image();
	thisImage.overImage.id = thisImage.id
	thisImage.overImage.src = thisImage.src.replace('/off/','/roll/')
	thisImage.onmouseover = rollOver;
}

//IMAGE ROLLOVER
function rollOver() {
	this.src = this.overImage.src;
}

//IMAGE ROLLOVER
function rollOut() {
	this.src = this.outImage.src;
}

window.onload = masterloader;

function masterloader(){
	rolloverInit();
	spidermenus('menutrigger','menu');
	nav('sc_header_');
	//nav('drop');
}
