function ValidateAndSubmit()
{
var errStrLcl = '';
sHTTP = 'https://';
document.frmACDCapture.action = sHTTP + 'app1.salesnet.com/api/in/XDC/XDCProcess.aspx';
errStrLcl += validateKeyIn('First Name',document.frmACDCapture.LeadNameFirst,'ONE',50,1,1,0,0,'N','|');
errStrLcl += validateKeyIn('Last Name',document.frmACDCapture.LeadNameLast,'ONE',50,1,1,0,0,'N','|');
errStrLcl += validateKeyIn('Company Name',document.frmACDCapture.LeadCompany,'ONE',100,1,1,0,0,'N','|');
errStrLcl += validateKeyIn('Job Title',document.frmACDCapture.LeadTitle,'ONE',100,1,0,0,0,'N','|');
errStrLcl += validateKeyIn('Main Phone',document.frmACDCapture.LeadPhone,'ONE',30,1,1,0,0,'N','|');
errStrLcl += validateKeyIn('Email',document.frmACDCapture.LeadEmail,'ONE',100,1,1,0,0,'E','|');
errStrLcl += validateKeyIn('Address 1',document.frmACDCapture.LeadAddr1,'ONE',150,1,0,0,0,'N','|');
errStrLcl += validateKeyIn('City',document.frmACDCapture.LeadCity,'ONE',50,1,1,0,0,'N','|');
errStrLcl += validatePicklist('State/Prov',document.frmACDCapture.LeadState,'ONE',0,false);
errStrLcl += validateKeyIn('Zip',document.frmACDCapture.LeadZip,'ONE',10,1,0,0,0,'N','|');
errStrLcl += validateKeyIn('Country',document.frmACDCapture.LeadCountry,'ONE',30,1,1,0,0,'N','|');
errStrLcl += validateKeyIn('Number of Employees',document.frmACDCapture.CDF381397,'ONE',20,3,0,0,0,' ','|');
errStrLcl += validateKeyIn('Number of Sales People',document.frmACDCapture.CDF368181,'ONE',20,3,1,0,0,' ','|');

if (errStrLcl != '')
{
alertErrMsg(errStrLcl);
errStrLcl = '';
}
else
{
setTimeout('document.frmACDCapture.submit()',1);
}
}

var validator_date_ValidMinDateStr = "1/2/1900";var validator_date_ValidMaxDateStr = "12/30/2078";var validator_date_ExampleDate = "M/d/yyyy";var validator_date_ValidMinDate = new Date(1900, 0, 2);var validator_date_ValidMaxDate = new Date(2078, 11, 30);var validator_date_TwoDigitMax = "2029";var validator_date_Century = "2000";var validator_date_TwoDigitsAllowed = false;var validator_date_MonthPosition = 1;var validator_date_DayPosition = 2;var validator_date_YearPosition = 3;var validator_date_DateRegExp = /^\D*(\d{1,2})\D+(\d{1,2})\D+(\d{4})\D*$/;var validator_date_StrictRegExpForDB = /(\d{1,2})\D+(\d{1,2})\D+(\d{4})/;
var validator_num_MaxInt = "2,147,483,648";var validator_num_MinInt = "-2,147,483,648";var validator_num_GroupingSym = ",";var vldt_regExp_decimal = /^(\d*)\.(\d*)$/;


var vldt_regExp_leading_ws = /^\s/;
var vldt_regExp_trailing_ws = /\s$/;
var vldt_regExp_ws = /^\s+$/;
var vldt_regExp_ws_gbl = /\s+/g;
var vldt_regExp_num = /^\d+$/;

function isDecimal(s,left,right,strObject)
{
var objectText = 'decimal';
if (isDecimal.arguments.length == 4)
{
objectText = strObject;
}
var returnVal = new Array(1);
s = stripWhitespace(s,1);
s = stripWhitespace(s,2);
returnVal[0] = '';
returnVal[1] = s;
if (isWhitespace(s))
return returnVal;
var negRet = isNegative(s);
s = negRet[1];
var numPart;
if (vldt_regExp_decimal.test(s))
{
numPart = vldt_regExp_decimal.exec(s);
if (numPart[1].length > left) 
returnVal[0]+= 'The item "' + returnVal[1] + '" has more than ' + left + ' digits to the left of the decimal symbol.\n\n';
if (numPart[2].length > right) 
returnVal[0]+= 'The item "' + returnVal[1] + '" has more than ' + right + ' digits to the right of the decimal symbol.\n\n';
}
else if (vldt_regExp_num.test(s))
{
numPart = vldt_regExp_num.exec(s);
if (numPart[0].length > left) 
returnVal[0]+= 'The item "' + returnVal[1] + '" has more than ' + left + ' digits to the left of the decimal symbol.\n\n';
}
else
{
returnVal[0] += 'The item "' + returnVal[1] + '" is not a valid ' + objectText + '.\n\n';
}
return returnVal;
}

function isCurrency(s, left, right)
{
var retVal = new Array(2);
retVal = isDecimal(s, left, right, 'currency');
return retVal;
}

function toDate(val)
{
var retDate = new Date(1900, 0, 1);
var okDate = isDate(val);
if (okDate[0] == '')
{
retDate = okDate[2];
}
return retDate;
}

function isDate(s)
{
var returnVal = new Array(2);
var errorcount = 0;
var errorstring = '';
s = stripWhitespace (s, 1);
s = stripWhitespace (s, 2); 
returnVal[1] = s;

if (isEmpty(s))
{
return returnVal;
}

var intMonth;
var intDay;
var intYear;
var matches = validator_date_DateRegExp.exec( s ); 

if ( !matches )
{
errorcount++;
errorstring+= 'The entry "' + returnVal[1] + '" was not entered in the required date format (' + validator_date_ExampleDate + ').\n\n';
returnVal[0] = errorstring;
return returnVal;
} 
else
{
intMonth = matches[validator_date_MonthPosition];
intDay = matches[validator_date_DayPosition];
intYear = matches[validator_date_YearPosition];

if (validator_date_TwoDigitsAllowed)
{
if ((intYear.length != 4) && (intYear.length != 2))
{
errorstring+= 'The entry "' + returnVal[1] + '" was not entered in the required date format (' + validator_date_ExampleDate + ').\n\n';
returnVal[0] = errorstring;
return returnVal;
}     

if (intYear.length == 2)    
{     
var tmpYear = 0;
tmpYear = parseFloat(intYear) + parseFloat(validator_date_Century);

if (tmpYear > validator_date_TwoDigitMax)
{
tmpYear = tmpYear - 100;       
}    
intYear = tmpYear + '';       
}
} 
}

var currDate = new Date(intYear, intMonth - 1, intDay);
returnVal[2] = currDate;
if ( parseFloat(intMonth) == currDate.getMonth()+1 && parseFloat(intDay) == currDate.getDate() && parseFloat(intYear) == currDate.getFullYear() )
{
if (errorstring == '') 
{   
if (currDate < validator_date_ValidMinDate)
{
errorstring = 'The entry "' + s + '" is less than the minimum allowable date of "' + validator_date_ValidMinDateStr + '".\n\n';
}
if (currDate > validator_date_ValidMaxDate)
{
errorstring = 'The entry "' + s + '" is greater than the maximum allowable date of "' + validator_date_ValidMaxDateStr + '".\n\n';
}
}
}
else
{
errorstring = 'The entry "' + returnVal[1] + '" is not a valid date (' + validator_date_ExampleDate + ').\n\n';         
}

returnVal[0] = errorstring;
returnVal[1] = s;
return returnVal;   
}

function isEmail (s)
{   
if (isEmpty(s)) 
if (isEmail.arguments.length == 1) return false;
else return (isEmail.arguments[1] == true);

if (isWhitespace(s)) return false;

var i = 1;
var sLength = s.length;
while ((i < sLength) && (s.charAt(i) != "@"))
{ i++
}

if ((i >= sLength) || (s.charAt(i) != "@")) return false;
else i += 2;

while ((i < sLength) && (s.charAt(i) != "."))
{ i++
}
if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
else return true;
}

function stripWhitespace (s, v)
{
switch (v)
{
case 1:
while (vldt_regExp_leading_ws.test(s))
{
s = s.replace(vldt_regExp_leading_ws, '');
}
break;
case 2:
while (vldt_regExp_trailing_ws.test(s))
{
s = s.replace(vldt_regExp_trailing_ws, '');
}
break;
case 3:
while (vldt_regExp_ws_gbl.test(s))
{
s = s.replace(vldt_regExp_ws_gbl, '');
}
break;
default:
break;
}
return s;
}

function isEmpty(s)
{   
return ((s == null) || (s.length == 0))
}

function isWhitespace (s)
{   
if (isEmpty(s)) return true;
return vldt_regExp_ws.test(s);
}

function validateMultiKeyIn(labelName,pickListItems,maxLen,dataType,isRequired,max_digs_left,max_digs_right,delim)
{
var itemArray = new Array();
var errorstring = '';
var itemLen = parseItemsToArray(pickListItems.value,itemArray);
if (isRequired == 1 && isWhitespace(pickListItems.value)) {
errorstring += labelName + ": This field cannot be empty.\n\n";
}
if ((itemLen >= 0)) {
stripLeadingAndTrailingSpaces(itemArray);
if (dataType != 1) 
{
maxLen = 255;
}
errorstring += checkItemLength(itemArray,maxLen,labelName);
errorstring += checkItemForDataType(itemArray,dataType,max_digs_left,max_digs_right,labelName);
errorstring += checkForDupe(itemArray,pickListItems,labelName);
errorstring += checkForDelim(itemArray,'|',labelName,false);
errorstring += checkForDelim(itemArray,delim,labelName,true);
}
var sortedOutput = itemArrayToVariable(itemArray);
pickListItems.value = sortedOutput;
return errorstring;
}

function checkForDupe(itemArray,fieldElement,labelName)
{
var errorstring = '';
var splitterStr = '~!)';
var placeHolder = new String('');
var tmpPlaceHolder = '';
var val1 = new String();
var val2 = new String();

for (var x = 0; x < itemArray.length; x++) {

for (var y = x + 1; y < itemArray.length; y++) {
val1 = itemArray[x] + '';
val2 = itemArray[y] + '';
if (val1.toLowerCase() == val2.toLowerCase()) {
tmpPlaceHolder = splitterStr + val1.toLowerCase() + splitterStr
if ((itemArray[x] != '') && (itemArray[y] != '') && (placeHolder.indexOf(tmpPlaceHolder) == -1)){
placeHolder += tmpPlaceHolder;
errorstring += labelName + ': Your list has more than one occurrence of the item "' + itemArray[y] + '".\n\n';
}
}
}
}
return errorstring;	
}

function parseItemsToArray(input,itemArray)
{
var output = '';
var j = 0;

sortedOutput = '';
itemArray[0] = '';

if ( input.length > 0 )
{
for (var i = 0; i < input.length; i++)
{
if ((input.charCodeAt(i) == 13) && (input.charCodeAt(i + 1) == 10))
{
j++;
i++;
itemArray[j] = '';
}
else
{
itemArray[j] += input.charAt(i);
}
}
}
else
{
if (isNaN(input.charCodeAt(i)) == false)
{
errorcount++;
errorstring+= 'You must add at least one item to your list to continue.\n\n';
}
}
return j;
}

function checkItemForDataType(itemArray, dataType, max_digs_left, max_digs_right,str_usage,labelName)
{
var errorstring = '';
var lclErr = '';

for (var x = 0; x < itemArray.length; x++) {

if (itemArray[x].length != 0){

switch (dataType) {
case 1:
if (str_usage == 'E')
{
if (! isEmail(itemArray[x]) || (containsExp(itemArray[x],/\s/, true))) {
errorstring += labelName + ': The item \"' + itemArray[x] + '\" is not a valid email (ex: a@b.com).\n\n';
}
}
break;

case 2:
lclErr = isDate(itemArray[x]);
itemArray[x] = lclErr[1] + '';
if (lclErr[0] != '') {
errorstring += labelName + ': ' + lclErr[0];
}
lclErr = '';
break;

case 3:
lclErr = isInteger(itemArray[x]);
itemArray[x] = lclErr[1] + '';
if (lclErr[0] != '') {
errorstring += labelName + ': ' + lclErr[0];
}
lclErr = '';
break;

case 4:
lclErr = isDecimal(itemArray[x], max_digs_left, max_digs_right);
itemArray[x] = lclErr[1] + '';
if (lclErr[0] != '') {
errorstring += labelName + ': ' + lclErr[0];
}
lclErr = '';
break;

case 5:
lclErr = isCurrency(itemArray[x], max_digs_left, max_digs_right);
itemArray[x] = lclErr[1] + '';
if (lclErr[0] != '') {
errorstring += labelName + ': ' + lclErr[0];
}
lclErr = '';
break;
}		
}
}
return errorstring;

}

function itemArrayToVariable(itemArray,blnEndBreak){
var sortedOutput = '';
for (var x = 0; x < itemArray.length; x++) {
if (itemArray[x] != '') {
if (sortedOutput != '') sortedOutput += '\n';
sortedOutput += itemArray[x];
}
}
if (blnEndBreak) sortedOutput += '\n';

return sortedOutput;
}

function stripLeadingAndTrailingSpaces(itemArray){

for (var x = 0; x < itemArray.length; x++) {
var str = itemArray[x];
str = stripWhitespace(str,1);
str = stripWhitespace(str,2);
itemArray[x] = str
}
}

function checkItemLength(itemArray,maxLength,labelName){
var errMsgVal = '';
var errorstring = '';
var currentItemLength = 0
maxLength -= 0;
for (var x = 0; x < itemArray.length; x++) {
currentItemLength = itemArray[x].length;
if ( currentItemLength > maxLength ) {
if (currentItemLength > 40) {
errMsgVal = itemArray[x].substring(0,40) + '...';
} else {
errMsgVal = itemArray[x];
}
errorstring += labelName + ': The item "' + errMsgVal + '"\nis ' + (currentItemLength - maxLength) + ' character';
if ((currentItemLength - maxLength) > 1) {
errorstring += 's';
}
errorstring += ' longer than the limit of ' + maxLength + ' character';
if (maxLength > 1) {
errorstring += 's';
}
errorstring += '.\n\n'
}
}
return errorstring;	
}


function isDigit (c)
{   
return vldt_regExp_num_char.test(c)
}

function stripCharsInBag (s, bag)
{   var i;
var returnString = '';
for (i = 0; i < s.length; i++)
{   
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}

function stripCharsNotInBag (s, bag)
{   var i;
var 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 checkForDelim(itemArray,delim,labelName,checkFrontAndBack)  {
var errorstring = '';
var delimMsg = '"' + delim + '"';
if (delim == '"')
{
delimMsg = 'double quotes';
}
else if (delim == "'")
{
delimMsg = 'single quotes';
}

if ( delim.length > 0  ) {
for (var x = 0; x < itemArray.length; x++) {
var errMsgVal = itemArray[x];
if (itemArray[x].length > 40) {
errMsgVal = itemArray[x].substring(0,40) + '...';
}
var intCharPos = itemArray[x].indexOf(delim);
if (intCharPos > -1)
{
if (intCharPos == 0 && checkFrontAndBack)
{
errorstring += labelName + ': The item "' + errMsgVal + '" may not begin with ' + delimMsg + '.\n\n';
}
else if (intCharPos == itemArray[x].length - delim.length && checkFrontAndBack)
{
errorstring += labelName + ': The item "' + errMsgVal + '" may not end with ' + delimMsg + '.\n\n';
}
else
{
errorstring += labelName + ': The item "' + errMsgVal + '" may not contain ' + delimMsg + '.\n\n';
}
}
if (delim == '--'){
if (itemArray[x].charAt(0) == '-') {
errorstring += labelName + ': The item "' + errMsgVal + '" may not begin with "-".\n\n';
}
if (itemArray[x].charAt(itemArray[x].length-1) == '-') {
errorstring += labelName + ': The item "' + errMsgVal + '" may not end with "-".\n\n';
}
}
}
}
return errorstring;
}

function isInteger(s)
{
var boolPosOnly = false;
if (isInteger.arguments.length == 2)
{
boolPosOnly = isInteger.arguments[1];
}
var returnVal = new Array(1);
s = stripWhitespace(s,1);
s = stripWhitespace(s,2);
returnVal[0] = '';
returnVal[1] = s;
if (isWhitespace(s))
return returnVal;
var negRet = isNegative(s);
s = negRet[1];

if (s.indexOf('+',0) == 0 && s.length > 1)
{
s = s.substr(1);
}

var numPart;
if (vldt_regExp_num.test(s))
{
if (boolPosOnly)
{
if (negRet[0])
{
returnVal[0] += 'The item "' + returnVal[1] + '" is not a valid positive integer. Please enter a positive whole number.\n\n';
return returnVal;
}
}

numPart = vldt_regExp_num.exec(s);
if (negRet[0])
{
numPart[0] = '-' + numPart[0];
}
if (parseInt(numPart[0]) > 2147483648)
{
returnVal[0] += 'The item "' + returnVal[1] + '" must be an integer less than ' + validator_num_MaxInt + '.\n';
}
if (parseInt(numPart[0]) < parseInt('-2147483648'))
{
returnVal[0] += 'The item "' + returnVal[1] + '" must be an integer greater than ' + validator_num_MinInt + '.\n';
}
}
else
{
if (boolPosOnly)
{
returnVal[0] += 'The item "' + returnVal[1] + '" is not a valid positive integer. Please enter a positive whole number.\n\n';
}
else
{
returnVal[0] += 'The item "' + returnVal[1] + '" is not a valid integer. Please enter a whole number.\n\n';
}
}
return returnVal;
}

function alertErrMsg(msg) {	
var commonMsg = 'The following errors occurred:\n___________________________________________________\n\n';
alert(commonMsg + msg);
}

function containsExp(strVal, Exp, bTrim) {
if (bTrim) {
strVal = stripWhitespace(strVal, 1);
strVal = stripWhitespace(strVal, 2);
}
if (strVal.match(Exp) == null) {
return false;
} else {
return true;
}
}

function isNegative(s)
{
var returnVal = new Array(1);
returnVal[0] = false;
returnVal[1] = s;
s = stripWhitespace(s,3);
s = stripCharsInBag(s,validator_num_GroupingSym);
if (isWhitespace(s))
{
return returnVal;
}
if (s.charAt(0) == '-')
{
returnVal[0] = true;
if (s.length > 1)
{
s = s.substr(1, s.length-1);
}
else
{
s = '';
}
}
else if (s.charAt(s.length-1) == '-')
{
returnVal[0] = true;
if (s.length > 1)
{
s = s.substr(0, s.length-1);
}
else
{
s = '';
}
}
else if (s.charAt(0) == '(' && s.charAt(s.length-1) == ')')
{
returnVal[0] = true;
if (s.length > 2)
{
s = s.substr(1, s.length-1);
s = s.substr(0, s.length-1);
}
else
{
s = '';
}
}
returnVal[1] = s;
return returnVal;
}

function checkItemRequiredLength(itemArray,minLength,labelName) {
var errorstring = '';
var boolFoundText = false;
for (var x = 0; x < itemArray.length; x++) {
if (itemArray[x].length > 0) {
boolFoundText = true;
if ( itemArray[x].length < minLength ) {
errorstring += labelName + ': The item "' + itemArray[x] + '" must be at least ' + minLength + ' characters long.\n\n'
}
}
}
if (! boolFoundText) {
if (minLength > 1) {
errorstring += labelName + ': This field must be at least ' + minLength + ' characters long.\n\n'
} else {
errorstring += labelName + ': This field cannot be empty.\n\n'
}
}
return errorstring;	
}

function validateKeyIn(labelName, fieldElement, valCardinal, maxLen, dataType, minLength, max_digs_left, max_digs_right, str_usage, delim){
labelName = HTMLDecode(labelName);
var itemArray = new Array();
var errorstring = '';
if (valCardinal == 'MNY') {
var itemLen = parseItemsToArray(fieldElement.value,itemArray);
} else {
var itemLen = 0;
itemArray[0] = fieldElement.value;
}
if (maxLen == 0) { maxLen = 80000; }
if ((itemLen >= 0)) {
stripLeadingAndTrailingSpaces(itemArray);
if ((dataType != 1) && (maxLen > 255)) {
maxLen = 255;
}
if (minLength > 0) {
errorstring += checkItemRequiredLength(itemArray,minLength,labelName);
}
errorstring += checkItemLength(itemArray,maxLen,labelName);
errorstring += checkItemForDataType(itemArray,dataType,max_digs_left,max_digs_right,str_usage,labelName);
errorstring += checkForDupe(itemArray,fieldElement,labelName);
if ((maxLen > 0) && (maxLen < 2000)) {
// don't allow pipes for non-note fields
errorstring += checkForDelim(itemArray,'|',labelName, false);
}
errorstring += checkForDelim(itemArray,delim,labelName, true);
}
var sortedOutput = itemArrayToVariable(itemArray);
fieldElement.value = sortedOutput;
return errorstring;
}


function validatePicklist(fieldLabel, formElement, valCardinal, isRequired, isCDF) {
var listElement;
var returnString = '';

if (valCardinal == 'MNY') {
var elementListLength = formElement.length;
for (var x = 0; x < elementListLength; x++)
{
listElement = formElement.options[x];
if (listElement.selected == true)
{
if ((listElement.value == '') || ((listElement.value == -1) && (! isCDF)))
{
listElement.selected = false;
}
}
}
if (isRequired > 0)
{
if (formElement.selectedIndex == -1)
{
returnString = HTMLDecode(fieldLabel) + ': You must select at least one item from this list.\n\n';
}
}
} else {
if (isRequired > 0) {
if ((formElement.selectedIndex == -1) || isWhitespace(formElement.options[formElement.selectedIndex].value) || ((formElement.options[formElement.selectedIndex].value == -1) && (! isCDF)))
{
returnString = HTMLDecode(fieldLabel) + ': You must select an item from this list.\n\n';
}
}
}
return returnString;
}

function HTMLDecode(strValue) {
var MyString = new String (strValue)
MyString = MyString.replace(/&lt;/g,'<');
MyString = MyString.replace(/&gt;/g,'>');
MyString = MyString.replace(/&quot;/g,'"');
MyString = MyString.replace(/&amp;/g,'&');	
return MyString;
}
