// copied and modified from the book JavaScript: The Definitive Guide by David Flanagan



/***********************************************
* Required field(s) validation v1.10- By NavSurf
* Visit Nav Surf at http://navsurf.com
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function formCheck(formobj){
	// Enter name of mandatory fields
	var fieldRequired = Array("txtName", "txtEmail", "txtAddress", "txtSubject", "txtMessage");
	// Enter field description to appear in the dialog box
	var fieldDescription = Array("Name", "Email", "Address", "Subject", "Message");
	// dialog message
	var alertMsg = "Please complete the following fields:\n";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}





function isblank(s)					// returns true if the field is blank
{
	for (var i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if ( (c != ' ') && (c != '\n') && (c != '\t') )
		     return false;
	}
	
	return true;
}

function invalidemail(s)			// returns true if the field is an invalid email address
{
	if ( (s.indexOf("@") == -1) || (s.indexOf(".") == -1) )
	{
		return true;
	}
}

function verify(f)
{
	var msg;
	var empty_fields = "";
	var errors = "";
	
	for (var i = 0; i < f.length; i++)
	{
		var e = f.elements[i];
		
		// only text areas will be checked...
		// make sure to use default values for other types in your html code
		if ( ( (e.type == "text") || (e.type == "textarea") ) && !e.optional)
		{
			if ( (e.value == null) || (e.value == "") || isblank(e.value) || (e.value == e.default_val) && ((e.fieldname != "Country Code") || (e.fieldname != "Area Code") || (e.fieldname != "Number")) )
			{
				// collect the blank fields in a string
				empty_fields += "\n     " + e.fieldname;
				continue;
			}
			
			if ( e.email )
			{
				if ( emailValidator(e.value) )
				{
					errors += " - The field \"" + e.fieldname + "\" is an invalid email address.";
					errors += "\n";
				}
				
				continue;
			}
			
 			if ( e.numeric || (e.min != null) || (e.max != null) )
			{
				// collect in a string erroneous inputs concerning numerical fields
				var v = parseFloat(e.value);
				if (isNaN(v) ||
				( (e.min != null) && (v < e.min) ) ||
				( (e.max != null) && (v > e.max) ) )
				{
					errors += " - The field \"" + e.fieldname + "\" must be a number";
					if (e.min != null)
				     		errors += " that is greater than " + e.min;
					if (e.max != null && e.min != null)
				     		errors += " and less than " + e.max;
					else if ( e.max != null)
				     		errors += " that is less than " + e.max;
					errors += ".\n";
				}
			}
		}
	}

	// everything's fine!
	if (!empty_fields && !errors) return true;

	msg = "_______________________________________\n\n";
	msg += "The form was not submitted because of the following error(s).\n";
	msg += "Please correct these error(s) and re-submit.\n";
	msg += "_______________________________________\n\n";

	// output errors
	if (empty_fields)
	{
		msg += " - The following required field(s) are empty:"
	      		+ empty_fields + "\n";
		if (errors)
	     		msg += "\n";
	}

	msg += errors;
	alert(msg);
	return false;
}

function verifyForm(f)
{
    f.txtEmail.email = true;
    f.txtCountryCode.numeric = true;
    f.txtAreaCode.numeric = true;

    f.txtName.fieldname = "Name";
    f.txtEmail.fieldname = "Email";
    f.txtCountryCode.fieldname = "Country Code";
    f.txtAreaCode.fieldname = "Area Code";
    f.txtNumber.fieldname = "Number";
    f.txtAddress.fieldname = "Address";
    f.txtSubject.fieldname = "Subject";
    f.txtMessage.fieldname = "Message";

    f.clientScreenRes.value = screen.width + "x" + screen.height;
    f.clientColorDepth.value = screen.colorDepth;

    return verify(f);
}

function verifySyndicationForm(f)
{
	f.txtCompany.fieldname = "Company Name";
	f.txtContactName.fieldname = "Contact Person";
	f.txtPhoneNumA.fieldname = "Country Code - Phone Number";
	f.txtPhoneNumB.fieldname = "Area Code - Phone Number";
	f.txtPhoneNum.fieldname = "Phone Number";
	f.txtFaxNumA.fieldname = "Country Code - Fax Number";
	f.txtFaxNumB.fieldname = "Area Code - Fax Number";
	f.txtFaxNum.fieldname = "Fax Number";
	f.txtEmail.fieldname = "Email Address";
	f.txtOfficeAdd.fieldname = "Office Address";
	f.txtNumArticles.fieldname = "Number of Articles";
	f.cap.fieldname = "Verification Code";
	f.txtQ1_1.optional = true;
	f.txtQ2_1.optional = true;
	f.txtQ2_2.optional = true;
	f.txtQ2_3.optional = true;
	f.txtQ3_1.optional = true;
	f.txtQ3_2.optional = true;
	f.txtQ3_3.optional = true;
	f.txtQ4_1.optional = true;
	f.txtQ4_2.optional = true;
	f.txtQ5_1.optional = true;
	return verify(f);
}

function syndicationForm(thisform)
{
   var txtCompany = document.srequest.txtCompany;
   var txtContactName = document.srequest.txtContactName;
   var txtPhoneNumA = document.srequest.txtPhoneNumA;
   var txtPhoneNumB = document.srequest.txtPhoneNumB;
   var txtPhoneNum = document.srequest.txtPhoneNum;
   var txtFaxNumA = document.srequest.txtFaxNumA;
   var txtFaxNumB = document.srequest.txtFaxNumB;
   var txtFaxNum = document.srequest.txtFaxNum;
   var txtEmail = document.srequest.txtEmail;
   var txtOfficeAdd = document.srequest.txtOfficeAdd;
   var txtNumArticles = document.srequest.txtNumArticles;
   var cap = document.srequest.cap;

	with (thisform)
    {
		if (validate_required(txtCompany,"Company Name must be filled out!")==false)
          {txtCompany.focus();return false;}
        if (validate_required(txtContactName,"Contact Name must be filled out!")==false)
          {txtContactName.focus();return false;}
	    if (isAlphabet(txtContactName,"Please enter a valid Contact Name!")==false)
          {txtContactName.focus();return false;}
        if (validate_required(txtPhoneNumA,"Country Code must be filled out")==false)
            {txtPhoneNumA.focus();return false;}
        if (isNumeric(txtPhoneNumA,"Please enter a valid Country Code!")==false)
            {txtPhoneNumA.focus();return false;}
        if (validate_required(txtPhoneNumB,"Area Code must be filled out")==false)
            {txtPhoneNumB.focus();return false;}
        if (isNumeric(txtPhoneNumB,"Please enter a valid Area Code!")==false)
            {txtPhoneNumB.focus();return false;}
        if (validate_required(txtPhoneNum,"Phone Number must be filled out")==false)
            {txtPhoneNum.focus();return false;}
        if (isNumeric(txtPhoneNum,"Please enter a valid Phone number!")==false)
            {txtPhoneNum.focus();return false;}
        if (validate_required(txtFaxNumA,"Country Code must be filled out")==false)
            {txtFaxNumA.focus();return false;}
        if (isNumeric(txtFaxNumA,"Please enter a valid Country Code!")==false)
            {txtFaxNumA.focus();return false;}
        if (validate_required(txtFaxNumB,"Area Code must be filled out")==false)
            {txtFaxNumB.focus();return false;}
        if (isNumeric(txtFaxNumB,"Please enter a valid Area Code!")==false)
            {txtFaxNumB.focus();return false;}
        if (validate_required(txtFaxNum,"Fax Number must be filled out")==false)
            {txtFaxNum.focus();return false;}
        if (isNumeric(txtFaxNum,"Please enter a valid Fax Number!")==false)
            {txtFaxNum.focus();return false;}
        if (validate_required(txtEmail,"Email Address must be filled out!")==false)
            {txtEmail.focus();return false;}
        if (emailValidator(txtEmail,"Please enter a valid email address!")==false)
            {txtEmail.focus();return false}
        if (validate_required(txtOfficeAdd,"Office Address must filled out!")==false)
            {txtOfficeAdd.focus();return false;}
        if (validate_required(txtNumArticles,"Number of Articles must be filled out")==false)
            {txtNumArticles.focus();return false;}
        if (isNumeric(txtNumArticles,"Please enter a valid Number of Articles!")==false)
            {txtNumArticles.focus();return false;}
        if (validate_required(cap,"Please verify the code!")==false)
            {cap.focus();return false;}


	}


}

function validate_required(field,alerttxt)
{
    with (field)
    {
        if (value==null||value=="")
            {alert(alerttxt);return false;}
        else {return true}
    }
}


function isNumeric(elem, helperMsg){
    var numericExpression = /^[0-9]+$/;
    if(elem.value.match(numericExpression)){
        return true;
    }else{
        alert(helperMsg);
        elem.focus();
        return false;
    }
}

function isAlphabet(elem, helperMsg){
    var alphaExp = /^[a-zA-Z\s.\-_']+$/;
    if(elem.value.match(alphaExp)){
        return true;
    }else{
        alert(helperMsg);
        elem.focus();
        return false;
    }
}


function emailValidator(elem, helperMsg){
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    if(elem.value.match(emailExp)){
        return true;
    }else{
        alert(helperMsg);
        elem.focus();
        return false;
    }
}


function checkCompleteDate(month,day,year)
{
    var errCompleteDate = "";
    if (month == '0')
    {
        if (day == '0')
        {
            if (year == '0')
            {
                if (year == '0') {  errCompleteDate = 0;    }
                else             {  errCompleteDate = 1;    }
            }
            else {  errCompleteDate = 1;    }
        }
        else     {  errCompleteDate = 1;    }
    }
    else
    {
        if (day == '0') {   errCompleteDate = 1;    }
        else
        {
            if (year == '0') {  errCompleteDate = 1;    }
        }
    }

    return errCompleteDate;
}

function checkSearchForm(form)
{
    var msg = "";
    var errCompleteDate = 0;
    var invalid = 0;

    var publish_startmonth = form.publish_startmonth.value;
    var publish_startday = form.publish_startday.value;
    var publish_startyear = form.publish_startyear.value;
    var publish_endmonth = form.publish_endmonth.value;
    var publish_endday = form.publish_endday.value;
    var publish_endyear = form.publish_endyear.value;

    publish_startdate = checkCompleteDate(publish_startmonth,publish_startday,publish_startyear);
    publish_enddate = checkCompleteDate(publish_endmonth,publish_endday,publish_endyear);

    if (publish_startdate||publish_enddate)
    {
        errCompleteDate = 1;
    }

        if (form.country_id[0].checked)
        {
            if (form.province_no.value == 0) { invalid = 1; }
            else { invalid = 0; }
        }
        else if (form.country_id[1].checked)
        {
            if (form.state_no.value == 0) { invalid = 1;}
            else { invalid = 0; }
        }
        else if (form.country_id[2].checked)
        {
            if (form.other_no.value == 0) { invalid = 1;}
            else { invalid = 0; }
        }

    if (invalid || errCompleteDate)
    {
        if (errCompleteDate) { msg += "Please complete the date field.\n";  }
        if (invalid) { msg += "Please choose a location.\n";}
        alert(msg);
        return false;
    }
    else
    {
        return true;
    }
}

function select_Country(form, radio_num, value)
{
    if (value == 'story')
    {
        form.country_id[radio_num].checked = true;

        if (radio_num != 0) {   form.province_no.options.selectedIndex = 0; }
        if (radio_num != 1) {   form.state_no.options.selectedIndex = 0;    }
        if (radio_num != 2) {   form.other_no.options.selectedIndex = 0;    }
    }
    else
    {
        form.file_country_id[radio_num].checked = true;
        if (radio_num != 0) {   form.file_province_no.options.selectedIndex = 0; }
        if (radio_num != 1) {   form.file_state_no.options.selectedIndex = 0;    }
        if (radio_num != 2) {   form.file_other_no.options.selectedIndex = 0;    }
    }
}

function set_Others(form, radio_num, others_select, value)
{
    if (value == 'story')
    {
        if (radio_num == 0)
        {
            var value = form.province_no[form.province_no.options.selectedIndex].value;
        }
        if (radio_num == 1)
        {
            var value = form.state_no[form.state_no.options.selectedIndex].value;
        }
        if (radio_num == 2)
        {
            var value = form.other_no[form.other_no.options.selectedIndex].value;
        }
        form.country_id[radio_num].value = value;
    }
    else
    {
        if (radio_num == 0)
        {
            var value = form.file_province_no[form.file_province_no.options.selectedIndex].value;
        }
        if (radio_num == 1)
        {
            var value = form.file_state_no[form.file_state_no.options.selectedIndex].value;
        }
        if (radio_num == 2)
        {
            var value = form.file_other_no[form.file_other_no.options.selectedIndex].value;
        }
        form.file_country_id[radio_num].value = value;
    }
}
