//roller menu

roller = function() {

total = $('#innerContainer li').length;
$totalImgs = $('#innerContainer li');

thumbHeight = 0;

for (x=0; x<total; x++) {
	thumbHeight = thumbHeight + 29;
}

$('#innerContainer') .css({height: thumbHeight + 'px'});

$('#cbslideUp') .hover(function(e){
	$('#innerContainer') .stop();
	posTop = -parseInt($('#innerContainer').css("top"))
	speed = (posTop) * 4;
	slideTop = thumbHeight - 340;
	$('#innerContainer') .animate({"top": "0px"}, speed)
},function(){
	$('#innerContainer') .stop();
});

$('#cbslideDown') .hover(function(e){
	posTop = parseInt($('#innerContainer').css("top"))
	speed = (thumbHeight + posTop) * 4;
	slideTop = thumbHeight - 340;
	$('#innerContainer') .stop();
	$('#innerContainer') .animate({"top": -slideTop + "px"}, speed)
},function(){
	$('#innerContainer') .stop();
});
}


//Password Testing
var required = "1px solid #d24508";
var normal = "1px solid #ccc";

function changeStyle(x) {
	x.style.border = normal;
	x.style.backgroundColor = '#fff';
}


function testPassword () {
	var img = document.getElementById("password_strength");
	
	var password = document.getElementById("p");
	var p = password.value;
	
	var flag1 = false;
	var flag2 = false;
	var flag3 = false;
	
	var items1 = Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
	var items2 = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
	
	if (p.length < 4) {
		img.src="images/password1.jpg";
	} else {
		
		for (i=0; i<p.length; i++) {
			
			var char = p.charAt(i);
			
			// test for lowercase letter
			for (j=0; j<items1.length; j++) {
				if (char == items1[j]) {
					flag1 = true;
					break;
				}
			}
			
			// test for uppercase letter
			for (j=0; j<items1.length; j++) {
				if (char == items1[j].toUpperCase()) {
					flag2 = true;
					break;
				}
			}
			
			// test for numbers
			for (j=0; j<items2.length; j++) {
				if (char == items2[j]) {
					flag3 = true;
					break;
				}
			}
		}
		
		img.src="images/password1.jpg";
			
		
		if (flag1 && flag2) {
			img.src="images/password2.jpg";
		} else if (flag1 && flag3) {
			img.src="images/password2.jpg";
		} else if (flag2 && flag3) {
			img.src="images/password2.jpg";
		}
		
		if (flag1 && flag2 && flag3) {
			img.src="images/password3.jpg";
		}
	}
	
	doubleCheckPassword ();
}

function doubleCheckPassword () {
	var img = document.getElementById("password_same");
	
	var password = document.getElementById("p");
	var password2 = document.getElementById("p2");
	var p = password.value;
	var p2 = password2.value;
	
	if (p.length > 0) {
		if (p == p2) {
			img.src="images/check1.jpg";
		} else {
			img.src="images/check0.jpg";
		}
	}
}

function testLogin () {
	
	var f = document.getElementById("managelogin");
	
	var password = document.getElementById("p");
	var password2 = document.getElementById("p2");
	var p = password.value;
	var p2 = password2.value;
	
	var success = false;
	
	if (p != "") {
		if (p == p2) {
			success = true;
		}
	}
	
	if (success) {
		f.submit();	
	} else {
		password.style.border = required;
		password2.style.border = required;
		alert('Your passwords do not match. Please try again.');
	}
}


// Confirm Logout
function confirmLogout(){
	var agree=confirm("Are you sure you want to logout?");
if (agree)
	return true ;
else
	return false ;
};



//Reset Menus
function resetForms(num, levelChanged) {
	
	var f = document.getElementById("menuForm");
	var n = num; // how many menus are showing
	var l = levelChanged; // the select box that was changed
	
	
	if (n >= 2 && l < 2) {
		var m2 = f.menu_2.selectedIndex = 0;
	}
	
	if (n >= 3 && l < 3) {
		var m3 = f.menu_3.selectedIndex = 0;
	}
	
	if (n >= 4 && l < 4) {
		var m4 = f.menu_4.selectedIndex = 0;
	}
	
	if (n >= 5 && l < 5) {
		var m5 = f.menu_5.selectedIndex = 0;
	}
	
	if (n >= 6 && l < 6) {
		var m6 = f.menu_6.selectedIndex = 0;
	}


	f.submit();
}


//Shopping Cart Functions

function getCookie(c_name) { 
	if (document.cookie.length>0) {
		
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
		
	}
	return "";
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	
}


function addToCart(ref, title, price, o1, o2, o3, o4, o5, o6, ship, c1, c2, c3, shipamt, shipfree) {
	
	var cart = getCookie("carbonCart");
	
	var f = document.getElementById("order_Form");
	var quantity = (f.quantity.selectedIndex + 1);
	var o7 = f.fillable1.value;
	var o8 = f.fillable2.value;
	var o9 = f.fillable3.value;
	
	//validation
	if(o1 != "" && o2 != "" && o3 != "" && o4 != "" && o5 != "" && o6 != "" && o7 != "" && o8 != "" && o9 != ""){
	
		//set cart
		var purchase = (ref + "%%%" + title + "%%%" + price + "%%%" + o1 + "%%%" + o2 + "%%%" + o3 + "%%%" + o4 + "%%%" + o5 + "%%%" + o6 + "%%%" + o7 + "%%%" + o8 + "%%%" + o9 + "%%%" + ship + "%%%" + quantity + "%%%" + c1 + "%%%" + c2 + "%%%" + c3 + "%%%" + shipamt + "%%%" + shipfree + "%%%%");
		
		
		if (cart != undefined) {
			purchase = cart+purchase;
		}
		
		setCookie("carbonCart", purchase, 365);
		
		f.submit();
		
	}else{
		alert ("Please select all your options and fill out all the fields.");
	}
}


function removeFromCart(id) {
	
	var cart = getCookie("carbonCart");
	
	cart = cart.split("%%%%");
	var updated = "";
	
	for (i=0; i<cart.length-1; i++) {
		if (i!= id) {
			updated += cart[i] + "%%%%";
		}
	}
	
	setCookie("carbonCart", updated, 365);
	alert ("Item Removed From Cart");
}



function checkout() {
	
	var f = document.getElementById("orderForm");
	
	var fname = f.fname.value;
	var lname = f.lname.value;
	var phone = f.phone.value;
	var email = f.email.value;
	var address = f.s_address.value;
	var city = f.s_city.value;
	var province = f.s_province.value;
	var postalcode = f.s_postalcode.value;
	
	
	var ccname = f.ccname.value;
	var ccnumber = f.ccnumber.value;
	var validation = f.code.value;
	var terms = f.terms.checked;
	
	var success = true;
	
	if (fname == "" || lname == "" || phone == "" || email == "" || address == "" || city == "" || province == "" || postalcode == "" || ccname == "" || ccnumber == "" || validation != "542794" || terms == false) {
		success = false;
	}
	
	if (success == true) {
		f.submit()	
	} else {
		alert ("Error! Please fill out all required fields, agree to the terms of use, and answer the security question correctly.");	
	}
}




function deleteCart () {
	setCookie("carbonCart", "", -3600);
}


