
//*************************************************************************************
//Task History
//*************************************************************************************
//Written by Kelechi Eke
//*************************************************************************************

function ProcessData(){
	CheckNameInput();
}

function CheckNameInput(){
	if (isFilled(frmConfirm.custname)==false){
		window.alert("Customer Name is required.");
		frmConfirm.custname.focus();}
	else{
		CheckEmailInput();
	}		
}

function CheckEmailInput(){
	if (isFilled(frmConfirm.email)==false){
		window.alert("E-Mail Address is required.");
		frmConfirm.email.focus();}
	else{
		if (! isValidEmail(frmConfirm.email.value)){
			window.alert("E-mail address specified does not follow published standards.");
			frmConfirm.email.focus();}
		else{
			CheckPhoneInput();
		}
	}
}

function CheckPhoneInput(){
	if (isFilled(frmConfirm.phone)==false){
		window.alert("Telephone number is required.");
		frmConfirm.phone.focus();}
	else{
		if(isValidPhoneNumber(frmConfirm.phone.value)==false){
			window.alert("Telephone number is not valid.");
			frmConfirm.phone.focus();}
		else{
			if(frmConfirm.phone.value.length < 10){
				window.alert("Telephone number required ten or more digits.");
				frmConfirm.phone.focus();}
			else{
				CheckAddressInput();
			}
		}
	}		
}

function CheckAddressInput(){
	if (isFilled(frmConfirm.address1)==false){
		window.alert("Shipping address is required.");
		frmConfirm.address1.focus();}
	else{
		CheckCityInput();
	}		
}

function CheckCityInput(){
	if (frmConfirm.city.selectedIndex==0){
		window.alert("Shipping City is required.");
		frmConfirm.city.focus();}
	else{
		CheckStateInput();
	}		
}

function CheckStateInput(){
	if (isFilled(frmConfirm.state)==false){
		window.alert("Shipping State is required.");
		frmConfirm.state.focus();}
	else{
		CheckZipInput();
	}		
}

function CheckZipInput(){
	if (isFilled(frmConfirm.zip)==false){
		window.alert("Shipping Zip Code is required.");
		frmConfirm.zip.focus();}
	else{
		frmConfirm.submit();
	}		
}

function isFilled(strVal){
	if (strVal.value == "" || strVal.value == null){
		return false;}
	else {return true;}
}

function isValidPhoneNumber(sVal) {
	var numChars = "0123456789().- :;,extension";
	var IsNumber = true;
	var i;
	sVal = sVal.toLowerCase();
	for (i = 0; i < sVal.length; i++) 
		{ 
		var Char = sVal.charAt(i); 
		if (!(numChars.indexOf(Char) > -1))
			{
				IsNumber = false;
				break;
			}

		if (hasMultipleChar(sVal, "extension")) {
			return false;
		}
		if (hasMultipleChar(sVal, "x")) {
			return false;
		}
		if (hasMultipleChar(sVal, "ext")) {
			return false;
		}
	}
	return IsNumber;				   
}

function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    // check for valid characters - letters "A" to "Z", numbers 0 to 9, "@", dot ".", dash "-", and underscore "_"
    if (! allValidChars(email)) {  
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.indexOf(".") < 1) {  // dot must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (hasMultipleChar(email, "@")) {  // must not contain multiple @
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
		return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
		return false;
    }
    return isValidDomain(Mid(email, (email.lastIndexOf(".") + 1), 5));	// return true or false on valid domain
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

function isValidDomain(sDom) {
	//sDom - domain to be validated
	var arDomain = new Array("AC", "AD", "AE", "AERO", "AF", "AG", "AI", "AL", "AM", "AN", "AO", "AQ", "AR", "ARPA", "AS", "AT", "AU", "AW", "AX", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BIZ", "BJ", "BM", "BN", "BO", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CAT", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "COM", "COOP", "CR", "CU", "CV", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EDU", "EE", "EG", "ER", "ES", "ET", "EU", "FI", "FJ", "FK", "FM", "FO", "FR", "GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", "GM", "GN", "GOV", "GP", "GQ", "GR", "GS", "GT", "GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IM", "IN", "INFO", "INT", "IO", "IQ", "IR", "IS", "IT", "JE", "JM", "JO", "JOBS", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "MG", "MH", "MIL", "MK", "ML", "MM", "MN", "MO", "MOBI", "MP", "MQ", "MR", "MS", "MT", "MU", "MUSEUM", "MV", "MW", "MX", "MY", "MZ", "NA", "NAME", "NC", "NE", "NET", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "ORG", "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PRO", "PS", "PT", "PW", "PY", "QA", "RE", "RO", "RU", "RW", "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "ST", "SU", "SV", "SY", "SZ", "TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TP", "TR", "TRAVEL", "TT", "TV", "TW", "TZ", "UA", "UG", "UK", "UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI", "VN", "VU", "WF", "WS", "YE", "YT", "YU", "ZA", "ZM", "ZW");
	var IsValid = false;
	var i;

	for (i = 0; i < arDomain.length && IsValid == false; i++) 
		{ 
		var validDomain = arDomain[i];
		if (sDom.toLowerCase()==validDomain.toLowerCase()) 
			{
				IsValid = true;
				break;						
			}
	}
	return IsValid;				   	
}

function Mid(str, start, len){
	//str - the string to be parsed
	//start - starting position of parsing
	//len - how many characters from start to be retrieved

	// Ensure that start and len are within proper bounds
	if (start < 0 || len < 0) return "";

	var iEnd, iLen = String(str).length;
	if (start + len > iLen)
		iEnd = iLen;
	else
		iEnd = start + len;
	//Return the substring from start to start+len
	return String(str).substring(start,iEnd);
}

function hasMultipleChar(sVal, sChar){
	//if lastIndexOf any character > indexOf, the
	//value contains multiple occurences of the character
	if(sVal.lastIndexOf(sChar) > sVal.indexOf(sChar)){
		return true;}
	else{ return false;}
}

