//DELETE FROM CART
function remitem(cartrow,cartitemid){
	Effect.Fade(cartrow);
	setTimeout("CSBfleXcroll('cart_content')",1000);
	//AJAX POST
	new Ajax.Request('sc_deletecartitem.aspx?id=' + cartitemid, {
	method: "get",
	onComplete: function(transport) { 
		     var resp = transport.responseText.split("^");
		     document.getElementById("Topnav1_sc_count_wrapper").innerHTML = '<span style="color:#90c2ca">' + resp[0] + '</span>';
		     document.getElementById("Topnav1_subtotalwrapper").innerHTML = '<span style="color:#90c2ca">$' + resp[1] + '</span>';
             document.getElementById("subtotalwrapper").innerHTML = '$' + resp[1];

		     var show = document.getElementById('cart_show');
		     var empty = document.getElementById('cart_empty');
		     if (resp[0] == 0){
                show.style.display = 'none';
                show.className = ''
                empty.style.display = 'block';
                }else{
                empty.style.display = 'none';
                show.style.display = 'block';
                show.className = 'flexcroll';
	        }
		}//-onSuccess
		}//-request arguments
		);// new ajax request
}

function ShowSwap(_image,_div){
    var x = document.getElementById(_div);
    x.src = _image
}

function menu(id){
    if (document.getElementById(id).style.display == "none"){
        document.getElementById('brand_title').src = "/images/menus/brandtitle_on.gif";
    }else{
        document.getElementById('brand_title').src = "/images/menus/brandtitle_off.gif";
    }    
}

function menu1(id){
    var selectname = 'select1';
    if (document.getElementById(selectname)){
        if (document.getElementById(id).style.display == "none"){        
        document.getElementById(selectname).style.visibility = "hidden";        
        }else{
        document.getElementById(selectname).style.visibility = "visible";
        }  
    }  
}

function showmenu(themenu){
	if (document.getElementById(themenu).style.display == "none"){
		document.getElementById(themenu).style.display = "block"
	} else {
		document.getElementById(themenu).style.display = "none"
	}
}

//TABS
function loaddiv(divid,ajaxpage,tab,handle){
	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	
}

//ADD TO CART
function changecolor(id){
    document.getElementById(id).style.color = '#fcf2db';
    document.getElementById(id).style.fontWeight = 'bold';
    Effect.Shake(id); 
    setTimeout("changecolorback('Topnav1_sc_count_wrapper')",2000);
}

function changecolorback(id){
    document.getElementById(id).style.color = '#90c2ca';
    document.getElementById(id).style.fontWeight = 'normal';
}

//ADD TO SHOPPING 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){
    document.getElementById('quantity').style.display = 'block';
    document.getElementById('quantity').innerHTML = '<br/>Please fill in a quantity<br/>';
    Effect.Shake('quantity');
    return;
    }
    
//    if (document.getElementById('menu1').style.display == 'none'){
//        Effect.toggle('menu1','appear');
//    }
    new Ajax.Request('sc_addtocart.aspx', {
	method: "post",
	parameters: Form.serialize(document.forms[formname]),
    onSuccess: function(transport,formname) { 	
            
		if(transport.responseText == 'Not Available'){
                alert('Sorry, quantity chosen is not available.');
            } else {
				
				//document.getElementById(messageid).style.display = 'block';
				//document.getElementById(messageid).innerHTML = '<div style="font-weight:bold;font-size:13px;" class="text2">Added!<br /><br /></div>';
				//var t = setTimeout("Effect.Fade('" + messageid + "');",1000);	
				var resp = transport.responseText.split("^");
				
				
		  //    document.getElementById("Topnav1_subtotalwrapper").innerHTML = '<span>$' + resp[1] + '</span>';
		  //      document.getElementById("Topnav1_sc_count_wrapper").innerHTML = '<span>' + resp[0] + '</span>';
				document.getElementById("Topnav1_subtotalwrapper").innerHTML = '<span style="color:#90c2ca">$' + resp[1] + '</span>';
				document.getElementById("Topnav1_sc_count_wrapper").innerHTML = '<span style="color:#90c2ca">' + resp[0] + '</span>';
				document.getElementById("subtotalwrapper").innerHTML = '$' + resp[1];
				document.getElementById('quantity').style.display = 'none';
				if(document.getElementById("menu1").style.display != "none"){
					setTimeout("CSBfleXcroll('cart_content')",1000);
				}
				var show = document.getElementById('cart_show');
				var empty = document.getElementById('cart_empty');
				if (resp[0] == 0){
					show.style.display = 'none';
					document.getElementById('cart_bg').style.display = 'none';
					document.getElementById('shadow').style.display = 'block';
					empty.style.display = 'block';
					}else{
					empty.style.display = 'none';
					show.style.display = 'block';
					document.getElementById('cart_bg').style.display = 'block';
					document.getElementById('shadow').style.display = 'block';
				}
				added('sc_addtocart');
				refreshcart('refreshcart');
				changecolor('Topnav1_sc_count_wrapper');
			
			}
            //refreshtotal('refreshtotal');  
			}//-onSuccess
	    }//-request arguments
    );// new ajax request
}

//ADDED IMAGE
function added(id){
    document.getElementById(id).src = "/images/buttons/added.gif";
    //Effect.Shake(id);
    setTimeout("changeaddedback('sc_addtocart')",5000); 
}

function changeaddedback(id){
    document.getElementById(id).src = "/images/buttons/add.gif";
}

//REFRESH CART
function refreshcart(divid){
    //AJAX REQUEST
    var Digital=new Date()
    var seconds=Digital.getSeconds()
    var hours=Digital.getHours()
    var minutes=Digital.getMinutes()
    new Ajax.Request('/ajax/loadcart.aspx?refresh=' + seconds + hours + minutes, {
    method: "get",
    onSuccess: function(transport) { 
		//alert('hi');
	    document.getElementById(divid).innerHTML = transport.responseText;
	  
	    }//-onSuccess
	    }//-request arguments
	    );// new ajax request	
   
}

//UPDATE CART
function updatecart(formname){
     new Ajax.Request('sc_addtocart.aspx', {
	method: "post",
	parameters: Form.serialize(document.forms[formname]),
    onSuccess: function(transport,formname) { 	
	        var resp = transport.responseText.split("^");
	        document.getElementById("subtotalwrapper").innerHTML = '$' + resp[1]
	    //    document.getElementById("Topnav1_subtotalwrapper").innerHTML = '<span>$' + resp[1] + '</span>';
	    //    document.getElementById("Topnav1_sc_count_wrapper").innerHTML = '<span>' + resp[0] + '</span>';
	        document.getElementById("Topnav1_subtotalwrapper").innerHTML = '<span style="color:#90c2ca">$' + resp[1] + '</span>';
	        document.getElementById("Topnav1_sc_count_wrapper").innerHTML = '<span style="color:#90c2ca">' + resp[0] + '</span>';
	        document.getElementById('quantity').style.display = 'none';
	        
	        refreshcart('refreshcart');
	        CSBfleXcroll('cart_content');
	        changecolor('Topnav1_sc_count_wrapper');
	        
            //refreshtotal('refreshtotal');  
			}//-onSuccess
	    }//-request arguments
    );// new ajax request
}

//REFRESH TOTAL
function refreshtotal(divid){
if (document.getElementById(divid)){
    //AJAX REQUEST
	    new Ajax.Request('/ajax/loadtotal.aspx', {
	    method: "get",
	    onSuccess: function(transport) { 
		    document.getElementById(divid).innerHTML = transport.responseText;
		    
		   // fleXenv.fleXcrollInit();
		    }//-onSuccess
		    }//-request arguments
		    );// new ajax request	
	}	    
}

//EMAIL PAGE TO FRIEND
function emailfrienda(formname,validateid,email1,email2,color1,color2) {
	//VALIDATE REQUIRED
	if(validateform(formname,color1,color2)){				
			
		//DECLARE VARIABLES
		var allgood;
		allgood = true;
		var validatemessage
		validatemessage = ''
	
		//EMAIL IS VALID?
		if(!validEmail(document.getElementById(email1).value)){ 
			validatemessage += 'Invalid Email<br>';
			document.getElementById(email1).style.background = color1; 
			allgood = false;
			} else {
			
			document.getElementById(email1).style.background = color2; 
			}
			
		if(!validEmail(document.getElementById(email2).value)){
			validatemessage += 'Invalid Friend\'s Email<br>';
			document.getElementById(email2).style.background = color1; 
			allgood = false;
			} else {
			document.getElementById(email2).style.background = color2; 
			}
		
		if (allgood) {
		//AJAX POST
		new Ajax.Request('sc_emailpagetofriend.aspx', {
		method: "post",
		parameters: Form.serialize(document.forms[formname]),
		onComplete: function(transport) { 
			document.getElementById(validateid).innerHTML = "Thank You."
			var t = setTimeout("Effect.toggle('blind1','blind')",2000);
			}//-onSuccess
			}//-request arguments
			);// new ajax request	
		}	else {
		
		    document.getElementById(validateid).innerHTML = validatemessage;
		    if (document.getElementById(validateid).style.display == "none"){
		                document.getElementById(validateid).style.display = "block";
		            } else {
		                Effect.Shake(validateid);
		            }
		}
	} 
}

function sendfeedback(formname,validateid,email,color1,color2) {
	//VALIDATE REQUIRED
	if(validateform(formname,color1,color2)){				
			
		//DECLARE VARIABLES
		var allgood;
		allgood = true;
		var validatemessage
		validatemessage = ''
	
		//EMAIL IS VALID?
		if(!validEmail(document.getElementById(email).value)){
			validatemessage += 'Invalid Email<br />';
			document.getElementById(validateid).innerHTML = validatemessage;
			document.getElementById(email).style.background = color1; 
			if (document.getElementById(validateid).style.display == "none"){
		                document.getElementById(validateid).style.display = "block";
		        } else {
		                Effect.Shake(validateid);
		        }
			allgood = false;
			} 
		
		if (allgood) {
		//AJAX POST
		new Ajax.Request('sc_questionsaboutthispage.aspx', {
		method: "post",
		parameters: Form.serialize(document.forms[formname]),
		onComplete: function(transport) { 
			document.getElementById(validateid).innerHTML = "Thank You.";
			var t = setTimeout("Effect.toggle('blind2','blind')",2000);
			
			}//-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;
			}
	}

//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;
}

//CART FLEXCROLL/PROTOTYPE DISPLAY:NONE HACK

function cartfade(id){
	Effect.toggle('menu1','appear');
}

function cartmove() {
	var cart_wrapper = document.getElementById('menu1');
	if (document.URL.indexOf('product')>-1){
        cart_wrapper.style.left = 479 + 'px';
    }else if (document.URL.indexOf('store/default')>-1){
        cart_wrapper.style.left = 496 + 'px';
    }else if (document.URL.indexOf('display')>-1){
        cart_wrapper.style.left = 477 + 'px';
    }else{
        cart_wrapper.style.left = 496 + 'px';
    }
    setTimeout("CSBfleXcroll('cart_content')",2000);
}

function hideselect(id){
    if (document.URL.indexOf('store/default')>-1 ){
        if(document.URL.indexOf('bid') < 0){
            if(document.getElementById('cart_bg').style.display == 'block'){
                Effect.toggle('sc_shopbydiv','appear');
            }
        }
    }    
}

window.onload = masterloader;

function masterloader(){
	rolloverInit();
	cartfade();
}