var errors, hddnCompanyId, edtCompanyName, edtCompanyEmail, edtCompanyLogo, edtCompanyAddress, edtCompanyURL, edtCompanyPhone, edtCompanyType, edtCompanyRegNo, edtCompanyRegNoYear, edtCompanyRegNoMid, edtCompanyRegNoType, edtUserName, edtUserSurname, edtCompanyVatNo,
edtCompanyContactName, edtCompanyContactSurname, edtCompanyContactEmail, edtCompanyContactPhone;
var companyModule = baseRootURL + "company/";

function registerCompany(action)
{
	errors = false;
	validate_hddnCompanyId();
	validate_edtCompanyName();
	validate_edtCompanyEmail();
	validate_edtCompanyLogo();
	validate_edtCompanyAddress();
	validate_edtCompanyURL();
	validate_edtCompanyPhone();
	validate_edtCompanyType();
	validate_edtCompanyRegNoYear();
	validate_edtCompanyRegNoMid();
	validate_edtCompanyRegNoType();
	validate_edtCompanyVatNo();
	if (!errors)
	{
		var data = {};
		data.dummy	= ""; //we seem to get errors from the first value, so always add a dummy value first.
		data.action	= action;
		if (document.getElementById("hddnCompanyId") != null) data.hddnCompanyId = hddnCompanyId;
		data.edtCompanyName		= edtCompanyName;
		data.edtCompanyEmail	= edtCompanyEmail;
		data.edtCompanyLogo		= edtCompanyLogo;
		data.edtCompanyAddress	= edtCompanyAddress;
		data.edtCompanyURL		= edtCompanyURL;
		data.edtCompanyPhone	= edtCompanyPhone;
		data.edtCompanyType		= edtCompanyType;
		data.edtCompanyRegNo 	= edtCompanyRegNoYear+'/'+edtCompanyRegNoMid+'/'+edtCompanyRegNoType;
		data.edtCompanyVatNo 	= edtCompanyVatNo;
		data.longitude = window.lng === undefined ? '' : window.lng;
		data.latitude = window.lat === undefined ? '' : window.lat;		
		callModule("company/company_val.php", data);
	}
}

function insertCompany(){registerCompany(0)}
function modifyCompany(){registerCompany(1)}

function submitRegistration()
{
	errors = false;
	//contact information
	validate_edtCompanyContactName();
	validate_edtCompanyContactSurname();
	validate_edtCompanyContactEmail();
	validate_edtCompanyContactPhone();
	
	//Business information
	validate_edtCompanyName();
	validate_edtCompanyEmail();
	validate_edtCompanyPhone();
	validate_edtCompanyAddress();
	
	validate_edtCompanyRegNoYear();
	validate_edtCompanyRegNoMid();
	validate_edtCompanyRegNoType();
	validate_edtCompanyVatNo();	
	
	validate_edtPwd();
	validate_edtConfirm();	

	if (!errors)
	{
		validate_cbAgree();
		if (!errors)
		{
			var data = {};
			data.dummy	= ""; //we seem to get errors from the first value, so always add a dummy value first.
			data.action = 5;
			data.edtCompanyContactName		= edtCompanyContactName;
			data.edtCompanyContactSurname	= edtCompanyContactSurname;
			data.edtCompanyContactEmail		= edtCompanyContactEmail;
			data.edtCompanyContactPhone		= edtCompanyContactPhone;
	
			data.edtCompanyName		= edtCompanyName;
			data.edtCompanyEmail	= edtCompanyEmail;
			data.edtCompanyPhone	= edtCompanyPhone;
			data.edtCompanyAddress	= edtCompanyAddress;
			data.edtCompanyRegNo 	= edtCompanyRegNoYear+'/'+edtCompanyRegNoMid+'/'+prependZeros(2, edtCompanyRegNoType);
			data.edtCompanyVatNo 	= edtCompanyVatNo;
			
			data.edtPwd 			= edtPwd;
			data.edtConfirm 		= edtConfirm;

			callModule("company/company_val.php", data);
		}
	}
};

function validate_cbAgree(){cbAgree = validateRequiredCheckbox("cbAgree", "Please agree to our terms & conditions.");}

function validate_hddnCompanyId(){if (document.getElementById("hddnCompanyId") != null) hddnCompanyId=document.getElementById("hddnCompanyId").value;}
function validate_edtCompanyName(){edtCompanyName=validateRequiredText("edtCompanyName", "This field is required.");}
function validate_edtCompanyEmail(){edtCompanyEmail=validateRequiredEmail("edtCompanyEmail", "This field is required.");}
function validate_edtCompanyLogo(){edtCompanyLogo=validateImage("edtCompanyLogo", baseRootURL+"media/images/profile/profile.png");}
function validate_edtCompanyAddress(){edtCompanyAddress=validateTextArea("edtCompanyAddress", "Address is required.", true);}
function validate_edtCompanyURL(){edtCompanyURL=document.getElementById('edtCompanyURL').value;}
function validate_edtCompanyPhone(){edtCompanyPhone=validatePhoneNumber("edtCompanyPhone", "Phone number is required.", true);}
function validate_edtCompanyType(){edtCompanyType=validateSelection("edtCompanyType", "Please select the company type.");}

function validate_edtCompanyRegNoYear(){edtCompanyRegNoYear=validateRegNumber("edtCompanyRegNoYear", "A valid registration number is required.");}
function validate_edtCompanyRegNoMid(){edtCompanyRegNoMid=validateRegNumber("edtCompanyRegNoMid", "A valid registration number is required.");}
function validate_edtCompanyRegNoType()
{
	var fieldValue = document.getElementById("edtCompanyRegNoType").value;
	if(fieldValue.length===1){document.getElementById("edtCompanyRegNoType").value = '0'+fieldValue;}
	edtCompanyRegNoType=validateRegNumber("edtCompanyRegNoType", "A valid registration number is required.");
}

function validateRegNumber(field, errorMessage)
{
	var fieldValue = document.getElementById(field).value;
	if (fieldValue) 
	{
		if ((!isNumber(fieldValue)))
		{
			errors = true;
			handleError('edtCompanyRegNo', "Please enter a numeric value only.");
		}
		else 
		{
			fieldValue = (fieldValue * 1);
			if (fieldValue <= 0)
			{
				errors = true;
				handleError('edtCompanyRegNo', "Please enter a value greater than zero.");
			}
			else 
			{
				clearError('edtCompanyRegNo');
			}
		}
	}
	return fieldValue;
}
function validate_edtCompanyVatNo(){edtCompanyVatNo=document.getElementById("edtCompanyVatNo").value;}

function validate_edtUserName(){edtUserName=validateRequiredText("edtUserName", "First name is required.");}
function validate_edtUserSurname(){edtUserSurname=validateRequiredText("edtUserSurname", "Surname is required.");}
function validate_edtPwd(){edtPwd=validatePassword("edtPwd", "edtConfirm", false)}
function validate_edtConfirm(){/*password confirmation taken care of above.*/}

function validate_edtCompanyContactName()
{
	edtCompanyContactName=validateRequiredText("edtCompanyContactName", "Please enter the contact's name.");
}

function validate_edtCompanyContactSurname()
{
	edtCompanyContactSurname=validateRequiredText("edtCompanyContactSurname", "Please enter the contact's surname.");
}

function validate_edtCompanyContactEmail()
{
	edtCompanyContactEmail=validateRequiredEmail("edtCompanyContactEmail", "A contact email address is required.");
}

function validate_edtCompanyContactPhone()
{
	edtCompanyContactPhone=validatePhoneNumber("edtCompanyContactPhone", "Contact phone number required.", false);
}

function viewCompany(id)
{
	var data = {}
	data.dummy = "";
	data.hddnCompanyId = id;
	data.action = 2;
	callModule("company/company_val.php", data);
}

function editCompany(id)
{
	var data = {}
	data.dummy = "";
	data.hddnCompanyId = id;
	data.action = 3;
	callModule("company/company_val.php", data);
}

function editContacts(id)
{
	var data = {}
	data.dummy = "";
	data.hddnCompanyId = id;
	data.action = 6;
	callModule("company/company_val.php", data);
}

function editEmailGroup(id)
{
	var data = {}
	data.dummy = "";
	data.hddnCompanyId = id;
	data.action = 8;
	callModule("company/company_val.php", data);}

function duplicateCompany(id, desc)
{
	if (confirm("Are you sure you want to duplicate "+desc+"?"))
	{
	var data = {}
	data.dummy = "";
	data.hddnCompanyId = id;
	data.action = 7;
	callModule("company/company_val.php", data);
	}
}

function deleteCompany(id, desc)
{
	if (confirm("Are you sure you want to delete "+desc+" and all associated records?"))
	{
	var data = {}
	data.dummy = "";
	data.hddnCompanyId = id;
	data.action = 4;
	callModule("company/company_val.php", data);
	}
}