
//CHANGE 01.may.2004  J.Strelow
//        adding phone and bad word check: phoneIsValid and CheckFilter
//  adding CheckBusinessRules to send them to a "nothankyou.asp" page based on certain criteria

function CheckBusinessRules(oForm) {
        var bIsGood = true;

        //make sure they are U.S.
        if (oForm.country.options[oForm.country.selectedIndex].value != 'United States'){
                oForm.action = "nothankyou.asp";
                bIsGood = false;
        }
		
	//check anotherprogram
	if ( oForm.anotherprogram[0].checked)
	{
		bIsGood = false;
		oForm.action = "nothankyou.asp";
	}
	//end of adding

    // check highest education - Business Management, Risk Management, Executive Coaching, and Six Sigma - if person selects NONE, HSGED, or Some, give ‘No Thank You’ page. 
	if	(oForm.highesteducation.options[oForm.highesteducation.selectedIndex].value == 'NONE' ||
		oForm.highesteducation.options[oForm.highesteducation.selectedIndex].value == 'HSGED' ||
		oForm.highesteducation.options[oForm.highesteducation.selectedIndex].value == 'SOME')
	{
		//alert('In highed block for Risk Mgt');
		if ((oForm.program.options[oForm.program.selectedIndex].value == 'Risk Management') ||
			(oForm.program.options[oForm.program.selectedIndex].value == '9|ECC') ||
			(oForm.program.options[oForm.program.selectedIndex].value == '9|BMC') ||
			(oForm.program.options[oForm.program.selectedIndex].value == 'Six Sigma Certificate'))
    	{
        	oForm.action = "nothankyou.asp";
        	bIsGood = false;
	    }
	}

	// For Financial Planning, NONE and HSGED should be rejected.  For Project Management SIF/Version 42/31, if prospect selects "None" or "HSGED" the lead should be rejected and give "No Thank you" Page.
	if ((oForm.program.options[oForm.program.selectedIndex].value == '9|FNC') ||
		(oForm.program.options[oForm.program.selectedIndex].value == '9|PMC'))
	{
		if	(oForm.highesteducation.options[oForm.highesteducation.selectedIndex].value == 'NONE' ||
		oForm.highesteducation.options[oForm.highesteducation.selectedIndex].value == 'HSGED')
		{
			oForm.action = "nothankyou.asp";
			bIsGood = false;
		}
	}

    if (bIsGood)
	{
		// change back for live copy                
		oForm.action = "/include/sdc_form_submit.asp";
		// testing only:
		//	oForm.action = "postbacktester.asp";
    }
}


function CheckFilter(oForm) {
        var w1 = String.fromCharCode(115, 104, 105, 116);
        var w2 = String.fromCharCode(112, 105, 115, 115);
        var w3 = String.fromCharCode(99, 117, 110, 116);
        var w4 = String.fromCharCode(116, 105, 116, 115);
        var w5 = String.fromCharCode(97, 115, 115);
        var arrIW = new Array(w1,w2,w3,w4,w5);
        var w6 = String.fromCharCode(102, 117, 99, 107);
        var w7 = String.fromCharCode(99, 111, 99, 107, 115, 117, 99, 107, 101, 114);
        var w8 = String.fromCharCode(109, 111, 116, 104, 101, 114, 102, 117, 99, 107, 101, 114);
        var w9 = String.fromCharCode(97, 115, 115, 104, 111, 108, 101);
        var arrW = new Array(w6,w7,w8,w9);
        var curW = '';
        var curV = '';
        for(var i=0;i<arrIW.length;i++) {
                curW = arrIW[i];
                for (var f=0;f<oForm.length;f++) {
                        if (oForm.elements[f].type == 'text') {
                                curV = oForm.elements[f].value;
                                curV = curV.toLowerCase();
                                if (curV.indexOf(curW) > -1) {
                                        if (curV.indexOf(' ' + curW + ' ') > -1 || curV.indexOf(curW + ' ') == 0 || curV.indexOf(' ' + curW) == (curV.length - (curW.length + 1))) {
                                                alert('Form must not contain profane content');
                                                return false;
                                        }
                                }
                        }
                }
        }
        for (var i=0;i<arrW.length;i++) {
                curW = arrW[i];
                for (var f=0;f<oForm.length;f++) {
                        if (oForm.elements[f].type == 'text') {
                                curV = oForm.elements[f].value;
                                curV = curV.toLowerCase();
                                if (curV.indexOf(curW) > -1) {
                                        alert ('Error: Profane content not permitted.');
                                        return false;
                                }
                        }
                }
        }
        return true;
}


function phoneIsValid(Obj,sLabel) {
        var sPhone = Obj.value;
        var sPhoneTemp = sPhone;
        sPhone = '';
        var sNumbers = '1234567890';
        for (var i=0;i<sPhoneTemp.length;i++) {
                if (sNumbers.indexOf(sPhoneTemp.charAt(i)) > -1) {
                        sPhone += sPhoneTemp.charAt(i);
                }
        }
        if (sPhone.charAt(0) == '1' || sPhone.charAt(0) == 1) {
                sPhone = sPhone.substring(1,(sPhone.length));
        }
        if (sPhone.length != 10) {
                return sLabel + ' must be a 10 digit number\n';
        }
        else {
                Obj.value = sPhone;
        }
        var sAreaCode = sPhone.substring(0,3);
        var sPrefix = sPhone.substring(3,6);
        var sNumber = sPhone.substring(6,10);
        ary7NotAllowed = new Array('1234567','4567890','0000000','1111111','2222222','3333333','4444444','5555555','6666666','7777777','8888888','9999999')
        ary3NotAllowed = new Array('000','911','555');
        for (var i=0;i<ary7NotAllowed.length;i++) {
                if (sPrefix.toString() + sNumber.toString() == ary7NotAllowed[i])  {
                        return sLabel + ' must be a valid phone number\n';
                }
        }
        for (var i=0;i<ary3NotAllowed.length;i++) {
                if (sPrefix.toString() == ary3NotAllowed[i].toString()) {
                        return sLabel + ' must be a valid phone number\n';
                }
        }
        return '';
}

//END CHANGE 01.may.2004



function CountryChange(oCountry) {
	var oState = oCountry.form.state;
        if (oCountry.value != 'United States' && oCountry.value != 'Canada') {
                oCountry.form.kaplan_state.selectedIndex = 0;
                oState.value = 'XX';
        }
}


function SetHiddenState(oKaplanState) {
        var aryStates = new Array('AL','Alabama','AK','Alaska','XX','American Samoa','AZ','Arizona','AR','Arkansas','CA','California','CO','Colorado','CT','Connecticut','DE','Delaware','DC','District of Columbia','XX','Federated States of Micronesia','FL','Florida','GA','Georgia','MS','Mississippi','MO','Missouri','MT','Montana','NE','Nebraska','NV','Nevada','NH','New Hampshire','XX','Guam','HI','Hawaii','ID','Idaho','IL','Illinois','IN','Indiana','IA','Iowa','KS','Kansas','KY','Kentucky','LA','Louisiana','ME','Maine','XX','Marshall Islands','MD','Maryland','MA','Massachusetts','MI','Michigan','MN','Minnesota','NJ','New Jersey','NM','New Mexico','NY','New York','NC','North Carolina','ND','North Dakota','XX','Northern Mariana Islands','OH','Ohio','OK','Oklahoma','OR','Oregon','PW','Palau','PA','Pennsylvania','PR','Puerto Rico','RI','Rhode Island','SC','South Carolina','XX','Armed Forces Europe','XX','Armed Forces Middle East','XX','Armed Forces Pacific','AB','Alberta','SD','South Dakota','TN','Tennessee','TX','Texas','UT','Utah','VT','Vermont','XX','Virgin Islands','VA','Virginia','WA','Washington','WV','West Virgina','WI','Wisconsin','WY','Wyoming','XX','Armed Forces Africa','XX','Armed Forces Americas','XX','Armed Forces Canada','BC','British Columbia','MB','Manitoba','NB','New Brusnwick','NF','Newfoundland','NT','Northwest Territories','NS','Nova Scotia','ON','Ontario','PE','Prince Edward Island','QB','Quebec','SK','Saskatchewan','YT','Yukon Territory')
        var oState = oKaplanState.form.state;
        if (oKaplanState.options[oKaplanState.selectedIndex].value == 'XX') {
                oState.value = 'XX';
        }
        else {
                for (var i=1;i<aryStates.length;i++) {
                        if (oKaplanState.options[oKaplanState.selectedIndex].value == aryStates[i]) {
                                oState.value = aryStates[i-1];
                                break;
                        }
                }
        }
}


function validateForm(oForm) {

        // new code here
        if (!validate('program','Program',oForm)) { return false; }
        if (!validate('firstname','First Name',oForm)) { return false; }
        if (!validate('lastname','Last Name',oForm)) { return false; }
        if (!validate('email','E-mail address',oForm)) { return false; }
        if (!checkEmail(oForm.email.value)) {
                alert('Form Error:\nPlease use a valid E-mail address');
                return false;
        }
        if (!validate('address','Address',oForm)) { return false; }
        if (!validate('city','City',oForm)) { return false; }
        if (oForm.country.options[oForm.country.selectedIndex].value == 'United States' || oForm.country.options[oForm.country.selectedIndex].value == 'Canada')
        {
                if (!validate('kaplan_state','State',oForm)) { return false; }
        }
        if (!validate('zip','Zip',oForm)) { return false; }
        if (!validate('country','Country',oForm)) { return false; }
        if (!validate('evephone','Home Phone',oForm)) { return false; }
        if (!validate('dayphone','Work or Mobile Phone',oForm)) { return false; }
        //at this point, we have already determined that day and eve phones are non-blank,
        //so just call the phoneIsValid function
        var sdayphonePhoneError = phoneIsValid(oForm.dayphone,'Work or Mobile Phone');
        if (sdayphonePhoneError.length > 0) {
                alert(sdayphonePhoneError);
                return false;
        }
        var sevephonePhoneError = phoneIsValid(oForm.evephone,'Home Phone');
        if (sevephonePhoneError.length > 0) {
                alert(sevephonePhoneError);
                return false;
        }
        if (!validate('highesteducation','What is the highest level of education you possess',oForm)) { return false; }       
        if (!validate('anotherprogram', 'Are you currently enrolled in another educational program?', oForm)){ return false;}
        if (!validate('occupation','Occupation',oForm)) { return false; }
        if (!validate('takenonline', 'Have you ever enrolled in an online education program?', oForm)){ return false;}
        if (!validate('besttime','When is the best time to contact you',oForm)) { return false; }

        CheckBusinessRules(oForm);
        if (!CheckFilter(oForm)){
                return false;
        }
        return true;
}

function checkEmail(sEmail) {
        var AtSign = sEmail.indexOf('@');
        var Period = sEmail.lastIndexOf('.');
        if (AtSign > -1 && Period > -1 && AtSign < Period) {
                return true;
        }
        else {
                return false;
        }
}

function validate(sFieldName,sLabelName,oForm) {
        var oField = eval('oForm.' + sFieldName);
        var undefined;
        if (oField != undefined) {
                 if (CheckRequired(oField) ) {
                        return true;
                }
                else {
                        if (oField[0] == undefined || oField.type == 'select-one') {
                                alert('Form Error:\nPlease complete: ' + sLabelName + '.');
                                oField.focus();
                                return false;
                        }
                        else {
                                if (!CheckRequired(oField)) {
                                        alert('Form Error:\nPlease complete: ' + sLabelName + '.');
                                        oField[0].focus();
                                        return false;
                                }
                        }
                }
        }
        else if (oField[0] != undefined) {
                if (!CheckRequired(oField)) {
                                        alert('Form Error:\nPlease complete: ' + sLabelName + '.');
                                        oField[0].focus();
                                        return false;
                }
        }
        else {
                alert('undefined field in form: ' + sFieldName);
                return false;
        }
}

function CheckRequired(Obj) {
        var ObjType;
        var retVal;
        var x = 0;
        var undefined;
        ObjType = Obj.type;
        /* types: password, radio, select-multiple, select-one, text, textarea */
        if (ObjType == undefined) {
                ObjType = Obj[0].type;
        }
        switch(ObjType) {
                case 'hidden':
                        if(Obj.value.length < 1) {
                                retVal = false;
                        }
                        else {
                                retVal = true;
                        }
                break;
                case 'text':
                        if(Obj.value.length < 1) {
                                retVal = false;
                        }
                        else {
                                retVal = true;
                        }
                break;

                case 'textarea':
                        if(Obj.value.length < 1) {
                                retVal = false;
                        }
                        else {
                                retVal = true;
                        }
                break;

                case 'password':
                        if(Obj.value.length < 1) {
                                retVal = false;
                        }
                        else {
                                retVal = true;
                        }
                break;
                case 'file':
                        if(Obj.value.length < 1) {
                                retVal = false;
                        }
                        else {
                                retVal = true;
                        }
                break;
                case 'select-one':
                        if(Obj.selectedIndex > 0) {
                                retVal = true;
                        }
                        else {
                                retVal = false;
                        }
                break;

                case 'select-multiple':
                        retVal = false;
                        for (var x=0;x<Obj.options.length;x++) {
                                if (Obj[x].selected) {
                                        retVal = true;
                                }
                        }
                break;

                case 'radio':
                        retVal = false;
                        for(var x=0;x<Obj.length;x++) {
                                if (Obj[x].checked) {
                                        retVal = true;
                                }
                        }
                break;

                case 'checkbox':

                        if (navigator.appName == 'Netscape' && (navigator.appVersion.toString().substr(0,1) == '4' || navigator.appVersion.toString().substr(0,1) == '3' || navigator.appVersion.toString().substr(0,1) == '2')) {
                                var arrNum = Obj.length - 1;
                                var cbObj;
                                if (Obj[arrNum] == undefined) {
                                        cbObj = Obj;
                                }
                                else {
                                        cbObj = Obj[arrNum];
                                }
                                var formObj = cbObj.form;
                                var cbName = cbObj.name;
                                var cbCount = 0;
                                var cbFirstChecked = cbObj.checked;
                                var cbOtherChecked = false;
                                for (var cb=0;cb<formObj.length;cb++) {
                                        if (formObj.elements[cb].type == 'checkbox' && formObj.elements[cb].name == cbName) {
                                                cbCount++;
                                                if (formObj.elements[cb].checked) {
                                                        cbOtherChecked = true;
                                                        }
                                        }
                                }
                                if (cbCount > 0) {
                                        if (cbFirstChecked || cbOtherChecked) {
                                                retVal = true;
                                        }
                                        else {
                                                retVal = false;
                                        }
                                }
                                else {
                                        if (cbFirstChecked) {
                                                retVal = true;
                                        }
                                        else {
                                                retVal = false;
                                        }
                                }
                        }
                        else {
                        retVal = false;
                        if (Obj.length != undefined) {
                                for(var x=0;x<Obj.length;x++) {
                                        if (Obj[x].checked) {
                                                retVal = true;
                                        }
                                }
                        }
                        else {
                                if (Obj.checked == true) {
                                        retVal = true;
                                }
                        }
                }
                break;


                default:
                        retVal = true;
        }
        return retVal;
}

// end std validation