/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}

function setShippingInfo(isChecked)
{
	with (window.document.frmCheckout) {
		if (isChecked) {
			shipFirstName.value  = custFirstName.value;
			shipLastName.value   = custLastName.value;
			shipAddress1.value   = custAddress1.value;
			shipAddress2.value   = custAddress2.value;
			shipCity.value       = custCity.value;
			setSelectedValue(shipState, getSelectedValue(custState));
			shipZip.value = custZip.value;
			shipPhone1.value      = custPhone1.value;
			
			shipFirstName.readOnly  = true;
			shipLastName.readOnly   = true;
			shipAddress1.readOnly   = true;
			shipAddress2.readOnly   = true;
			//shipState.readOnly      = true;			
			shipCity.readOnly       = true;
			shipZip.readOnly = true;			
			shipPhone1.readOnly      = true;
		} else {
			shipFirstName.readOnly  = false;
			shipLastName.readOnly   = false;
			shipAddress1.readOnly   = false;
			shipAddress2.readOnly   = false;
			//shipState.readOnly      = false;			
			shipCity.readOnly       = false;
			shipZip.readOnly = false;			
			shipPhone1.readOnly      = false;
		}
	}
}



function validateCheckout(checkID) {
	if(!validateAddress()) return false;
	if(checkID) {
		if(!validateID()) return false;
	}
	if(!validatePayment()) return false;
	if(!skipTMobile && !window.document.frmCheckout.termsTMobile.checked) {
		alert("Please indicate that you have read the T-Mobile terms.");
		return false;
	}

	if(!window.document.frmCheckout.termsUTalk.checked) {
		alert("Please indicate that you have read the UTalkALot terms");
		return false;
	}

	return true;
}

function isValidPhone(phone) {
	if(isEmptyStr(phone)) return false;
	var reg = new RegExp("[a-zA-Z]");
	if(reg.test(phone)) return false;
	return true;
}
function isValidZip(instr) {
	if(isEmptyStr(instr)) return false;
	var reg = new RegExp("[a-zA-Z]");
	if(reg.test(instr)) return false;
	return true;
}


function validateAddress() {

	with (window.document.frmCheckout) {
		if (isEmpty(custFirstName, 'Please enter first name')) {
			return false;
		} else if (isEmpty(custLastName, 'Please enter last name')) {
			return false;
		} else if (isEmpty(custAddress1, 'Please enter address')) {
			return false;
		} else if (isEmpty(custCity, 'Please enter city')) {
			return false;
		} else if (isEmptyStr(getSelectedValue(custState))) {
			alert('Please select your state');
			return false;
		} else if (!isValidZip(custZip.value)) {
			alert('Please enter a valid zip code');
			custZip.focus();
			return false;
		} else if (!isValidPhone(custPhone1.value)) {
			alert('Please enter a valid home phone number');
			custPhone1.focus();
			return false;
		} else if (!isValidPhone(custPhone2.value)) {
			alert('Please enter a valid day phone number');
			custPhone2.focus();
			return false;

		} else if (!isValidEmail(custEmail1.value)) {
			alert('Please enter a valid email address');
			custEmail1.focus();
			return false;
		} else if (isEmpty(custEmail2, 'Please confirm email address')) {
			return false;
		} else if(custEmail2.value != custEmail1.value) {
			alert("You email entries do not match");
			return false;	
		} else if (isEmpty(custPassword, 'Please enter a password for your account')) {
			return false;
		} else if (isEmptyStr(getSelectedValue(custReferredBy))) {
			alert('Please select how you heard about us');
			return false;
		} else if (isEmpty(shipFirstName, 'Please enter shipping first name')) {
			return false;
		} else if (isEmpty(shipLastName, 'Please enter shipping last name')) {
			return false;
		} else if (isEmpty(shipAddress1, 'Please enter shipping address')) {
			return false;
		} else if (isEmpty(shipCity, 'Please enter shipping city')) {
			return false;
		} else if (isEmptyStr(getSelectedValue(shipState))) {
			alert('Please select your shipping state');
			return false;
		} else if (!isValidZip(shipZip.value)) {
			alert('Please enter a valid shipping zip code');
			shipZip.focus();
			return false;
		} else if (!isValidPhone(shipPhone1.value)) {
			alert('Please enter a valid shipping phone number');
			shipPhone1.focus();
			return false;
		}

		return true;
	}
}

function validateID() {

	with (window.document.frmCheckout) {
		if (isEmptyStr(getSelectedValue(custBirthMonth))
		 	|| isEmptyStr(getSelectedValue(custBirthDay))
		 	|| isEmptyStr(getSelectedValue(custBirthYear))) {
			alert('Please enter your birth date');
			return false;
		}

		if (isEmpty(custSSN, 'Please enter your social security number')) {
			custSSN.focus();
			return false;
		} 
		if (isEmptyStr(getSelectedValue(custIdType))) {
			alert('Please select your id type');
			return false;
		}
		if (isEmptyStr(getSelectedValue(custIdState))) {
			alert('Please select the state your id was issued in');
			return false;
		}
		if (isEmpty(custIdNumber, 'Please enter your id number')) {
			custIdNumber.focus();
			return false;
		} 

		if (isEmptyStr(getSelectedValue(custIdMonth))
		 	|| isEmptyStr(getSelectedValue(custBirthYear))) {
			alert('Please enter the expiration date of your id');
			return false;
		}
		if (isEmpty(custIdEmployer, 'Please enter your employer')) {
			custIdEmployer.focus();
			return false;
		} 
		if (isEmpty(areaCode, 'Please enter your requested area code')) {
			areaCode.focus();
			return false;
		} else if(!isInteger(areaCode.value)) {
			alert("Please enter a valid area code.");
			areaCode.focus();
			return false;
		}

		return true;
	}
}

function isInteger(s) {

	for (i = 0; i < s.length; i++) {
         	var c = s.charAt(i);
         	if (!isDigit(c)) return false;
      	}
      	return true;
}
function isDigit (c) {
      return ((c >= "0") && (c <= "9"))
}

function validatePayment() {

	with (window.document.frmCheckout) {
		if (isEmpty(cardNumber, 'Please enter credit card number')) {
			cardNumber.focus();
			return false;
		} 
		if (isEmpty(cardCCV, 'Please enter credit card security number')) {
			cardCCV.focus();
			return false;
		} 

		if (isEmptyStr(getSelectedValue(cardMonth))
		 	|| isEmptyStr(getSelectedValue(cardYear))) {
			alert('Please enter the expiration date of credit card');
			return false;
		}

		return true;
	}
}

function isValidEmail(email) {
	if(isEmptyStr(email)) return false;
	email = trim(email);
	var reg1 = /\s/;
	if(reg1.test(email)) return false;
	var reg2 = /^.+@.+\.[a-zA-Z]+/;
	if(reg2.test(email)) return true;
	return false;
}


function getSelectedValue(select) {
	index = -1;
	for(i=0; i<select.length; i++) {
		if(select[i].selected) { 
			return select[i].value;	
		}
	}
	return("");
}
function setSelectedValue(select, val) {
	index = -1;
	for(i=0; i<select.length; i++) {
		if(select[i].value == val) {
			select[i].selected = true;	
			break;
		}
	}
	return("");
}

function isEmptyStr(s) { return ((s == null) || (s.length == 0)) }

function getCheckedValue(cb) {
	alert(cb.length);
for (var i=0; i < cb.length; i++)
   {
		alert("in for here");
   if (cb[i].checked)
      {
      return cb[i].value;
      }
   }
	return "";
}

function updateDiv(did, newstyle) {
	if ((document.getElementById) || (document.all)) {
		var tstyle = getStyle(did);
		if (tstyle != false) {
			tstyle.display = newstyle;
		}
	}
}

function getStyle(did) {
	if (document.getElementById && document.getElementById(did)) {
		return document.getElementById(did).style;
	} else if (document.all && document.all(did)) {
		return document.all(did).style;
	} else {
		return false;
	}
}




