// 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";
				}
			}
		}
	}
formobj.clientScreenRes.value = screen.width + "x" + screen.height;
    formobj.clientColorDepth.value = screen.colorDepth;
	if (alertMsg.length == l_Msg){
		
		if(invalidemail(document.getElementById("email").value)){
			alert('Email invalid');
			return false;
		}else{
			return true;
		}
		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) )
			{
				// collect the blank fields in a string
				empty_fields += "\n     " + e.fieldname;
				continue;
			}
			
			if ( e.email )
			{
				if ( invalidemail(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 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;
    }
}

