// Generic Functions And Global Variables

var errorFree = true;
var theError = "You have entered in the following information improperly:\n";
var submitted = false;

//  List of generic functions
/*
    strip_commas(form.field);
    HeightInInches(form.feet, form.inches, form.height);
    req_text(form.field, "");
    req_phone_3_fields(form.ac, pre, num, "");
    req_number(form.field, "");
    opt_number(form.field, "", zeroed);
    req_number_length(form.field, length, "");
    req_one_number(form.field1, form.field2, "");
    opt_number_length(form.field, length, "");
    checkbox_validator(form, "box", "");
    req_combo(form.combo, index, "");
    req_text_w_combo(form.combo, index, field, "");
    req_number_w_combo(form.combo, index, field, "");
    req_regexp(form.field, exp_text, "");
    finish_Validation (form.theForm);
*/


//Removes Commas from numeric fields
function strip_commas(field) {
    if(field.value.length != 0) {
	re = /[,$]/gi;
	str = field.value;
	field.value = str.replace(re, "");
    }
}


function HeightInInches(feet, inches, height) {
    if(!isNaN(feet.value) && !isNaN(inches.value)) {
	height.value = (feet.value * 12) + inches.value;
    }
}


function req_text(field, msg) {

    if(field.value.length == 0) {
	addMsg(field, msg);
    }
}


function req_phone_3_fields(ac, pre, num, msg) {
    if(ac.value.length != 3 || isNaN(ac.value)) {
	addMsg(ac, msg);
    }
    else if(pre.value.length != 3 || isNaN(pre.value)) {
	addMsg(pre, msg);
    }
    else if(num.value.length != 4  || isNaN(num.value)) {
	addMsg(num, msg);
    }
}


function req_number(field, msg) {

    strip_commas(field);
    if(field.value.length == 0 || isNaN(field.value)) {
	addMsg(field, msg);
    }
}


function req_number_amt(field, amt, msg) {

    strip_commas(field);
    if(field.value.length == 0 || isNaN(field.value)) {
	addMsg(field, msg);
    }
    else if(field.value <= amt) {
	addMsg(field, msg + " Must be greater than: " + amt);
    }
}


function req_one_number(field1, field2, msg) {

    strip_commas(field1);
    strip_commas(field2);
    if((field1.value.length == 0 || isNaN(field1.value)) && (field2.value.length == 0 || isNaN(field2.value))) {
	addMsg(field1, msg);
    }
}


function opt_number(field, msg, zeroed) {

    strip_commas(field);
    if(field.value.length != 0 && isNaN(field.value)) {
	addMsg(field, msg + " (not required)");
    }
    else if(field.value.length == 0 && zeroed) {
	field.value = 0;
    }
}


function req_number_length(field, length, msg) {

    strip_commas(field);
    if(field.value.length != length || isNaN(field.value)) {
	addMsg(field, msg);
    }
}


function opt_number_length(field, length, msg) {

    strip_commas(field);
    if(field.value.length != 0 && (field.value.length != length || isNaN(field.value))) {
	addMsg(field, msg + " (not required)");
    }
}


function checkbox_validator(form, box, msg)
{
    if(isNaN(form.elements[box].length)) {
	if(form.elements[box].checked) {
	    return true;
	}
	if(errorFree) {
	    form.elements[box].focus();
	}
    }
    else {
	for(var i = 0; i < form.elements[box].length; i++) {
	    if(form.elements[box][i].checked) {
		return true;
	    }
	}
	if(errorFree) {
	    form.elements[box][0].focus();
	}
    }

    addMsg(form.elements[box][0], msg);
    return false;
}


//Checks if a certain combo option is selected and throws an error if it is
function req_combo(combo, index, msg) {

    if(combo.options[index].selected) {
	addMsg(combo, msg);
    }
}


//Checks a text field if a certain combo option is selected
function req_text_w_combo(combo, index, field, msg) {

    if(combo.options[index].selected && field.value.length == 0) {
	addMsg(field, msg);
    }
}


//Checks a numeric field if a certain combo option is selected
function req_number_w_combo(combo, index, field, msg) {

    strip_commas(field);

    if(combo.options[index].selected && (field.value.length == 0 || isNaN(field.value))) {
	addMsg(field, msg);
    }
}

//URL   "http://.+\..+\..+"
//Email ".+@.+\..+"
function req_regexp(field, exp_text, msg) {

    req_regexpRE = new RegExp(exp_text);
    if(!req_regexpRE.test(field.value)) {
	addMsg(field, msg);
    }
}

//Functions for validate Date format
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var dtCh= "/";
	var minYear=1900;
	var maxYear=2100;
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}

function req_date(field, msg){	
	if (field.value.length != 0 && isDate(field.value)==false){
		addMsg(field, msg);
	}
 }
//End of Validate Date format

function addMsg(control, msg) {
    theError += "\n\t-" + msg;
    if(errorFree) {
	control.focus();
    }
    errorFree = false;
}


function finish_Validation (theForm) {

   if(!errorFree)
	{
	    alert(theError);
	    return false;
	}
    else if(!submitted)
	{
	    //submitted = true;
	    //theForm.submit();
	    return true;
	}
   return false;
}

// Form Validator script
function ChkCCCInfoReq(form) {

    errorFree = true;
    theError = "You have entered in the following information improperly:";

    req_text(form.username, "Your Client ID");
    req_text(form.password, "Password");
	
	var str = form.username.value.toLowerCase();
	if(str.match("admin") == null) {
		req_number(form.username, "Client ID must be numeric");
    }

    return finish_Validation(form);
    //return false;
}

// Form Validator script
function ChkCCCACInfoReq(form) {

    errorFree = true;
    theError = "You have entered in the following information improperly:";

    req_text(form.clientName, "Name");
	req_text(form.EMail, "E-mail Address");
	req_regexp(form.EMail, ".+@.+\\..+", "E-mail Address format");
	checkbox_validator(form, "document", "At least one Document Type");	
	
	if(form.newPwd.value.length != 0 && form.currentPwd.value.length == 0) {
		addMsg(form.confirmPwd, "Current Password");
    }
	
	if(form.newPwd.value.length != 0 && form.confirmPwd.value.length == 0) {
		addMsg(form.confirmPwd, "Confirm Password");
    }
	
	if(form.newPwd.value.length == 0 && form.confirmPwd.value.length != 0) {
		addMsg(form.newPwd, "New Password");
    }

    return finish_Validation(form);
    //return false;
}


// Form Validator script
function ChkCCCStopRollingInfoReq(form) {

    errorFree = true;
    theError = "You have entered in the following information improperly:";
 
    req_text(form.StopRollingReason, "Reason");  


    return finish_Validation(form);
    //return false;
}

// Form Validator script
function ChkCCCRpcmtInfoReq(form) {

    errorFree = true;
    theError = "You have entered in the following information improperly:";

    req_text(form.leadId, "Lead ID");  
    req_text(form.replacementReason, "Reason");  
	
	req_number(form.leadId, "Lead ID must be numeric");	
	var str = form.replacementReason.value.toLowerCase();
	if(str.match("bogus") != null) {
	addMsg(form.replacementReason, "Reason cannot contains the word BOGUS");
    }

    return finish_Validation(form);
    //return false;
}


// Form Validator script
function ChkCCCIncLeadsInfoReq(form, amount) {

    errorFree = true;
    theError = "You have entered in the following information improperly:"; 
	
	amount = amount - 1;	
	checkbox_validator(form, "newStartTime", "When you want to start?");  
	req_number_amt(form.newNumLeads, amount, "New number of leads");

    return finish_Validation(form);
    //return false;
}

// Form Validator script
function ChkCCCAdminLookup(form) {

    errorFree = true;
    theError = "You have entered in the following information improperly:";
	
	if((form.clientName.value.length == 0) && (form.clientId.value.length == 0) && (form.clientEmail.value.length == 0)) {
	addMsg(form.clientName, "You must enter at least one value in order to query user information");
    }
	
	if(form.clientId.value.length != 0)
	{	
	    req_number(form.clientId, "Client Id must be numeric");
	}


    return finish_Validation(form);
    //return false;
}

// Form Validator script
function ChkCCCResendBatchReq(form) {

    errorFree = true;
    theError = "You have entered in the following information improperly:";

    req_combo(form.batchId, 0, "Select Batch");	
	//req_number(form.batchId, "Batch ID must be numeric");

    return finish_Validation(form);
    //return false;
}

// Form Validator script
function ChkCCCAResendBatchReq(form) {

    errorFree = true;
    theError = "You have entered in the following information improperly:";

    req_text(form.ClientId, "Client ID");	
	req_number(form.ClientId, "Client ID must be numeric");
	req_text(form.BatchId, "Batch ID");	
	req_number(form.BatchId, "Batch ID must be numeric");

    return finish_Validation(form);
    //return false;
}

// Form Validator script
function ChkCCCAdminTerminateInfoReq(form) {

    errorFree = true;
    theError = "You have entered in the following information improperly:";
 
    req_text(form.StopRollingReason, "Reason");  
	req_combo(form.dispositions, 0, "Disposition");

    return finish_Validation(form);
    //return false;
}

// Form Validator script
function ChkCCCRollingRepInfoReq(form) {

    errorFree = true;
    theError = "You have entered in the following information improperly:";
 
    req_text(form.TerReportEmail, "Email Address"); 
	req_regexp(form.TerReportEmail, ".+@.+\\..+", "E-mail Address format");
	req_text(form.TerReportFrom, "Report From Date"); 
	req_text(form.TerReportTo, "Report To Date"); 
	req_date(form.TerReportFrom, "Valid Report From Date");
	req_date(form.TerReportTo, "Valid Report To Date");

    return finish_Validation(form);
    //return false;
}

// Form Validator script
function ChkCCCOrderCountiesInfoReq(form, amount) {

    errorFree = true;
    theError = "You have entered in the following information improperly:";
 
 	req_text(form.SelectedCounties, "Select At least one County");
	amount = amount -1;
	req_number_amt(form.EstAvbLeads, amount, "Est. Available Leads");
 

    return finish_Validation(form);
    //return false;
}

// Form Validator script
function ChkCCCPwdReq(form) {

    errorFree = true;
    theError = "You have entered in the following information improperly:";
 
    req_text(form.email, "Email Address"); 
	req_regexp(form.email, ".+@.+\\..+", "E-mail Address format");
 

    return finish_Validation(form);
    //return false;
}