// JavaScript Document

// Description Switcher
function showItem(id)
{
	document.getElementById('desc_desc').style.display = 'none';
	document.getElementById('desc_features').style.display = 'none';
	document.getElementById('desc_care').style.display = 'none';
	document.getElementById(id).style.display = '';
	return false;
}


function Jump2Brand(elem)
	{
	brand = elem.value;
	window.location = '/products.php?sBrand='+brand;
	}

function Jump2Lifestyle(elem)
	{
	ls = elem.value;
	window.location = '/products.php?sLifestyles='+ls;
	}

function createSizes(elem, where)
	{
	//alert(elem.value);
	// Count current options and remove
	cur = document.getElementById('i');
	cur_total = cur.length;
	//alert(cur.length);
	for(i=1;i<cur_total;i++)
		{
		//alert('del '+i);
		document.getElementById('i').remove(i);
		}
	//alert(cur.length);
	// Get New values
	var s = elem.value;
	var dd = '';
	//alert(s);
	s = s.split('|');
	for(i=0;i<s.length;i++)
		{
		if(s[i] != '')
			{
			n = s[i].split(',');
			var y=document.createElement('option');
			y.text=n[0];
			y.value =n[1];
			if(n[0].indexOf("Sold out!") > -1) y.disabled =  'disabled';
			try
				{
				cur.add(y,null); // standards compliant
				}
			catch(ex)
				{
				cur.add(y); // IE only
				}
			}
		}
	cur.disabled=false;
	}
	

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkPhone(strPhone){
s=stripCharsInBag(strPhone,phoneNumberDelimiters);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}


function co_showError(txt, elem)
	{
	document.getElementById(elem + '_error').innerHTML = '<div class="check_out_error"><div>'+txt+'</div></div>';
	}

function co_noError(elem)
	{
	document.getElementById(elem + '_error').innerHTML = '';
	}
	
function co_check_b_first_name()
	{
	f = 'b_first_name';
	elem = document.getElementById(f);
	if(elem.value.length < 1)
		{
		co_showError('You must enter your first name.', f);
		return false;
		}
	else
		{
		co_noError(f);
		co_same_as_billing();
		return true;
		}
	}

function co_check_b_last_name()
	{
	f = 'b_last_name';
	elem = document.getElementById(f);
	if(elem.value.length < 1)
		{
		co_showError('You must enter your last name.', f);
		return false;
		}
	else
		{
		co_noError(f);
		co_same_as_billing();
		return true;
		}
	}

function co_check_b_phone()
	{
	f = 'b_phone';
	elem = document.getElementById(f);
	if(elem.value  == '' || elem.value == null)
		{
		co_showError('You must enter your phone number. Please do not use any punctuation.', f);
		return false;
		}
	else if (!checkPhone(elem.value))
		{
		co_showError('You must enter a valid phone number including area code. Please do not use any punctuation.', f);
		return false;
		}
	else
		{
		co_noError(f);
		co_same_as_billing();
		return true;
		}
	}

function co_check_b_email()
	{
	f = 'b_email';
	elem = document.getElementById(f);
	if(elem.value  == '' || elem.value == null)
		{
		co_showError('You must enter your email address.', f);
		return false;
		}
	
	else if (!elem.value.match(/^[_a-zA-Z0-9\.\-]+@[_a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,4}$/))
		{
		co_showError('You must enter a valid email address', f);
		return false;
		}
	else
		{
		co_noError(f);
		co_same_as_billing();
		return true;
		}
	}
	
function co_check_b_addr1()
	{
	f = 'b_addr1';
	elem = document.getElementById(f);
	if(elem.value.length < 3)
		{
		co_showError('You must enter the address associated with your credit card.', f);
		return false;
		}
	else
		{
		co_noError(f);
		co_same_as_billing();
		return true;
		}
	}


function co_check_b_city()
	{
	f = 'b_city';
	elem = document.getElementById(f);
	if(elem.value.length < 2)
		{
		co_showError('You must enter the city associated with your credit card.', f);
		return false;
		}
	else
		{
		co_noError(f);
		co_same_as_billing();
		return true;
		}
	}

function co_check_b_state()
	{
	f = 'b_state';
	elem = document.getElementById(f);
	if(elem.value == 'null')
		{
		co_showError('You must enter the state associated with your credit card.', f);
		return false;
		}
	else
		{
		co_noError(f);
		co_same_as_billing();
		return true;
		}
	}

function co_check_b_zip()
	{
	f = 'b_zip';
	elem = document.getElementById(f);
	if(elem.value.length != 5 || isNaN(elem.value))
		{
		co_showError('You must enter the 5-digit zip code associated with your credit card.', f);
		return false;
		}
	else
		{
		co_noError(f);
		co_same_as_billing();
		return true;
		}
	}

function co_check_cctype()
	{
	f = 'cctype';
	elem = document.getElementById(f);
	if(elem.value == 'null' || elem.value == '')
		{
		co_showError('You must choose the type of credit card you are using.', f);
		return false;
		}
	else
		{
		co_noError(f);
		return true;
		}
	}

function co_check_ccnum()
	{
	f = 'ccnum';
	elem = document.getElementById(f);
	if(elem.value.length < 13 || isNaN(elem.value))
		{
		co_showError('You must enter the full credit card number without spaces or dashes.', f);
		return false;
		}
	else
		{
		co_noError(f);
		return true;
		}
	}

	
function co_check_exp()
	{
	f = 'exp';
	m = document.getElementById('exp_m');
	y = document.getElementById('exp_y');
	if(m.value == 'null' || y.value == 'null')
		{
		co_showError('You must enter a correct expiration date for this credit card.', f);
		return false;
		}
	else
		{
		co_noError(f);
		return true;
		}
	}

function co_same_as_billing()
	{
	if(document.getElementById('sb').checked == true)
		{
		document.getElementById(	's_first_name').value = document.getElementById('b_first_name').value;
		document.getElementById(	's_first_name').disabled = 'disabled';
		co_noError('s_first_name');
		document.getElementById(	's_last_name').value = document.getElementById('b_last_name').value;
		document.getElementById(	's_last_name').disabled = 'disabled';
		co_noError('s_last_name');
		document.getElementById(	's_addr1').value = document.getElementById('b_addr1').value;
		document.getElementById(	's_addr1').disabled = 'disabled';
		co_noError('s_addr1');
		document.getElementById('s_city').value = document.getElementById('b_city').value;
		document.getElementById('s_city').disabled = 'disabled';
		co_noError('s_city');
		document.getElementById('s_state').selectedIndex = document.getElementById('b_state').selectedIndex;
		document.getElementById(	's_state').disabled = 'disabled';
		co_noError('s_state');
		document.getElementById(	's_zip').value = document.getElementById('b_zip').value;
		document.getElementById(	's_zip').disabled = 'disabled';
		co_noError('s_zip');
		}
	}

function so_same_as_billing_click()
	{
	if(document.getElementById('sb').checked == true)
		{
		co_same_as_billing();
		}
	else
		{
		document.getElementById('s_first_name').value = '';
		document.getElementById('s_first_name').disabled = '';
		document.getElementById('s_last_name').value = '';
		document.getElementById('s_last_name').disabled = '';
		document.getElementById('s_addr1').value = '';
		document.getElementById('s_addr1').disabled = '';
		document.getElementById('s_city').value = '';
		document.getElementById('s_city').disabled = '';
		document.getElementById('s_state').selectedIndex = 0;
		document.getElementById('s_state').disabled = '';
		document.getElementById('s_zip').value = '';
		document.getElementById('s_zip').disabled = '';
		}
	}

function co_check_s_first_name()
	{
	if(document.getElementById('sb').checked == true) return true;
	
	f = 's_first_name';
	elem = document.getElementById(f);
	if(elem.value.length < 1)
		{
		co_showError('You must enter the first name of the person you are shipping this to.', f);
		return false;
		}
	else
		{
		co_noError(f);
		return true;
		}
	}

function co_check_s_last_name()
	{
	if(document.getElementById('sb').checked == true) return true;
	f = 's_last_name';
	elem = document.getElementById(f);
	if(elem.value.length < 1)
		{
		co_showError('You must enter the last name of the person you are shipping this to.', f);
		return false;
		}
	else
		{
		co_noError(f);
		return true;
		}
	}

function co_check_s_addr1()
	{
	if(document.getElementById('sb').checked == true) return true;
	f = 's_addr1';
	elem = document.getElementById(f);
	if(elem.value.length < 1)
		{
		co_showError('You must enter the address you want this order shipped to.', f);
		return false;
		}
	else
		{
		co_noError(f);
		return true;
		}
	}

function co_check_s_city()
	{
	if(document.getElementById('sb').checked == true) return true;
	f = 's_city';
	elem = document.getElementById(f);
	if(elem.value.length < 2)
		{
		co_showError('You must enter the city you want this order shipped to.', f);
		return false;
		}
	else
		{
		co_noError(f);
		return true;
		}
	}

function co_check_s_state()
	{
	if(document.getElementById('sb').checked == true) return true;
	f = 's_state';
	elem = document.getElementById(f);
	if(elem.value == 'null')
		{
		co_showError('You must enter the state you want this order shipped to.', f);
		return false;
		}
	else
		{
		co_noError(f);
		return true;
		}
	}

function co_check_s_zip()
	{
	if(document.getElementById('sb').checked == true) return true;
	f = 's_zip';
	elem = document.getElementById(f);
	if(elem.value.length != 5 || isNaN(elem.value))
		{
		co_showError('You must enter the 5-digit zip code you want this order shipped to.', f);
		return false;
		}
	else
		{
		co_noError(f);
		return true;
		}
	}

function co_check_form()
	{
	var toReturn = true;
	if(!co_check_b_first_name()) toReturn = false;
	if(!co_check_b_last_name()) toReturn = false;
	if(!co_check_b_phone()) toReturn = false;
	if(!co_check_b_email()) toReturn = false;
	if(!co_check_b_addr1()) toReturn = false;
	if(!co_check_b_city()) toReturn = false;
	if(!co_check_b_state()) toReturn = false;
	if(!co_check_b_zip()) toReturn = false;
	if(!co_check_ccnum()) toReturn = false;
	if(!co_check_exp()) toReturn = false;
	if(!co_check_s_first_name()) toReturn = false;
	if(!co_check_s_last_name()) toReturn = false;
	if(!co_check_s_addr1()) toReturn = false;
	if(!co_check_s_city()) toReturn = false;
	if(!co_check_s_state()) toReturn = false;
	if(!co_check_s_zip()) toReturn = false;
	if(!co_check_cctype()) toReturn = false;
	if(!co_check_ccnum()) toReturn = false;
	if(!co_check_exp()) toReturn = false;
	return toReturn;
	}

function changeBigImage(img, w, h)
{
	document.getElementById('imageBox').innerHTML = '<img src="/images/additionalImages/' + img +'" border="0"  />';
}