// 
// Filename : alljavascript.js
// Author : Han Hanafi, info@algosmith.com
// Just for validation
//


function winOpen(inWinName, inFeature) 
{
	myWin=open("", inWinName, inFeature);

	return true; 
}//winOpen


function checkingWhitespaces(inString)
{
	var theLength = inString.length;
	var count = 0;
	if (theLength == 0) {
		return false;
	}
	
	if (theLength > 0) {
		for (var i = 0; i < theLength; i++) {			
			if (inString.charAt(i) == " ") {
				count++;
			}
		}

		if (count == theLength) {
			return false;
		}
	}
	
	return true;
}//checkingWhitespaces


function validateLength(inString, inLimit)
{
	var theLength = inString.length;
	if (theLength < inLimit) {
		return false;
	}

	return true
}//validateLength


function validateEmailAddress(inString)
{
	var theLength = inString.length;

	if (theLength == 0) {
		return false;
	}

	if (theLength > 0) {
		var findAt = inString.indexOf("@");
		var findDot = inString.indexOf(".");
		var findSpc = inString.indexOf(" ");
		
		if (findAt == -1 || findAt == 0 || findAt == theLength-1 || findDot == -1 || findDot == 0 || findDot == theLength-1 || 
			(findSpc > 0 && findSpc < theLength)) {
			return false;
		}
	}
	
	return true;	
}//validateEmailAddress


function validatePassword(inString)
{
	var theLength = inString.length;
	var findSpc = inString.indexOf(" ");

	if (theLength < 5 || theLength > 15 || findSpc >= 0) {
		return false;
	}
	
	return true;
}//ValidateUserName


function validateRegistration(inObj, inType)
{
	var genericParam;
	var response;
	var response1;
	var indexSelection;
	
	if (inType == 1) {
		response = inObj.ageflag.checked;
		if (response == false) {
			alert("Please Check this Field to Validate that You Are at Least 18 Years of Age!");

			inObj.ageflag.focus();		
			return false;
		}

		inObj.ageflag.value = 1;
		
		genericParam = inObj.first.value;
		response = checkingWhitespaces(genericParam);
		if (response == false) {
			alert("Please Complete this Field with Your FirstName!");

			inObj.first.focus();		
			return false;
		}
		
		genericParam = inObj.last.value;
		response = checkingWhitespaces(genericParam);
		if (response == false) {
			alert("Please Complete this Field with Your LastName!");

			inObj.last.focus();		
			return false;
		}

		genericParam = inObj.address1.value;
		response = checkingWhitespaces(genericParam);
		if (response == false) {
			alert("Please Complete this Field with Your Current Address!");

			inObj.address1.focus();		
			return false;
		}

		genericParam = inObj.city.value;
		response = checkingWhitespaces(genericParam);
		if (response == false) {
			alert("Please Complete this Field with the City of Your Current Address!");

			inObj.city.focus();		
			return false;
		}

		indexSelection = inObj.state.selectedIndex;
		genericParam = inObj.state.options[indexSelection].value
		if (genericParam == "") {
			alert("Please Select the State of Your Current Address!");

			inObj.state.focus();		
			return false;
		}

		genericParam = inObj.zip.value;
		response = checkingWhitespaces(genericParam);
		if (response == false) {
			alert("Please Complete this Field with the Zip/Postal Code of Your Current Address!");

			inObj.zip.focus();		
			return false;
		}


		indexSelection = inObj.country.selectedIndex;
		genericParam = inObj.country.options[indexSelection].value
		if (genericParam == "") {
			alert("Please Select the Country of Your Current Address!");

			inObj.country.focus();		
			return false;
		}

		genericParam = inObj.phone.value;
		response = checkingWhitespaces(genericParam);
		if (response == false || genericParam.length < 10) {
			alert("Please Complete this Field with Your Phone!");

			inObj.phone.focus();		
			return false;
		}


		genericParam = inObj.userid.value;
		response = checkingWhitespaces(genericParam);
		response1 = validateEmailAddress(genericParam);	
		if (response == false || response1 == false) {
			alert("Please Complete Your UserID with your Valid Email Address!");

			inObj.userid.focus();		
			return false;
		}


		genericParam = inObj.password.value;	
		response = checkingWhitespaces(genericParam);
		response1 = validatePassword(genericParam);
		if (response == false || response1 == false) {
			alert("Please Fill Your Password between 5 to 15 Characters Length!" + 
			"\nTips: you may combine any letter, digit, and other BUT white space!");
			
			inObj.password.focus();
			return false;
		}

		var reconfirmpass = inObj.repassword.value;		
		if (genericParam != reconfirmpass) {
			alert("Must Retype as same as Your Password!");

			inObj.repassword.focus();
			return false;
		}

	} else if (inType == 2) {

		genericParam = inObj.billfirst.value;
		response = checkingWhitespaces(genericParam);
		if (response == false) {
			alert("Please Complete this Field with Your FirstName!");

			inObj.billfirst.focus();		
			return false;
		}
		
		genericParam = inObj.billlast.value;
		response = checkingWhitespaces(genericParam);
		if (response == false) {
			alert("Please Complete this Field with Your LastName!");

			inObj.billlast.focus();		
			return false;
		}

		genericParam = inObj.billaddress1.value;
		response = checkingWhitespaces(genericParam);
		if (response == false) {
			alert("Please Complete this Field with Your Current Address!");

			inObj.billaddress1.focus();		
			return false;
		}

		genericParam = inObj.billcity.value;
		response = checkingWhitespaces(genericParam);
		if (response == false) {
			alert("Please Complete this Field with the City of Your Current Address!");

			inObj.billcity.focus();		
			return false;
		}

		indexSelection = inObj.billstate.selectedIndex;
		genericParam = inObj.billstate.options[indexSelection].value
		if (genericParam == "") {
			alert("Please Select the State of Your Current Address!");

			inObj.billstate.focus();		
			return false;
		}

		genericParam = inObj.billzip.value;
		response = checkingWhitespaces(genericParam);
		if (response == false) {
			alert("Please Complete this Field with the Zip/Postal Code of Your Current Address!");

			inObj.billzip.focus();		
			return false;
		}


		indexSelection = inObj.billcountry.selectedIndex;
		genericParam = inObj.billcountry.options[indexSelection].value
		if (genericParam == "") {
			alert("Please Select the Country of Your Current Address!");

			inObj.billcountry.focus();		
			return false;
		}

		genericParam = inObj.billphone.value;
		response = checkingWhitespaces(genericParam);
		if (response == false || genericParam.length < 10) {
			alert("Please Complete this Field with Your Phone!");

			inObj.billphone.focus();		
			return false;
		}


		genericParam = inObj.billemail.value;
		response = checkingWhitespaces(genericParam);
		response1 = validateEmailAddress(genericParam);	
		if (response == false || response1 == false) {
			alert("Please Complete this Field with Your Valid Email Address!");

			inObj.billemail.focus();		
			return false;
		}


		//credit card check
		indexSelection = inObj.cardType.selectedIndex;
		var CardType = inObj.cardType.options[indexSelection].value
		if (CardType == "") {
			alert("Please Select the Credit Card Type!");

			inObj.cardType.focus();		
			return false;
		}


		genericParam = inObj.cardNo.value;
		response = checkingWhitespaces(genericParam);
		if (response == false || genericParam.length < 10 || isNaN(genericParam)) {
			alert("Please Complete this Field with Your Valid Credit Card Number!");

			inObj.cardNo.focus();		
			return false;
		}

		
		indexSelection = inObj.cardExpMonth.selectedIndex;
		genericParam = inObj.cardExpMonth.options[indexSelection].value
		if (genericParam == "") {
			alert("Please Select the Month of Expiration as Shown on Your Credit Card!");

			inObj.cardExpMonth.focus();		
			return false;
		}


		indexSelection = inObj.cardExpYear.selectedIndex;
		genericParam = inObj.cardExpYear.options[indexSelection].value
		if (genericParam == "") {
			alert("Please Select the Year of Expiration as Shown on Your Credit Card!");

			inObj.cardExpYear.focus();		
			return false;
		}


		genericParam = inObj.cardCVV.value;
		response = checkingWhitespaces(genericParam);
		if (response == false || genericParam.length < 3 || isNaN(genericParam)) {
			alert("Please Complete this Field with the Security Code Found on Your Credit Card Number!");

			inObj.cardCVV.focus();		
			return false;
		}

		genericParam = inObj.cardIssueNo.value;
		response = checkingWhitespaces(genericParam);
		if (((CardType == "Solo" || CardType == "Switch") && response == false) ||		
			(response == true && isNaN(genericParam))) {
			alert("Please Complete this Field with the Valid Issue Number!");

			inObj.cardIssueNo.focus();		
			return false;
		}

	} else if (inType == 3) {
		
	
		return false;

	} else {

		//no action takes palce.
		return false;
	}



	return true;
}//validateRegistration