/* General Functions */
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
function isCharsInBag (s, bag)
{
    var i;
    // Search through string's characters one by one.
    // If character is in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) return(false);
    }
    return true;
}

function isValidChar(testingStr, userStr)
{
	var condition = true;

	for(counter=0; counter < testingStr.length; counter++)
	{
		if(testingStr.indexOf(userStr.charAt(counter)) == -1)
		{
			condition = false;
		}
	}

	return condition;
}
function EmailCheck(email){

		if(email.value==''){
			alert("Please Enter Email Address");
            email.focus();
            return false;
		}
		if ( !this.isCharsInBag( email.value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._@-" ))
		{

            alert("Invalid Email Address");
            email.focus();
            return false;
		}

	if(email.value != '')
	   {
		var str=email.value;
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		var sdot=str.indexOf(dot,ldot+1);

		if (str.indexOf(at)==-1){
			alert("Invalid Email Address");
            email.focus();
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
			alert("Invalid Email Address");
            email.focus();
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
			alert("Invalid Email Address");
            email.focus();
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
			alert("Invalid Email Address");
            email.focus();
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			alert("Invalid Email Address");
            email.focus();
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
			alert("Invalid Email Address");
            email.focus();
		    return false;
		 }
		if (str.substring(ldot+1)==''){
			alert("Invalid Email Address");
            email.focus();
		    return false;
		 }
		 if (str.substring(sdot+1)==''){
			alert("Invalid Email Address");
            email.focus();
		    return false;
		 }
		 if (str.indexOf(" ")!=-1){
			alert("Invalid Email Address");
            email.focus();
		    return false;
		 }
		 if(str.lastIndexOf(".") == str.length-1)
		 {
		 	alert("Invalid Email Address");
            email.focus();
		    return false;
		 }
	}//end of else
	return true;
}
function checkFileName( filename )
{
	if( filename.match(/(.*)[\/\\]([^\/\\]+\.\w+)$/) && filename.match(/(:|\/)/) )
	{
		return true
	}
	else
	{
		return false;
	}
}
function CheckExtention(el) // pass Upload File object’s ClientID here
{
	var objFSO ;
	try
	{
		objFSO = new ActiveXObject("Scripting.FileSystemObject");
	}
	catch(err)
	{
		//alert(err.message);
		//alert(err.number);
	}
	var myel= document.getElementById(el);
	var file;
	var path = myel.value;
	//alert(path);
	file = objFSO.getFile(path);
	var size;
	size = file.size ; // This size will be in Bytes
	// We are converting it to KB as below
	alert("File Size is : " + file.size / 1024 +" KB");
}
function newsShowHide(sec, sub_sec, id)
{
	var shortDiv  = document.getElementById(sec+"_"+id);
	var longDiv = document.getElementById(sec+"_details_"+id);

	if (sub_sec == 'details')
	{
		shortDiv.style.display = 'none';
		longDiv.style.display = 'inline';
	}
	else //if (sub_sec == 'short')
	{
		longDiv.style.display = 'none';
		shortDiv.style.display = 'inline';
	}
}
function divShowhide(chkDiv)
{
	objDiv = document.getElementById(chkDiv);

	if (objDiv.style.display == 'none')
	{
		objDiv.style.display = 'inline';
	}
	else if (objDiv.style.display == 'inline')
	{
		objDiv.style.display = 'none';
	}
}
var total_sub_menu;
var current_sub_menu;
var sel_sub_menu;
var t = 0;
function showMenu(chkDiv)
{
   	//alert("total_sub_menu: "+total_sub_menu+"\nchkDiv: "+chkDiv);
	t=0;
	for(var a=0;a<total_sub_menu;a++){
	   	if(a==chkDiv){
			objDiv = document.getElementById("parent"+a);
			objDiv.style.display = 'block';
			sel_sub_menu = a;
		}
		else {
			objDiv = document.getElementById("parent"+a);
			objDiv.style.display = 'none';
		}
	}

}
function hideMenu()
{
	if(t==0){
		t = setTimeout("hideMenuDone()", 1000);
	}
	return false;
}
function hideMenuDone()
{
	if(t>0){
		for(var a=0;a<total_sub_menu;a++){
			if(a==current_sub_menu){
				objDiv = document.getElementById("parent"+a);
				objDiv.style.display = 'block';
			}
			else {
				objDiv = document.getElementById("parent"+a);
				objDiv.style.display = 'none';
				sel_sub_menu = -1;
			}
		}
	}
	t=0;
}
function divShowhideOpenClose(chkOpen, chkClose)
{
	chkOpenObj = document.getElementById(chkOpen);
	chkOpenObj.style.display = 'block';

	chkCloseObj = document.getElementById(chkClose);
	chkCloseObj.style.display = 'none';
}
var hWnd
function OpenNewWindow( passurl , width, height, left, top)
{
	//var x = screen.availWidth/2-200;
//	var x = (screen.availWidth-width);
//	var y = 150;

	var x = left;
	var y = top;

	enterCalled = true
	window.name = "parent"
	var url = passurl
	if( !hWnd )
	{
		//if no help window is open then open a new help window and give it focus
		hWnd = window.open(url,"popup","width=" + width + ",height=" + height +", left=" + x + ", top=" + y + ", resizable=yes,scrollbars=yes")
		hWnd.focus()
	}
	else if ( hWnd.closed )
	{
		//if no help window is open then open a new help window and give it focus
		hWnd = window.open(url,"popup","width=" + width + ",height=" + height +", left=" + x + ", top=" + y + ", resizable=yes,scrollbars=yes")
		hWnd.focus()
	}
	else
	{
		//if the help window is already open then update it and give it focus
		hWnd.location=url
		hWnd.focus()
	}
	// handle Navigator 2, which doesn't have an opener property
	if (!hWnd.opener) { hWnd.opener = window}
}
// my address finder code end  here

function newWindow(url)
{
	var width = screen.width;
	var height = screen.height;
	var ctbWnd = window.open(url,"ctbWnd","status=1,resizable=1,width=" + width + ",height=" + height);
	ctbWnd.moveTo(0,0);
}
function newWindowWithFixDimensions(url)
{
	var width = 520;
	var height = 400;
	var ctbWnd = window.open(url,"ctbWnd","scrollbars=1,status=0,resizable=1,width=" + width + ",height=" + height);
	ctbWnd.moveTo(0,0);
}
/* Site Validation Functions */
function validateCVForm()
{
	var yname = document.frmCV.yname;
	if (yname.value == "")
	{
		alert("Please Fill the 'Your Name' field.");
		yname.focus();
		return false;
	}
	var cv = document.frmCV.cv;
	if (cv.value == "")
	{
		alert("Please Fill the 'Your CV' field.");
		cv.focus();
		return false;
	}
	else
	{
		if(!this.checkFileName(cv.value)) {
			alert("Invalid File Path/URL in 'Your CV' field.");
			cv.focus();
			return false;
		}
	}
	var comments = document.frmCV.comments;
	if (comments.value == "")
	{
		alert("Please Fill the 'Comments' field.");
		comments.focus();
		return false;
	}
	return true;
}
function validateRegistrationForm()
{

   	var sur_name = document.getElementById("sur_name");
	if(sur_name.value==""){
	   alert("Please enter 'Surname'.");
	   sur_name.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ", sur_name.value))
	{
		alert("Surname fields only allow alpha-numeric characters");
		sur_name.focus();
		return false;
	}
	var first_name = document.getElementById("first_name");
	if(first_name.value==""){
	   alert("Please enter 'Forename'.");
	   first_name.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ", first_name.value))
	{
		alert("Forename fields only allow alpha-numeric characters");
		first_name.focus();
		return false;
	}
	var phone = document.getElementById("phone");
	if(phone.value==""){
	   alert("Please enter 'Phone'.");
	   phone.focus();
	   return false;
	}
	else if (!isValidChar("1234567890 ", phone.value))
	{
		alert("'Phone' fields only allow numeric characters");
		phone.focus();
		return false;
	}


	var address1 = document.getElementById("address1");
	if(address1.value==""){
	   alert("Please enter 'Address 1'.");
	   address1.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890\\~!@#$%^&*()_-+={}[]:;|/ ", address1.value))
	{
		alert("'Address 1' fields only allow alpha-numeric characters");
		address1.focus();
		return false;
	}


	var address2 = document.getElementById("address2");
	if(address2.value!=""){
		if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890\\~!@#$%^&*()_-+={}[]:;|/ ", address2.value))
		{
			alert("'Address 2' fields only allow alpha-numeric characters");
			address2.focus();
			return false;
		}
	}

	var address3 = document.getElementById("address3");
	if(address3.value!=""){
		if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890\\~!@#$%^&*()_-+={}[]:;|/ ", address3.value))
		{
			alert("'Address 3' fields only allow alpha-numeric characters");
			address3.focus();
			return false;
		}
	}

	var address4 = document.getElementById("address4");
	if(address4.value!=""){
		if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890\\~!@#$%^&*()_-+={}[]:;|/ ", address4.value))
		{
			alert("'Address 4' fields only allow alpha-numeric characters");
			address4.focus();
			return false;
		}
	}


	var postcode = document.getElementById("postcode");
	if(postcode.value==""){
	   alert("Please enter 'Postcode'.");
	   postcode.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890- ", postcode.value))
	{
		alert("'Postcode' fields only allow alpha-numeric characters");
		postcode.focus();
		return false;
	}


	var email = document.getElementById("email");
	if(!this.EmailCheck(email))
	{
		return false ;
	}


	/*
	var date_of_birth = document.getElementById("date_of_birth");
	if(date_of_birth.value==""){
	   alert("Please enter 'Date of Birth'.");
	   date_of_birth.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890- ", date_of_birth.value))
	{
		alert("'Date of Birth' fields only allow alpha-numeric characters");
		date_of_birth.focus();
		return false;
	}
	*/
	var passport_number = document.getElementById("passport_number");
	if(passport_number.value==""){
	   alert("Please enter 'Passport Number'.");
	   passport_number.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890- ", passport_number.value))
	{
		alert("'Passport Number' fields only allow alpha-numeric characters");
		passport_number.focus();
		return false;
	}

	/*
	var date_of_issue = document.getElementById("date_of_issue");
	if(date_of_issue.value==""){
	   alert("Please enter 'Date of Issue'.");
	   date_of_issue.focus();
	   return false;
	}
	else if (!isValidChar("1234567890 ", date_of_issue.value))
	{
		alert("'Date of Issue' fields only allow numeric characters");
		date_of_issue.focus();
		return false;
	}
	var date_of_enquiry = document.getElementById("date_of_enquiry");
	if(date_of_enquiry.value==""){
	   alert("Please enter 'Date of Enquiry'.");
	   date_of_enquiry.focus();
	   return false;
	}
	else if (!isValidChar("1234567890 ", date_of_enquiry.value))
	{
		alert("'Date of Enquiry' fields only allow numeric characters");
		date_of_enquiry.focus();
		return false;
	}
	*/
	return true;
}
function validateRegistrationForm2()
{
	return true;
	var national_insurance_number = document.getElementById("national_insurance_number");
	if(national_insurance_number.value==""){
	   alert("Please enter 'National Insurance Number'.");
	   national_insurance_number.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-/ ", national_insurance_number.value))
	{
		alert("'National Insurance Number' fields only allow alpha-numeric characters");
		national_insurance_number.focus();
		return false;
	}


	var unique_tax_ref_number = document.getElementById("unique_tax_ref_number");
	if(unique_tax_ref_number.value==""){
	   alert("Please enter 'Unique Tax Reference Number'.");
	   unique_tax_ref_number.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-/ ", unique_tax_ref_number.value))
	{
		alert("'Unique Tax Reference Number' fields only allow alpha-numeric characters");
		unique_tax_ref_number.focus();
		return false;
	}


	var cscs_number = document.getElementById("cscs_number");
	if(cscs_number.value==""){
	   alert("Please enter 'CSCS Number'.");
	   cscs_number.focus();
	   return false;
	}
	else if (!isValidChar("1234567890 ", cscs_number.value))
	{
		alert("'CSCS Number' fields only allow numeric characters");
		cscs_number.focus();
		return false;
	}

	var expiry_date = document.getElementById("expiry_date");
	if(expiry_date.value==""){
	   alert("Please enter 'Expiry Date'.");
	   expiry_date.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890- ", expiry_date.value))
	{
		alert("'Expiry Date' fields only allow alpha-numeric characters");
		expiry_date.focus();
		return false;
	}

	var address = document.getElementById("address");
	if(address.value==""){
	   alert("Please enter 'Address'.");
	   address.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890\\~!@#$%^&*()_-+={}[]:;|/ ", address.value))
	{
		alert("'Address' fields only allow alpha-numeric characters");
		address.focus();
		return false;
	}

	var type = document.getElementById("type");
	if(type.value==""){
	   alert("Please enter 'Type'.");
	   type.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-/ ", type.value))
	{
		alert("'Type' fields only allow alpha-numeric characters");
		type.focus();
		return false;
	}


	var trades_specify = document.getElementById("trades_specify");
	if(trades_specify.value==""){
	   alert("Please enter 'Other Specifications'.");
	   trades_specify.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-/@()*&%$#!}{[]?><~ ", trades_specify.value))
	{
		alert("'Other Specifications' fields only allow alpha-numeric characters");
		trades_specify.focus();
		return false;
	}


	var card_type = document.getElementById("card_type");
	if(card_type.value==""){
	   alert("Please enter 'Type of Card'.");
	   card_type.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-/ ", card_type.value))
	{
		alert("'Type of Card' fields only allow alpha-numeric characters");
		card_type.focus();
		return false;
	}


	var card_number = document.getElementById("card_number");
	if(card_number.value==""){
	   alert("Please enter 'Card Number'.");
	   card_number.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-/ ", card_number.value))
	{
		alert("'Card Number' fields only allow alpha-numeric characters");
		card_number.focus();
		return false;
	}


	var card_expiry_date = document.getElementById("card_expiry_date");
	if(card_expiry_date.value==""){
	   alert("Please enter 'Expiry Date'.");
	   card_expiry_date.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890- ", card_expiry_date.value))
	{
		alert("'Expiry Date' fields only allow alpha-numeric characters");
		card_expiry_date.focus();
		return false;
	}
	var company_name = document.getElementById("company_name");
	if(company_name.value=="")
	{
	   alert("Please enter 'Company Name'.");
	   company_name.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:_-/()<>{}[]| ", company_name.value))
	{
		alert("'Company Name' fields only allow alpha-numeric characters");
		company_name.focus();
		return false;
	}

	var duration = document.getElementById("duration");
	if(duration.value=="")
	{
	   alert("Please enter 'Duration'.");
	   duration.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:_-/()<>{}[]| ", duration.value))
	{
		alert("'Duration' fields only allow alpha-numeric characters");
		duration.focus();
		return false;
	}
	return true;
}
function validateNeedToKnowMore()
{
	var name = document.getElementById("name");
	if(name.value==""){
	   alert("Please enter 'Name'.");
	   name.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_:.- ", name.value))
	{
		alert("Name fields only allow alpha-numeric characters");
		name.focus();
		return false;
	}
	var position = document.getElementById("position");
	if(position.value==""){
	   alert("Please enter 'Position'.");
	   position.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_:.- ", position.value))
	{
		alert("Position fields only allow alpha-numeric characters");
		position.focus();
		return false;
	}
	var company_name = document.getElementById("company_name");
	if(company_name.value=="")
	{
	   alert("Please enter 'Company Name'.");
	   company_name.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:_-/()<>{}[]| ", company_name.value))
	{
		alert("'Company Name' fields only allow alpha-numeric characters");
		company_name.focus();
		return false;
	}
	var phone = document.getElementById("phone");
	if(phone.value==""){
	   alert("Please enter 'Phone'.");
	   phone.focus();
	   return false;
	}
	else if (!isValidChar("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567891234567890[]()- ", phone.value))
	{
		alert("'Phone' fields only allow numeric characters");
		phone.focus();
		return false;
	}
	var email = document.getElementById("email");
	if(!this.EmailCheck(email))
	{
		return false ;
	}

	var question1 = document.getElementById("question1");
	if(question1.value=="0" || question1.value==""){
	   alert("You must answer Q1: How did you hear about Number 8 Construction Services Ltd?");
	   question1.focus();
	   return false;
	}

	var question2 = document.getElementById("question2");
	if(question2.value=="0" || question2.value==""){
	   alert("You must answer Q2: What is the nature of your construction business?");
	   question2.focus();
	   return false;
	}

	var question3 = document.getElementById("question3");
	if(question3.value=="0" || question3.value==""){
	   alert("You must answer Q3: How many directly employed &amp; self employed workers do you currently have on your sites?");
	   question3.focus();
	   return false;
	}

	var question4 = document.getElementById("question4");
	if(question4.value=="0" || question4.value==""){
	   alert("You must answer Q4: What percentage of your workforce is currently directly employed?");
	   question4.focus();
	   return false;
	}

	var question5 = document.getElementById("question5");
	if(question5.value=="0" || question5.value==""){
	   alert("You must answer Q5: How many temporary contractors do you currently use?");
	   question5.focus();
	   return false;
	}

	var question6 = document.getElementById("question6");
	if(question6.value=="0" || question6.value==""){
	   alert("You must answer Q6: Would your business benefit from pre-planning your temporay labour needs?");
	   question6.focus();
	   return false;
	}

	var question7 = document.getElementById("question7");
	if(question7.value=="0" || question7.value==""){
	   alert("You must answer Q7: What criteria do you use when hiring temporary labour?");
	   question7.focus();
	   return false;
	}
	return true;
}
/**
 *
 * @access public
 * @return void
 **/
function checkOther(idName){
	var selObj = document.getElementById( idName );
	//alert(selObj.selectedIndex);
	var selObjOther = document.getElementById( idName+"_other_div" );
	if(selObj.options[selObj.selectedIndex].value=="Other"){
		selObjOther.style.display = 'block';
		var selObjOther = document.getElementById( idName+"_other" );
		selObjOther.focus();
	}
	else{
		selObjOther.style.display = 'none';
	}
}
