//------------------Value Checking Functions ------------------/

function isValidRadio(oElement, message){
    // set var radio_choice to false
    var radio_choice = false;

    // Loop from zero to the one minus the number of radio button selections
    for (counter = 0; counter < oElement.length; counter++)
    {
      // If a radio button has been selected it will return true
      // (If not it will return false)
      if (oElement[counter].checked)
        radio_choice = true; 
    }

    if (!radio_choice)
    {
      // If there were no selections made display an alert box 
      alert(message)
      return (false);
    }
    return (true);
}

//Validate that a checkbox is checked
function isValidRequiredCheckbox(oElement, name) {
	
	if (oElement.checked == false) {
		alert (name + " must be checked.");
		return (false);
	} else {
		return (true);
	}

}

//do not allow spaces or semicolons in input to project against sql injection errors
function isSafeValue(oElement, name) {

    var isValid = true;
    var str = Trim(oElement.value);
    for (var i=0; i<str.length; i++) {
	if ( (str.charAt(i)==" ") || (str.charAt(i)==";") ) {
		alert("The "+name+" you entered is not valid.");
		isValid = false;
    	}
    }
    return isValid;
}

function isValidCreditCard(cardNumber, cardType, fieldName)
{

  var isValid = false;
  var ccCheckRegExp = /[^\d ]/;
  isValid = !ccCheckRegExp.test(cardNumber);

  if (isValid)
  {
    var cardNumbersOnly = cardNumber.replace(/ /g,"");
    var cardNumberLength = cardNumbersOnly.length;
    var lengthIsValid = false;
    var prefixIsValid = false;
    var prefixRegExp;

    switch(cardType)
    {
     //mastercard 
     case "3":
        lengthIsValid = (cardNumberLength == 16);
        prefixRegExp = /^5[1-5]/;
        break;
	
      //visa
      case "2":
        lengthIsValid = (cardNumberLength == 16 || cardNumberLength == 13);
        prefixRegExp = /^4/;
        break;
        
	  //amex
      case "4":
        lengthIsValid = (cardNumberLength == 15);
        prefixRegExp = /^3(4|7)/;
        break;
	
      //amex
      case "5":
        lengthIsValid = (cardNumberLength == 16);
        prefixRegExp = /^(6011|65)/;
        break;
        
      default:
        prefixRegExp = /^$/;
        alert("Card type not found");
    }

    prefixIsValid = prefixRegExp.test(cardNumbersOnly);
    isValid = prefixIsValid && lengthIsValid;
  }

  if (isValid)
  {
    var numberProduct;
    var numberProductDigitIndex;
    var checkSumTotal = 0;

    for (digitCounter = cardNumberLength - 1; 
      digitCounter >= 0; 
      digitCounter--)
    {
      checkSumTotal += parseInt (cardNumbersOnly.charAt(digitCounter));
      digitCounter--;
      numberProduct = String((cardNumbersOnly.charAt(digitCounter) * 2));
      for (var productDigitCounter = 0;
        productDigitCounter < numberProduct.length; 
        productDigitCounter++)
      {
        checkSumTotal += 
          parseInt(numberProduct.charAt(productDigitCounter));
      }
    }

    isValid = (checkSumTotal % 10 == 0);
  }

  if (isValid) return true;
  else {
  	alert("The credit card number you entered is not valid. Please check the card type and number.");
  	return false;
  }

}

function isValidCreditCardNoType(cardNumber, fieldName) {

    var cardNumbersOnly = cardNumber.replace(/ /g, "");
    var cardNumberLength = cardNumbersOnly.length;
    
    var isValid = false;
    var ccCheckRegExp = /[^\d ]/;
    isValid = !ccCheckRegExp.test(cardNumber);

    if (isValid) {
        if (cardNumberLength == 16 || cardNumberLength == 15 || cardNumberLength == 14 || cardNumberLength == 13) {
            isValid = true;
        }
        else {
            isValid = false;
        }
    }

    if (isValid) {
        var numberProduct;
        var numberProductDigitIndex;
        var checkSumTotal = 0;

        for (digitCounter = cardNumberLength - 1; digitCounter >= 0; digitCounter--) {
            checkSumTotal += parseInt(cardNumbersOnly.charAt(digitCounter));
            digitCounter--;
            numberProduct = String((cardNumbersOnly.charAt(digitCounter) * 2));
            for (var productDigitCounter = 0; productDigitCounter < numberProduct.length; productDigitCounter++) {
                checkSumTotal += parseInt(numberProduct.charAt(productDigitCounter));
            }
        }

        isValid = (checkSumTotal % 10 == 0);
    }

    if (isValid) return true;
    else {
        alert("The credit card number you entered is not valid. Try retyping the number exactly as it is shown on your credit card (without spaces or dashes).  If you feel this is an error on our end, or would like to pay by phone, contact us at 1-800-780-6064 or billing@flyingcart.com");
        return false;
    }

}

function isValidPhone(value,country)
{
	/*if (country=='223') var part = new RegExp("^[0-9]{10}$");
	else var part = new RegExp("^[0-9]{9-20}$");
	if (part.test(value)) return true;
	else 
	{
		alert("Please enter a valid phone number. It must be 9-20 numeric digits and may not contain any spaces, dashes, or any other characters.");
		return false;
	}*/
	return true;
}

function isValidCvv2(value,cardType) {
  
  var cvv2Length = value.length;
  var justDigits = new RegExp("^[0-9]{3,4}$");
  var lengthIsValid = false;
  
  switch(cardType)
    {
     //amex 
     case "4":
        lengthIsValid = (cvv2Length == 4);
        break;
	
      default:
        lengthIsValid = (cvv2Length == 3);
    }
  prefixIsValid = justDigits.test(value);
  isValid = prefixIsValid && lengthIsValid;
    
  if (isValid) {
    return true;
  } else {
    alert("The security code you entered is not valid.");
    return false;
  }     

}

function isValidPWVerify(pw1, pw2) {
    if (Trim(pw1.value) != Trim(pw2.value)) {
        alert("The new Passwords do not match:  'Password' must equal 'Confirm Password'");
	  	return false;
  	}
  	else return isValidPWChars(pw1.value, 5, 100, "Password");
}

function isValidPWChars(value, minlength, maxlength, label) {
    var expr = "^[a-zA-Z0-9_-]{" + minlength + "," + maxlength + "}$";
    var reg = new RegExp(expr);
    if (reg.test(value)) {
        return true;
    } else {
        alert("The " + label + " must be between " + minlength + " and " + maxlength + " alphanumeric characters. No spaces are allowed.");
        return false;
    }
}

function isValidZipCode(value, label) {
    var isValid = true;
    var trimVal = Trim(value);
    if (trimVal.length < 3 || trimVal.length > 10) { isValid = false; }
    if (isValid == false) {
        alert("A valid " + fieldName + " is required.");
        return false;
    }
    else {
        return true;
    }
}

function isValidReferredBy(value) {
    var isValid = true;
    var trimVal = Trim(value);
    if (trimVal.length == 0) { isValid = false; }
    if (isValid == false) {
        alert("Please select how you heard about us (from the dropdown box in Step 1).");
        return false;
    }
    else {
        return true;
    }
}

function isValidEmailAddress(value, label) {
  var reg = new RegExp("^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$");
  if (reg.test(value)) {
    return true;
  } else {
    alert("Please enter a valid " + label + ".");
		return false;
  }
}

function isValidLinkText(value) {
  if (value.length <= 20) {
    	return true;
  } else {
    	alert("The link text must be 20 characters or less.");
		return false;
  }
}

function isValidLinkURL(value) {
  var reg = new RegExp("^(http://).");
  if (reg.test(value)) {
    	return true;
  } else {
    	alert("The link URL must begin with http://");
	return false;
  }
}

function isNumber(value,label) {
  var reg = new RegExp("^[0-9]");
  if (reg.test(value)) {
    	return true;
  } else {
    	alert("Please enter a valid " + label + ". " + label + " can not be negative.");
	return false;
  }
}

function isValidFilename (file,label) {
  /*var WinFile = new RegExp("^([a-zA-Z]\:|\\)\\([^\\]+\\)*[\/0-9A-Za-z_-|]+\.(gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG)$");
  var MacFile = new RegExp("^(/)*([^\/]+\/)*[\/0-9A-Za-z_-|]+\.(gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG)$");
  if ( WinFile.test(file.value) || MacFile.test(file.value) ) {
  	return true;
  } else {
        alert(label + " is not valid. Filenames may not contain spaces or punctuation marks, and the file must be in gif, jpg or png format.");
        return false;
  }*/
  return true;

}

function isValidFilenameTxt (file,label) {
  var reg = new RegExp("^(.*)?[\/|\\][0-9A-Za-z_]+\.(txt|TXT)$");
  if (reg.test(file.value)) {
        return true;
  } else {
        alert(label + " is not valid. Filenames may not contain spaces or punctuation marks, and the file must be in txt format.");
        return false;
  }

}

function isValidDomain(value) {
  var reg = new RegExp("^[A-Za-z0-9]{5,25}$");
  var reg2 = new RegExp("^(help|admin|brian|margo|hq|dev|staging|rishi|jon|www)$");
  if (reg.test(value) && !reg2.test(value)) {
    return true;
  } else {
    alert("The web address must be 5-25 characters, and it may only contain letters and digits 0-9.");
                return false;
  }     

}

function isValidPaypalUsername(value, label) {
  var reg = new RegExp("^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$");
  if (reg.test(value)) { 
    return true;
  } else {
    alert("Please enter a valid " + label + ". PayPal usernames are in email format (ex. name@gmail.com).");
                return false;
  } 
}

function isValidCustomDomain(domain) 
{
  var reg = new RegExp("^(http://)?(www.)?(([a-zA-Z0-9_-]+)[\.]?)([a-zA-Z0-9_-]+)[\.]([A-Za-z0-9_-]{2,4})$");
  var reg2 = new RegExp("^(.*)(.flyingcart.com)$");
  if (reg.test(domain.value))
  {
  	if (reg2.test(domain.value)) {
		alert("If you would like to use a different Flying Cart domain, please contact us or set up a new store.");
		return false;
	} else { return true;}  
  }
  else 
  {
		alert("The domain you entered is not valid.");
		return false;
  }
}

function weightIsOver70(value) {
  if (value>70) {
	alert("USPS will not ship packages that are over 70 lbs.");
  }
}
function isValidPriceOrWeight(value, label) {
  var reg = new RegExp("^([0-9]+)?(?:\.[0-9]{0,2})?$");
  //var reg2 = new RegExp("^[0]+(.[0]+)$");	
  if (reg.test(value)) {
    	/*if (!reg2.test(value)) 
    	{
			return true;
		} 
		else 
		{
			alert(label + " must be greater than zero.");
			return false;
		}*/
    	return true;  
  } else {
    	alert("Please enter a valid " + label + ".");
	return false;
  }
}

function isValidShippingPrice(value, label) {
  var reg = new RegExp("^([0-9]+)?(?:\.[0-9]{0,2})?$");
  if (reg.test(value)) {
        return true;
  } else {
        alert("Please enter a valid " + label + ".");
        return false;
  }
}

function isValidTax(value, label) {
  var reg = new RegExp("^[0-9]+(?:\.[0-9]{1,4})?$");
  if (reg.test(value)) {
 	return true;
  } else {
    	alert("Please enter a valid " + label + ".");
	return false;
  }
}

function isValidAlphanumeric(value, minlength, maxlength, label) {
  var expr = "^\\w{"+minlength+","+maxlength+"}$";
  var reg = new RegExp(expr);
  if (reg.test(value)) {
 	return true;
  } else {
    	alert("The " + label + " must be between " + minlength + " and " + maxlength + " alphanumeric characters. No spaces are allowed.");
	return false;
  }
}

function isValidFanNickname(value, minlength, maxlength, label) {
    var expr = "^[a-zA-Z0-9.-]{" + minlength + "," + maxlength + "}$";
    var reg = new RegExp(expr);
    if (reg.test(value)) {
        return true;
    } else {
        alert("The " + label + " must be between " + minlength + " and " + maxlength + " alphanumeric characters. No spaces are allowed.");
        return false;
    }
}

function isValidRequiredColor(oElement, fieldName){
    var isValid = true;
    if (Trim(oElement.value) == ""){ isValid = false; }
    if (isValid == false){
        alert(fieldName + " is required.");
        oElement.focus();
    }
    else
    {
    	var reg = new RegExp("^(#)[0-9a-fA-F]{6}$");
    	if (reg.test(oElement.value)) { return true;
  		} else {
        	alert("Please enter a valid " + fieldName + ".");
        	return false;
  		}
    }
    return isValid;
}

//--------- Utility Functions ----------/
function Trim(TRIM_VALUE){
    if(TRIM_VALUE.length < 1){
        return"";
    }
    TRIM_VALUE = RTrim(TRIM_VALUE);
    TRIM_VALUE = LTrim(TRIM_VALUE);
    if(TRIM_VALUE==""){
        return "";
    }else{
        return TRIM_VALUE;
    }
} //End Function

function RTrim(VALUE){
    var w_space = String.fromCharCode(32);
    var v_length = VALUE.length;
    var strTemp = "";
    if(v_length < 0){
        return"";
    }
    var iTemp = v_length -1;

    while(iTemp > -1){
        if(VALUE.charAt(iTemp) == w_space){
        }else{   
            strTemp = VALUE.substring(0,iTemp +1);
            break;
        }
        iTemp = iTemp-1;
    } //End While
    return strTemp;
} //End Function

function LTrim(VALUE){
    var w_space = String.fromCharCode(32);
    if(v_length < 1){
        return"";
    }
    var v_length = VALUE.length;
    var strTemp = "";

    var iTemp = 0;

    while(iTemp < v_length){
        if(VALUE.charAt(iTemp) == w_space){
        }else{
            strTemp = VALUE.substring(iTemp,v_length);
            break;
        }
        iTemp = iTemp + 1;
    } //End While
    return strTemp;
} //End Function

function isValidRequired(oElement, fieldName){
    var isValid = true;
    if (Trim(oElement.value) == ""){ isValid = false; }
    if (isValid == false){
        alert(fieldName + " is required.");
        oElement.focus();
    }
    return isValid;
}
  
function isValidRequiredSelect(oElement, fieldName){
    var isValid = true;
    if (oElement.selectedIndex == 0) isValid = false;
    if (isValid == false) {
	alert(fieldName + " is required.");
	oElement.focus();
    }
    return isValid;
}

function isSelected(oElement, fieldName) {
    if (oElement.selectedIndex==0) return false;
    else return true;
}

function jsMessage(oElement, message){
    alert(message);
    oElement.focus();
    return false;
}
