﻿String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}

/* Client-side access to querystring name=value pairs
	Version 1.3
	28 May 2008
	
	License (Simplified BSD):
	http://adamv.com/dev/javascript/qslicense.txt
*/
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = {};
	
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}


function tooLong(txtBox, iLength, errorSpan) {
    var errorTxt = "";
    
    if (txtBox.value.length > iLength) {
        txtBox.style.background = 'Yellow';
        errorTxt = "Must be less than " + iLength.toString() + " characters long.";
    }
    errorSpan.innerHTML = errorTxt;
}
function pReq(pList){
	if(pList.value == "-1"){
		alert("Make a selection in drop down.");
		pList.style.backgroundColor = 'Yellow';
		return false;
	}
	else{
		pList.style.border = 'White';
		return true;
	}
}
function cReq(cBox){
if(cBox.checked){
cBox.style.backgroundColor = 'White';
return true;
}
cBox.style.backgroundColor = 'Yellow';
alert("Must select checkbox");
return false;
}
function rReq(rBut1, rBut2){
	if(rBut1.checked == false && rBut2.checked == false){
	alert("Must make a selection.");
	rBut1.style.backgroundColor = 'Yellow';
	rBut2.style.backgroundColor = 'Yellow';
	return false;
	}
	rBut1.style.backgroundColor = 'White';
	rBut2.style.backgroundColor = 'White';
	return true;
}
function integerCheck(txtBox, iMinValue, iMaxValue, errorSpan) {
    var errorTxt = "";

    if ( iMinValue > iMaxValue ) {
	var iHold = iMinValue;
	iMinValue = iMaxValue;
	iMaxValue = iHold;
    }
    txtBox.style.background = 'White';

    if (txtBox.value.length > 0) {
        var numEx = new RegExp("^[-+]?[0-9]*$");
        if (txtBox.value.match(numEx)) {
            var iVal = parseInt(txtBox.value, 10);
            if (iVal > iMaxValue) {
                txtBox.style.background = 'Yellow';
                errorTxt += "The Maximum Value allowed is: " + iMaxValue.toString() + "\n";
            }
            if (iVal < iMinValue) {
                txtBox.style.background = 'Yellow';
                errorTxt += "The Minimum Value allowed is: " + iMinValue.toString() + "\n";
            }
        }
        else {
            txtBox.style.background = 'Yellow';
            errorTxt += "Must be an integer value.\n";
        }
    }

    if(errorTxt != ""){
	alert(errorTxt);
	txtBox.value = "";
    }
}
function floatCheck(txtBox, iMinValue, iMaxValue, iPrecision, errorSpan) {
    var errorTxt = "";

    if (iMinValue > iMaxValue) {
        var iHold = iMinValue;
        iMinValue = iMaxValue;
        iMaxValue = iHold;
    }

    txtBox.style.background = 'White';

    if (txtBox.value.length > 0) {
        var numEx = new RegExp("^[-+]?[0-9]*[.]?([0-9]*)$");
        if (txtBox.value.match(numEx)) {
            var fVal = parseFloat(txtBox.value);
            var precExp = new RegExp("^[-+]?[0-9]*[.]?([0-9]{0," + iPrecision.toString() + "})$");
            if (txtBox.value.match(precExp)) {
                if (fVal > iMaxValue) {
                    errorTxt += "The Maximum Value allowed is: " + iMaxValue.toString() + "\n";
                }
                if (fVal < iMinValue) {
                    errorTxt += "The Minimum Value allowed is: " + iMinValue.toString() + "\n";
                }
            }
            else {
                errorTxt += "Value can only have " + iPrecision.toString() + " points of precision.\n";
            }
        }
        else {
            errorTxt += "Value must be a valid floating point number.\n";
        }
    }

    if (errorTxt != "") {
        txtBox.style.background = 'Yellow';
        alert(errorTxt);
        txtBox.value = "";
    }
}

function checkEmail(txtBox) {
    var emailEx = new RegExp("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$");

    if (!txtBox.value.match(emailEx)) {
        txtBox.value = "";
        txtBox.style.background = 'Yellow';
        
    }
    else {
        txtBox.style.background = 'White';
    }
}
function checkUrl(txtBox) {
    var urlEx = new RegExp("(http://)|(https://)(.*)");
    if (!txtBox.value.match(urlEx)) {
        txtBox.value = "http://" + txtBox.value;
    }
}

function fileChecker() {
	var f = document.getElementById('datafile');
	
	if (f && f.value) {
		document.form1._has_file.value = 'true';
	}
}

function generateCaptcha(first, second) {
	try{
		first.innerHTML = Math.floor(Math.random() * 10);
		second.innerHTML = Math.floor(Math.random() * 10);
	}
	catch(e){
	}
}
        function checkCaptcha(first, second, answer) {
            try {
                var iFirst = parseInt(first.innerHTML, 10);
                var iSecond = parseInt(second.innerHTML, 10);
                var iAnswer = parseInt(answer.value, 10);

                var realAnswer = iFirst + iSecond;
                if (realAnswer != iAnswer) {
                    answer.value = "";
                    answer.style.background = 'Yellow';
                    alert('Wrong answer to Captcha');
                    generateCaptcha(first, second);
                    return false;
                }
                else {
                    answer.style.background = 'White';
                    return true;
                }
            }
            catch (e) {

            }
        }
function DateCheck(txtBox) {
    var errorTxt = "";

    var dateTimeRegEx = RegExp("^(([0]?[1-9]{1,1})|([1]{1,1}[1-2]{1,1}))[-|/]{1,1}(([0]?[1-9]{1,1})|([1]{1,1}[0-9]{1,1})|([2]{1,1}[0-9]{1,1})|([3]{1,1}[0-1]{1,1}))[-|/]{1,1}(((18)|(19)|((2)[0-9]))[0-9][0-9])?$");
    if (txtBox.value.trim().match(dateTimeRegEx)) {
        var dateArray = new Array();
        dateArray = txtBox.value.split('/');
        if (dateArray.length == 1) {
            dateArray = txtBox.value.split('-');
        }
        var adjustedMonth = parseInt(dateArray[0], 10);
        adjustedMonth = adjustedMonth - 1;
        var testDate = new Date(dateArray[2], adjustedMonth.toString(), dateArray[1]);

        if ((dateArray[1] == testDate.getDate()) && (adjustedMonth == testDate.getMonth()) && (dateArray[2] == testDate.getFullYear())) {
            txtBox.style.background = 'White';
        }
        else {
            errorTxt = "The Date Entered was not valid";
        }

    }
    else {
        errorTxt = "Enter Date in a MM/DD/YYYY format";
    }

    if (errorTxt != "") {
        txtBox.style.background = 'Yellow';
        alert(errorTxt);
        txtBox.value = "";
    }
}
function hideCalendars(calendarHolder) {
    var allCal = calendarHolder.childNodes;
    var calCount = allCal.length;
    for( var i=0; i<calCount; i++){
        allCal[i].style.visibility = 'hidden';
    }
}
function IsRequired(txtBox) {
    if (txtBox.value.trim() == "") {
	
	txtBox.style.background = 'Yellow';
	return false;
    }
    else {
        txtBox.style.background = 'White';
    }

    return true;
}
function FillHiddenValues(submitForm) {
    var qs = new Querystring();
    for (i = 0; i < submitForm.elements.length; i++) {
        if (submitForm.elements[i].type == "hidden") {
            var qsValue = qs.get(submitForm.elements[i].name);

            if (qsValue != undefined) {
                submitForm.elements[i].value = qsValue;
            }
            
        }
    }
}
function ValidateTextCheck(tBox1,tBox2){
	if(tBox1.value != tBox2.value){
		tBox1.style.background = 'Yellow';
		tBox2.style.background = 'Yellow';
		alert("Values must match");
		return false;
	}
	else{
		tBox1.style.background = 'White';
		tBox2.style.background = 'White';
		return true;		
	}
}
function cvs(){
	try{
		var vState = document.getElementById("__VIEWSTATE");
		vState.parentName.removeChild(vState);
	}
	catch(e){}
}

function DoRequiredChecks() {
var isValid = true;if (IsRequired(document.getElementById('firstname')) == false) {isValid = false;}if (IsRequired(document.getElementById('lastname')) == false) {isValid = false;}if (IsRequired(document.getElementById('companyname')) == false) {isValid = false;}if (IsRequired(document.getElementById('emailaddress1')) == false) {isValid = false;}if (IsRequired(document.getElementById('description')) == false) {isValid = false;}if(checkCaptcha(document.getElementById('Web2CRMfieldOne'), document.getElementById('Web2CRMfieldTwo'), document.getElementById('Web2CRMAnswer')) == false ) isValid = false;return isValid;}

