// ---  R O U T I N E S  ------------------------------------------------------------------------------
function get_radio_value(radio_array)
{
	var i;
	for (i = 0; i < radio_array.length; ++ i)
		if (radio_array[i].checked)
			return radio_array[i].value;
	return ' ';
}

// ---  V A L I D A T I O N S  ------------------------------------------------------------------------

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

// This is the function that performs form verification. It will be invoked
// from the onSubmit() event handler. The handler should return whatever
// value this function returns.
function verify(f)
{
	var msg;
    var empty_fields = "";
    var errors = "";
    // Loop through the elements of the form, looking for all 
    // text and textarea elements that have a "required" property
    // defined. Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // then verify that they are numbers and that they are in the right range.
    // Put together error messages for fields that are wrong.
    for(var i = 0; i < f.length; i++) {

        var e = f.elements[i];
		var efilter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

        if (((e.type == "text") || (e.type == "textarea")) && e.required) {
            // first check if the field is empty
            if ((e.value == null) || (e.value == "") || isblank(e.value)) {
                empty_fields += "\n          " + e.name;
                continue;
            }
		}	
        if (!isblank(e.value) && ((e.type == "text") || (e.type == "textarea"))) {
            // Now check for fields that are supposed to be numeric.
            if ((e.min != null) || (e.max != null)) { 
                var v = parseFloat(e.value);
                if (isNaN(v) || !e.numeric || 
                    ((e.min != null) && (v < e.min)) || 
                    ((e.max != null) && (v > e.max))) {
                    errors += "- The field " + e.name + " 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";
                }
            }
        }
//        if ((e.type == "radio") && e.required) {
//           // make sure one value is checked
//		   	for (var j = 0; j < e.length; ++ j)
//				if (e[j].checked)
//                continue;
//            }
//			if j > e.length {
//				errors += "- Please select a '" + e.name + "' option.\n";
//			}
//		}			
       // Now, if it is an email type field, verify it.
		if ((e.emailtype) && !efilter.test(e.value)){
			 errors += "- The field  '" + e.name + "'  must be a valid email address.\n";
		}
    }
    // Now, if there were any errors, then display the messages, and
    // return false to prevent the form from being submitted. Otherwise
    // return true.
    if (!empty_fields && !errors) return true;
    msg  = "____________________________________________________________\n\n"
    msg += "The Questionnaire was not submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "____________________________________________________________\n\n"
    if (empty_fields) {
        msg += "- The following required field(s) are empty:" 
                + empty_fields + "\n";
    }
    if (errors) msg += "\n";    
	msg += errors;
    alert(msg);
    return false;
}

// ---  S E C U R I T Y  ---------------------------------------------------------------------------

// Disable Select-Text
//form tags to omit in NS6+:

var omitformtags=["input", "textarea", "select"]
omitformtags='|'+omitformtags.join("|")+'|';

function disableselect(e){
if (typeof document.onselectstart!="undefined") var thisTarget = window.event.srcElement; else var thisTarget = e.target;
thisTarget ='|'+thisTarget.tagName.toLowerCase()+'|';
if (omitformtags.indexOf(thisTarget)==-1)
return false
}

function reEnable(){
return true
}

if (typeof document.onselectstart!="undefined")
document.onselectstart=disableselect
else{
document.onmousedown=disableselect
document.onmouseup=reEnable
}

// Disable Right-Click
var message="";
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if 
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers) 
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}
document.oncontextmenu=new Function("return false")

// ---  R O L L O V E R S  ------------------------------------------------------------------------

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		profile_over = newImage("images/profile-over.gif");
		services_over = newImage("images/services-over.gif");
		portfolio_over = newImage("images/portfolio-over.gif");
		methodology_over = newImage("images/methodology-over.gif");
		contact_over = newImage("images/contact-over.gif");
		manage_over = newImage("images/manage-over.gif");
		preloadFlag = true;
	}
}
