function validateEmailPassword(formobj){

	if ((!formCheck(formobj,'email','Email'))
		|| (!emailCheck(formobj, 'email', 'Email'))
		){
			return false;
	} else {
		return true;		
	}
	
}

function validateSubscription(formobj){
	if ((!formCheck(formobj,'sender_email','Your email'))
		|| (!emailCheck(formobj, 'sender_email', 'Your email'))
		|| (!formCheck(formobj,'confirm_sender_email','Confirm your email'))
		|| (!emailCheck(formobj, 'confirm_sender_email', 'Confirm your email'))	
		|| (!TCCheck(formobj, 'termsconditions'))
		){
			return false;
	} else {
			if (toLowerCase(formobj.sender_email.value) != toLowerCase(formobj.confirm_sender_email.value)){
				alert('The e-mail addresses you entered do not match. Please re-enter and confirm your e-mail address');
				return false;
			} else {
				return true;		
			}	
	}	
}

function onKeyDown() {
  // current pressed key
  var pressedKey = String.fromCharCode(event.keyCode).toLowerCase();

  if (event.ctrlKey && (pressedKey == "c" || 
                        pressedKey == "v")) {
    // disable key press porcessing
    event.returnValue = false;
  }

}

function validateContactEmail(formobj){

	if ((!formCheck(formobj,'sender_name,sender_email,captcha','Your name,Your email,Security code'))
		|| (!emailCheck(formobj, 'sender_email', 'Your email'))
		){
			return false;
	} else {
		return true;		
	}
	
}


function validateEmailColleague(formobj){
	
	if ((!formCheck(formobj,'email,sender_name,sender_email','Email address,Your name,Your email'))
		|| (!emailCheck(formobj, 'email,sender_email', 'Email address,Your email'))
		|| (!TCCheck(formobj, 'termsconditions'))
		){
			return false;
	} else {
		return true;		
	}
	
}

function validateContactUs(formobj){
	
	if ((!formCheck2(formobj,'email,dev_sector,comment','Email,Category,Comment/feedback','Enter your email,,Enter your comment or feedback'))
		|| (!emailCheck(formobj, 'email', 'Email address'))
		|| (!TCCheck(formobj, 'termsconditions'))
		){
			return false;
	} else {
		return true;		
	}
	
}

function validateSubmitEvent(formobj){
	
	if ((!formCheck2(formobj,'contact_name,contact_email,event_title,start_date,end_date,venue_address1,venue_address2,venue_city,venue_country,event_description','contact name,contact email,event title,start date,end date,venue address1,venue address2,venue city,venue country,event description'
		, 'Enter contact name,Enter contact email,Enter event title,Enter start date,Enter end date,Enter venue address - line 1,Enter venue address - line 2,Enter venue city,Enter venue country,Provide a brief overview of the event'
	    ))
		|| (!dateCheck(formobj,'start_date,end_date','start date,end date')) 
		|| (!TCCheck(formobj, 'termsconditions'))
		|| (!emailCheck(formobj, 'contact_email', 'Contact email'))
		){
			return false;
	} else {
		return true;		
	}
	
}

function validateAskTheExpert(formobj){
	
	if ((!formCheck2(formobj,'name,email,question','name,email,question','Enter your name,Enter your email,Enter your question'))
		|| (!TCCheck(formobj, 'termsconditions'))
		|| (!emailCheck(formobj, 'email', 'Email address'))
		){
			return false;
	} else {
		return true;		
	}
	
}

function validateRecommendExpert(formobj){
	
	if ((!formCheck2(formobj,'name,email,expert_name,expert_email,expertise','your name,your email,expert\'s name,expert\'s email address,expert\'s field of expertise'
		,'Enter your name,Enter your email,Enter the expert\'s name,Enter the expert\'s email,Provide the expert\'s field of expertise'
		))
		|| (!TCCheck(formobj, 'termsconditions'))
		|| (!emailCheck(formobj, 'email,expert_email', 'Email address,Expert\'s email'))
		){
			return false;
	} else {
		return true;		
	}
	
}

function validateWrongInformation(formobj){
	
	if ((!formCheck2(formobj,'name,email','name,email','Enter your name,Enter your email'))
		|| (!emailCheck(formobj, 'email', 'Email address'))
		|| (!TCCheck(formobj, 'termsconditions'))
		){
			return false;
	} else {
		return true;		
	}
	
}

function validateSuggestResource(formobj){
	
	if ((!formCheck2(formobj,'name,email,resource_summary','name,email,summary','Enter your name,Enter your email,Enter the summary'))
		|| (!TCCheck(formobj, 'termsconditions'))
		|| (!altCheck(formobj, 'resource_link,resource_file', 'a) provide a website address or\nb) attach a file'))
		|| (!emailCheck(formobj, 'email', 'Email address'))		
		){
			return false;
	} else {
		return true;		
	}
	
}


function validateSubmitPerson(formobj){
	
	if ((!formCheck2(formobj,'email,confirmEmail,password,confirmPassword,firstname,lastname,languages,short_skills_description,dev_sector,available_from,available_to','Email,Confirm email,Password,Confirm password,Firstname,Lastname,Languages,Short skills description,Area of expertise,Available from,Available to'
		,'Enter your email,Confirm your email,,,Enter your first name,Enter your last name,Enter languages,,,Enter available from,Enter available to'
		))
		|| (!dateCheck(formobj,'available_from,available_to','available from,available to'))
		|| (!matchCheck(formobj,'email,confirmEmail','Email address,Confirm email address'))
		|| (!matchCheck(formobj,'password,confirmPassword','Password,Confirm password'))
		|| (!emailCheck(formobj, 'email', 'Email address'))		
		|| (!TCCheck(formobj, 'termsconditions'))
		){
			return false;
	} else {
		return true;		
	}
	
}

function validateEditPerson(formobj){
	
	if ((!formCheck(formobj,'firstname,lastname,languages,short_skills_description','firstname,lastname,languages,short_skills_description'))
		|| (!dateCheck(formobj,'available_from,available_to','available from,available to'))
		|| (!matchCheck(formobj,'email,confirmEmail','Email address,Confirm email address'))
		|| (!matchCheck(formobj,'password,confirmPassword','Password,Confirm password'))
		|| (!emailCheck(formobj, 'email', 'Email address'))
		|| (!TCCheck(formobj, 'termsconditions'))		
		){
			return false;
	} else {
		return true;		
	}
	
}

function validateSubmitOrganisation(formobj){
	
	if ((!formCheck2(formobj,'email,confirmEmail,password,confirmPassword,org_name,contact_person,address1,city,country,objective,org_type,dev_sector,countries','email,confirm email,password,confirm password,organisation name,contact person,address,city,country,objective,organisation type,focus area,working in'
		, 'Enter your email,Confirm your email,,,Enter organisation name,Enter contact person,Enter address - line 1,Enter city,Enter country,Provide a brief overview of the organisation,,,'))
		|| (!matchCheck(formobj,'email,confirmEmail','Email address,Confirm email address'))
		|| (!matchCheck(formobj,'password,confirmPassword','Password,Confirm password'))
		|| (!emailCheck(formobj, 'email', 'Email address'))
		|| (!TCCheck(formobj, 'termsconditions'))	
		){
			return false;
	} else {
		return true;		
	}
}

function validateEditOrganisation(formobj){
	
	if ((!formCheck(formobj,'org_name,contact_person,address1,city,country,objective,org_type,dev_sector,countries','org_name,contact_person,address1,city,country,objective,org_type,dev_sector,countries'))
		|| (!matchCheck(formobj,'email,confirmEmail','Email address,Confirm email address'))
		|| (!matchCheck(formobj,'password,confirmPassword','Password,Confirm password'))
		|| (!emailCheck(formobj, 'email', 'Email address'))
		|| (!TCCheck(formobj, 'termsconditions'))	
		){
			return false;
	} else {
		return true;		
	}
}

function validateSubmitProject(formobj){
	
	if ((!formCheck2(formobj,'user_name,user_email,project_name,lead_organisation,status,start_date,end_date,country,dev_sector,description,contact_person,contact_email','your name,email address,project name,lead organisation,status,start date,end date,country,development sector,project description,contact person,contact email'
	    , 'Enter your name,Enter your email address,Enter project name,Enter lead organisation,,Enter start date,Enter end date,,,Provide a brief overview of the project goals and beneficiaries,Enter contact person,Enter contact email'
	    ))
		|| (!dateCheck(formobj,'start_date,end_date','start date,end date')) 
		|| (!emailCheck(formobj, 'user_email,contact_email', 'Email address,Contact email'))
		|| (!TCCheck(formobj, 'termsconditions'))		
		){
			return false;
	} else {
		return true;		
	}
}

function validateEditProject(formobj){
	
	if ((!formCheck(formobj,'user_name,user_email,project_name,lead_organisation,status,start_date,end_date,country,dev_sector,description','your name,email address,project name,lead organisation,status,start date,end date,country,development sector,project description'))
		|| (!dateCheck(formobj,'start_date,end_date','start date,end date'))
		|| (!emailCheck(formobj, 'user_email,contact_email', 'Email address,Contact email'))
		|| (!TCCheck(formobj, 'termsconditions'))		
		){
			return false;
	} else {
		return true;		
	}
}

function validateSubmitProjectIdea(formobj){
	
	if ((!formCheck2(formobj,'user_name,user_email,project_name,country,dev_sector,description,outcome','your name,email address,project name,country,development sector,project description,proposed outcome'
		, 'Enter your name,Enter your email,Enter project name,,,Provide a brief overview of need for the project,Provide a brief overview of the proposed outcome'))
		|| (!emailCheck(formobj, 'user_email', 'Email address'))
		|| (!TCCheck(formobj, 'termsconditions'))	
	){
			return false;
	} else {
		return true;		
	}
}


function validateEditProjectIdea(formobj){
	
	if ((!formCheck(formobj,'user_name,user_email,project_name,country,dev_sector,description,outcome','your name,email address,project name,country,development sector,project description,proposed outcome'))
		|| (!emailCheck(formobj, 'user_email', 'Email address'))
		|| (!TCCheck(formobj, 'termsconditions'))	
		){
			return false;
	} else {
		return true;		
	}
}

function validateSubmitJob(formobj){
	
	if ((!formCheck2(formobj,
			'user_name,user_email,org_name,org_description,contact_person,instructions,closing_date,job_title,type,country,job_category,job_description,skills_required',
			'name,email address,organisation name,organisation description,contact person,instructions,closing date,job title,type,country,job category,job description,skills required',
			'Enter name,Enter email,Enter organisation name,Enter organisation description,Enter contact person,Enter instructions,Enter closing date,Enter job title,,,,Provide a brief overview of the job,Provide a brief overview of the skills required'
	))
		|| (!dateCheck(formobj,  'closing_date','closing date'))
		|| (!emailCheck(formobj, 'user_email',  'Email address'))
		|| (!TCCheck(formobj, 'termsconditions'))		
		){
			return false;
	} else {
		return true;		
	}
}

function validateEditJob(formobj){
	
	if ((!formCheck(formobj,'user_name,user_email,org_name,org_description,contact_person,instructions,closing_date,job_title,type,country,job_category,job_description,skills_required','name, email address,organisation name,organisation description,contact person,instructions,closing date,job title,type,country,job category,job description,skills required'))
		|| (!dateCheck(formobj,'closing_date','closing date'))
		|| (!emailCheck(formobj, 'user_email', 'Email address'))
		|| (!TCCheck(formobj, 'termsconditions'))		
		
		){
			return false;
	} else {
		return true;		
	}
}

function validateSubmitTender(formobj){
	
	if ((!formCheck2(formobj,'user_name,user_email,org_name,contact_person,contact_email,instructions,closing_date,project_name,type,country,dev_sector,project_description','name,email address,organisation name,contact person,contact email,instructions,closing date,project name,type,country,development sector,project description'
		, 'Enter your name,Enter your email,Enter organisation name,Enter contact person,Enter contact email,Enter instructions,Enter closing date,Enter project name,,,,Provide a brief overview of the project'
		))
		|| (!dateCheck(formobj,'closing_date','closing date'))
		|| (!emailCheck(formobj, 'user_email', 'Email address'))
		|| (!TCCheck(formobj, 'termsconditions'))	
		|| (!emailCheck(formobj, 'contact_email', 'Contact email'))	
		){
			return false;
	} else {
		return true;		
	}
}

function validateEditTender(formobj){
	
	if ((!formCheck(formobj,'user_name,user_email,org_name,contact_person,instructions,closing_date,project_name,type,country,dev_sector,project_description','name,email address,organisation name,contact person,instructions,closing date,project name,type,country,development sector,project description'))
		|| (!dateCheck(formobj,'closing_date','closing date'))
		|| (!emailCheck(formobj, 'user_email', 'Email address'))
		|| (!TCCheck(formobj, 'termsconditions'))		
		){
			return false;
	} else {
		return true;		
	}
}

//supporting functions
function formCheck(formobj, requiredfields, fielddescription){
	//alert("formCheck() called");
	// name of mandatory fields
	var fieldRequired = requiredfields.split(",");
	// field description to appear in the dialog box
	var fieldDescription = fielddescription.split(",");
	// 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){
			if (obj.type == null){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				continue;
			}

			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "" || obj.options[obj.selectedIndex] == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
			case "password":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
		}
	}

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

function formCheck2(formobj, requiredfields, fielddescription, fieldtips){
	//alert("formCheck2() called");
	// name of mandatory fields
	var fieldRequired = requiredfields.split(",");
	// field description to appear in the dialog box
	var fieldDescription = fielddescription.split(",");
	// field tips that should be considered as "empty value"
	
	var fieldTip = fieldtips.split(",");
	// 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){
			if (obj.type == null){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				continue;
			}

			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "" || obj.options[obj.selectedIndex].value == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
			case "password":
				if (obj.value == "" || obj.value == null || obj.value == fieldTip[i]){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
		}
	}

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


function altCheck(formobj, requiredfields, fielddescription){
	//alert("altCheck() called");
	// name of alternative mandatory fields
	var fieldRequired = requiredfields.split(",");
	// field description to appear in the dialog box
	var fieldDescription = fielddescription.split(",");
	// dialog message
	var alertMsg = "Please complete any of the following fields:\n";
	alertMsg += fielddescription;
	
	var blnValue = false;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			if (obj.type == null){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (blnchecked){
					blnValue = true;
				}
				continue;
			}

			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex != -1 && obj.options[obj.selectedIndex].text != ""){
					blnValue = true;
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex != -1){
					blnValue = true;
				}
				break;
			case "text":
			case "textarea":
			case "password":
			case "file": 
				if (obj.value != "" && obj.value != null){
					blnValue = true;
				}
				break;
			default:
			}
		}
	}

	if (blnValue){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}


function dateCheck(formobj, datefields, fielddescription){
	//alert("formCheck() called");
	// name of mandatory fields
	var dateFields = datefields.split(",");
	// field description to appear in the dialog box
	var fieldDescription = fielddescription.split(",");
	// dialog message
	var alertMsg = "Please correct the following date fields:\n";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < dateFields.length; i++){
		var obj = formobj.elements[dateFields[i]];
		if (obj){
			if (!(isDate(obj)||(obj.value.length==0)))
				alertMsg += " - " + fieldDescription[i] + "\n";		
		}
		
	}

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

function emailCheck(formobj, fields, fielddescription){
	//alert("emailCheck() called");
	// name of mandatory fields
	var emailFields = fields.split(",");
	// field description to appear in the dialog box
	var fieldDescription = fielddescription.split(",");
	// dialog message
	var alertMsg = "Please correct the following email fields:\n";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < emailFields.length; i++){
		var obj = formobj.elements[emailFields[i]];
		if (obj){
			if (!(isEmail(obj.value)||(obj.value.length==0)))
				alertMsg += " - " + fieldDescription[i] + "\n";		
		}
		
	}

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


function matchCheck(formobj, fields, fielddescription){
	//alert("matchCheck() called");
	// name of fields that should be matched
	var mFields = fields.split(",");
	// field description to appear in the dialog box
	var fieldDescription = fielddescription.split(",");
	// dialog message
	var alertMsg = "Value in these fields do not match:\n";
	
	var l_Msg = alertMsg.length;
	
	var temp = "";
	var bMatch = true;
	for (var i = 0; i < mFields.length; i++){
		var obj = formobj.elements[mFields[i]];
		if (obj){
			if (temp.length == 0)
				temp = formobj.elements[mFields[i]].value;
			else
				if (temp!= formobj.elements[mFields[i]].value) bMatch = false;
		}
	if (!bMatch) {
		for (var i = 0; i < fieldDescription.length; i++)
			alertMsg += " - " + fieldDescription[i] + "\n";		
		}
	
		
	}

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


function TCCheck(formobj, field){
	//alert("TCCheck() called");
	// terms and conditions must be agreed.
	// dialog message
	var alertMsg = "Please read and accept the terms and conditions.\n";
	
	var l_Msg = alertMsg.length;
	
	var bAgreed = true;
	
	var obj = formobj.elements[field];
	if (obj){
		if (obj.type == null){
			var blnchecked = false;
			for (var j = 0; j < obj.length; j++){
				if (obj[j].checked){
					blnchecked = true;
				}
			}
			if (!blnchecked){
				bAgreed = false;
			}
		}

		switch(obj.type){
		case "select-one":
			if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
				bAgreed = false;
			}
			break;
		case "select-multiple":
			if (obj.selectedIndex == -1){
				bAgreed = false;
			}
			break;
		case "text":
		case "textarea":
		case "password":
			if (obj.value == "" || obj.value == null){
				bAgreed = false;
			}
			break;
		default:
			var blnchecked = false;
			
			if (obj.checked){
				blnchecked = true;
			}
			if (!blnchecked){
				bAgreed = false;
			}
		
		}
	}

	if (bAgreed){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}


/*
 * Validates date strings.
 *
 * Parameter: field    - field containing the date string
 * Internal Calls: isInteger() and isNotBlank()
 * Returns:   true (valid date) or false (not valid) boolean
 */
function isDate(field) {

    var valid = true;
    var sDate = field.value;
    var Slash1Pos = sDate.indexOf("/",0);
    var Slash2Pos = sDate.indexOf("/",Slash1Pos + 1);
    var dd = sDate.substring(0,Slash1Pos);
    var mm = sDate.substring(Slash1Pos + 1,Slash2Pos);
    var yyyy = sDate.substring(Slash2Pos + 1,sDate.length);

    // Validate date
    if (isInteger(mm) == false || isInteger(dd) == false ||
isInteger(yyyy) == false)
        valid = false;

    if (yyyy.length != 4)
        valid = false;
    else if (mm < 1 || mm > 12)
        valid = false;
    else if (dd < 1 || dd > 31)
        valid = false;
    else if (mm == 2) {
        if (dd > 29)
            valid = false ;
        else if (dd == 29) {
            if (yyyy % 100 == 0 && yyyy % 400 != 0)
                valid = false;
            else if (yyyy % 4 != 0)
                valid = false;
        }
    } else if (mm == 4 || mm == 6 || mm == 9 || mm == 11) {
        if (dd > 30)
            valid = false;
    }

    if (valid == false) {
        //alert("Data Entry Error:\n\nInvalid date entered.\nPlease enter the date in MM/DD/YYYY format.");
        //field.focus();
        //field.select();
    }

    return valid;
}

/*
 * Validates integer strings.
 *
 * Parameter: sInteger - a string to validate as an integer
 * Returns:   true (is integer) or false (is not) boolean
 */
function isInteger(sInteger) {

    var isInt = true;
    inputStr = sInteger.toString(); // in case not a string already
    for (var i = 0; i < inputStr.length; i++) {
        var oneChar = inputStr.charAt(i);
        if (oneChar < "0" || oneChar > "9") {
            isInt = false;
            i = inputStr.length; // break out of loop when bad char found
        }
    } return isInt;
}

/*
 * Validates email addresses.
 *
 * Parameter: sEmail - a string to validate as an email address
 * Returns:   true (is integer) or false (is not) boolean
 */
function isEmail(sEmail) {

    var isEmail = true;
    if ((sEmail.indexOf("@")==-1)||(sEmail.indexOf("@")==0)||(sEmail.indexOf("@")==sEmail.length-1)){
    	isEmail = false;
    	}
    		
    return isEmail;
}

function textCounter(field, maxlimit) {
if (field.value.length > maxlimit){ // if too long...trim it!
	field.value = field.value.substring(0, maxlimit);
	alert("You have entered too many characters into the text box!");
}
}
