function fnEmailValidation(strTxtCtrlValue,strDelimiter,strMaxLength)
{
	var strAtSym
	var strPeriod
	var strSpace
	var strLength

	if (fnMandatory(strTxtCtrlValue) == false) // This includes Null and Space check
		return false;
	if (strTxtCtrlValue.length > strMaxLength)
		return false;

	strTxtCtrlValue = fnTrim(strTxtCtrlValue);
	var regExpression = /[^0-9a-zA-Z][^0-9a-zA-Z]/;
	
	if (regExpression.test(strTxtCtrlValue))
		return false;
	
	var arrEmailIds = new Array(); 
	var strFieldValue = strTxtCtrlValue;
	if (strDelimiter == "")
	{
		arrEmailIds[0] = strTxtCtrlValue
	}
	else
	{
		arrEmailIds = strFieldValue.split(strDelimiter); // Split to get individual email ids using the Delimiter 
	}	
	for (intk = 0;intk < arrEmailIds.length;intk++ )
	{
		if (arrEmailIds[intk] == "") // Null check
			return false;
			
		arrEmailIds[intk] = fnTrim(arrEmailIds[intk]);
		if (arrEmailIds[intk].length == 0 )			 // Spaces Check
			return false;
				
		if (arrEmailIds[intk].search(/\s+/) != -1 ) // checking space in between email id
			return false;
		var arrSplit = new Array();
		arrSplit = 	arrEmailIds[intk].split("@");
		if ( arrSplit.length != 2) 				 // Only one @ is allowed in an email id
			return false;
	
		// Call Function for Alphanumeric Check along with special character (. - _) 
		// for the string before @ and pass 0 in maxlength as maxlength check not required
		
		if (fnIsAlphaNumeric(arrSplit[0],"M","0",".-_") == false)
			return false;
		
		//Check whether first charater is in a-z /A-Z
		var strValidchar = arrSplit[0].substring(0,1)
		if (!((strValidchar >= "A") && (strValidchar <= "Z")) &&  !((strValidchar >= "a") && (strValidchar <= "z")))
			return false
		
		// Call Function for IsAlpha Check along with special character (.) 
		// for the string after @ and pass 0 in maxlength as maxlength check not required
		if (fnIsAlphaNumeric(arrSplit[1],"M","0",".-_")== false)
			return false;
		
		//Anil ------------------- commented to allow number after @
		//Check whether first charater is in a-z /A-Z
		//strValidchar = arrSplit[1].substring(0,1)
		//if (!((strValidchar >= "A") && (strValidchar <= "Z")) &&  !((strValidchar >= "a") && (strValidchar <= "z")))
		//	return false
		//Check whether last character is in a-z /A-Z
		strValidchar = arrSplit[1].substring(arrSplit[1].length-1,arrSplit[1].length)
		
		if (!((strValidchar >= "A") && (strValidchar <= "Z")) &&  !((strValidchar >= "a") && (strValidchar <= "z")))
			return false
		
		//Every portion of the email address after @ must be of <= 2 characters
		var arrEmails
		arrEmails = arrSplit[1].split("."); 
			
		for (intm = 0;intm < arrEmails.length;intm++ )
		{
			if (arrEmails[intm].length < 2)
				return false;
		}
					
		strAtSym=arrEmailIds[intk].indexOf('@')
		strPeriod=arrEmailIds[intk].lastIndexOf('.')
		strSpace=arrEmailIds[intk].indexOf(' ')
		strLength=arrEmailIds[intk].length-1
		if ((strAtSym < 1) ||(strPeriod <= strAtSym+1)|| (strPeriod==strLength) ||(strSpace!=-1))
			return false;
		
	}
			
	return true;
}


function fnTrim(strString)
{

	// white space consist of (blank,tab,newline)
	var intLeftIndex = 0; //Store position of first non-white space from leftmost side
	var intRightIndex = 0; //Store position of first non-white space from rightmost side
	var blnFound = false; //Check for any non-white space character
	var regExp = /\S+/; 
	var intCount;
	if (strString.search(regExp) == -1) //Check for non-white space character
	{
		strString = ""; //Valid character not found then return empty string
		return(strString);
	}

	//If  atleast one non-white space character found.
	for (intCount=0;intCount < strString.length; intCount++)
	{
		if (strString.charAt(intCount) != " ") // Checking for first non-white spaces 
											// from left side
		{
			intLeftIndex = intCount - 1;
			break;
		}
	}
	for (intCount=strString.length - 1;intCount >= 0; intCount--)
	{
		if (strString.charAt(intCount) != " ") // Checking for first non-white spaces 
											// from Right most side
		{
			intRightIndex = intCount + 1;
			break;
		}
	}

	strString=strString.substring(intLeftIndex+1,intRightIndex); //Remove leading and trailing
														// spaces
	return (strString);
}


function fnIsAlphaNumeric(strString,chrFormat,intMaxLength,strSpecialChar)
{
	
	var regExp;
	
	strString = fnTrim(strString); //Remove leading and trailing spaces
	var regSpChar = /([\$\@\#\%\^\&\*\(\)\[\]\+\_\{\}\`\|\-\\])/g;
	strSpecialChar = strSpecialChar.replace(regSpChar,"\\$1") //Take care of special character in RegExp
	if (intMaxLength > 0) 
	{
		if (strString.length > intMaxLength)
		{
			//alert ("Invalid Maxlength") //Return False if no. of  characters are greater than 
			return false				//specified max length
			
		}
	}

	switch(chrFormat) //Validate character against Format passed as parameter.
	{
		case "U": //All Upper case
			regExp = new RegExp("\[\^A-Z0-9" + strSpecialChar + "\]");
			if (regExp.test(strString)) 
			{
				//alert ("Invalid Characters");
				return false;
			}
			break;
		case "L": // All Lower Case
			regExp = new RegExp("\[\^a-z0-9" + strSpecialChar + "\]");
			if (regExp.test(strString)) 
			{
				//alert ("Invalid Characters");
				return false;
			}
			break;
		case "M": //Mixed Case
			regExp = new RegExp("\[\^a-zA-Z0-9" + strSpecialChar + "\]");
			if (regExp.test(strString)) 
			{
				//alert ("Invalid Characters");
				return false;
			}
			break;
		case "P": //Proper Case
			var arrWords;
			arrWords = strString.split(/\s+/) //Split string in words
			for (var i=0 ;i<arrWords.length; i++)
			{
				if (arrWords[i].search(/^[^A-Z]|\S[A-Z]/) != -1)//Check first letter of each word 
				{										// must be Capital Letter
					//alert ("Invalid Characters");
					return false;
				}
			}
			regExp = new RegExp("\[\^a-zA-Z0-9" + strSpecialChar + "\]");
			if (regExp.test(strString)) 
			{
				//alert ("Invalid Characters");
				return false;
			}
			break
		default:
			//alert ("incorrect  format");//Format Passed as a parameter is not valid
			return false;
	}
			
	//alert ("Valid Characters");
	return true; //Valid Character
}

function fnIsNumeric(strInteger)
{
	/*Generate Regular expression which will check 
	1) If (+,-) sign exist then  it must be the first character.
	2) Input value is a valid real number
	*/
	var regExp = /^[\-\+]{0,1}\d{0,100}\.{0,1}\d{1,100}$/
	strInteger = fnTrim(strInteger); //Remove leading and trailing spaces
	if (regExp.test(strInteger)) //Validate Integer
	{
		//alert ("valid number");
		return true;
	}
	//alert ("invalid");
	return false; //Not a valid integer
}

function fnMandatory(strTxtCtrlValue)
{
	if (strTxtCtrlValue == "")
        return false
    
    strTxtCtrlValue = fnTrim(strTxtCtrlValue)
    if (strTxtCtrlValue.length == 0)
       return false
    
	return true
}

function visibleBox(id,visiblehide) 
{
	elm3 = document.getElementById("box"+id);
	if(!visiblehide)
	{
		if(elm3.style.display=="none")
		{
			visiblehide=""
		}
		else
		{
			visiblehide="none"
		}
	}
	showHideBox(id,visiblehide)
	for(i=1;i<=100;i++)
	{
		if(!document.getElementById("box"+i))break;
		if(i!=parseInt(id))
		{
			showHideBox(""+i,"none")
		}
	}	
}
function showHideBox(id,visiblehide)
{
	elm1 = document.getElementById("open"+id);
	elm2 = document.getElementById("close"+id);
	elm3 = document.getElementById("box"+id);
	if(!elm3)return false;
	if(elm1) 
	{
		if (visiblehide == "none") 
		{
			elm1.style.display = "none";
			if(elm2)elm2.style.display = "";
			elm3.style.display = "none";
		}
		else 
		{
			elm1.style.display = "";
			if(elm2)elm2.style.display = "none";
			elm3.style.display = "";
		}
	}
}

function gal()
{
	window.location.href="gallery.php"
}

function checkvat()
{
	if(document.clientdetails.vat[1].checked){
		document.clientdetails.vatperc.disabled=true;
	}else{
		document.clientdetails.vatperc.disabled=false;
	}	
}

function checksuppvat()
{
	if(document.supplierdetails.vat[1].checked){
		document.supplierdetails.vatperc.disabled=true;
	}else{
		document.supplierdetails.vatperc.disabled=false;
	}	
}

function checkclient()
{	
	clientid	= document.clientdetails.clientid.value;
	clientname	= document.clientdetails.clientname.value;
	contactper	= document.clientdetails.contactpername.value;
	address		= document.clientdetails.address.value;
	phone		= document.clientdetails.phone.value;
	email		= document.clientdetails.email.value;
	currency	= document.clientdetails.currencyid.value;
	daystopay	= document.clientdetails.daystopay.value;
	notifyday	= document.clientdetails.notifydays.value;
	supportplan	= document.clientdetails.supportplan.value;

	errmsg = "";
	submitflag = true;

	if(!fnMandatory(clientid)){
		errmsg += "Please enter the clientid.\n"; 
		submitflag = false;
	}
	if(!fnMandatory(clientname)){
		errmsg += "Please enter the clientname.\n"; 
		submitflag = false;
	}
	if(!fnMandatory(contactper )){
		errmsg += "Please enter the contact person name.\n"; 
		submitflag = false;
	}
	if(!fnMandatory(address)){
		errmsg += "Please enter the address.\n"; 
		submitflag = false;
	}
	if(!fnMandatory(phone)){
		errmsg += "Please enter the phone.\n"; 
		submitflag = false;
	}else if(!fnIsNumeric(phone)){
		errmsg += "Please enter the correct phone no \n";
		submitflag = false;
	}
	if(!fnMandatory(email)){
		errmsg += "Please enter the email.\n";
		submitflag = false;
	}else if (!fnEmailValidation(email,"",100)){
			errmsg += "Please enter the correct email.\n";
			submitflag = false;
	}
	if(!fnMandatory(currency)){
		errmsg += "Please select the currency.\n"; 
		submitflag = false;
	}
	if(!fnMandatory(daystopay)){
		errmsg += "Please enter the days to pay .\n"; 
		submitflag = false;
	}else if(!fnIsNumeric(daystopay)){
		errmsg += "Please enter the correct days \n";
		submitflag = false;
	}
	if(!fnMandatory(notifyday)){
		errmsg += "Please enter the notification days .\n"; 
		submitflag = false;
	}else if(!fnIsNumeric(notifyday)){
		errmsg += "Please enter the correct notification days \n";
		submitflag = false;
	}
	if(!fnMandatory(supportplan)){
		errmsg += "Please enter the support plan.\n"; 
		submitflag = false;
	}

	if(submitflag){
		document.clientdetails.action="addclient.php";
		document.clientdetails.submit();
	}else{
		alert(errmsg);
		return false;
	}

}

function checksupplier()
{	
	supplierid		= document.supplierdetails.supplierid.value;
	suppliername	= document.supplierdetails.suppliername.value;
	compdesc		= document.supplierdetails.compdesc.value;
	reference		= document.supplierdetails.reference.value;
	address			= document.supplierdetails.address.value;
	phone			= document.supplierdetails.phone.value;
	email			= document.supplierdetails.email.value;
	currency		= document.supplierdetails.currencyid.value;
	type			= document.supplierdetails.type.value;

	errmsg = "";
	submitflag = true;

	if(!fnMandatory(supplierid)){
		errmsg += "Please enter the supplierid.\n"; 
		submitflag = false;
	}
	if(!fnMandatory(suppliername)){
		errmsg += "Please enter the suppliername.\n"; 
		submitflag = false;
	}
	if(!fnMandatory(compdesc)){
		errmsg += "Please enter the company description.\n"; 
		submitflag = false;
	}
	if(!fnMandatory(reference)){
		errmsg += "Please enter the reference.\n"; 
		submitflag = false;
	}
	if(!fnMandatory(address)){
		errmsg += "Please enter the address.\n"; 
		submitflag = false;
	}
	if(!fnMandatory(phone)){
		errmsg += "Please enter the phone.\n"; 
		submitflag = false;
	}else if(!fnIsNumeric(phone)){
		errmsg += "Please enter the correct phone no \n";
		submitflag = false;
	}
	if(!fnMandatory(email)){
		errmsg += "Please enter the email.\n";
		submitflag = false;
	}else if (!fnEmailValidation(email,"",100)){
			errmsg += "Please enter the correct email.\n";
			submitflag = false;
	}
	if(!fnMandatory(currency)){
		errmsg += "Please select the currency.\n"; 
		submitflag = false;
	}
	if(!fnMandatory(type)){
		errmsg += "Please select the type .\n"; 
		submitflag = false;
	}
	
	if(submitflag){
		document.supplierdetails.action="addsupplier.php";
		document.supplierdetails.submit();
	}else{
		alert(errmsg);
		return false;
	}

}

function del()
{
	if (!confirm("Are you sure you want to delete the client?"))
	 return false; 
}

function delinvoice()
{
	if (!confirm("Are you sure you want to delete the invoice?"))
	 return false; 
}

function delexpense()
{
	if (!confirm("Are you sure you want to delete the expense?"))
	 return false; 
}

function delsupp()
{
	if (!confirm("Are you sure you want to delete the supplier?"))
	 return false; 
}

function checkinvoice()
{

	invoiceno = document.geninvoice.invoiceno.value;
	settlementduedate = document.geninvoice.settlementduedate.value;
	

	errmsg = "";
	submitflag = true;

	if(!fnMandatory(invoiceno)){
		errmsg += "Please enter the invoiceno .\n"; 
		submitflag = false;
	}

	if(!fnMandatory(settlementduedate)){
		errmsg += "Please enter the settlement date .\n"; 
		submitflag = false;
	}

	if(submitflag){
		document.geninvoice.action="generateinvoiceshow.php";
		document.geninvoice.submit();
	}else{
		alert(errmsg);
		return false;
	}
}

function checkexpense()
{

	expensedate = document.generateexpense.expensedate.value;
	cost		= document.generateexpense.cost.value;
	

	errmsg = "";
	submitflag = true;

	if(!fnMandatory(expensedate)){
		errmsg += "Please enter the date .\n"; 
		submitflag = false;
	}

	if(!fnMandatory(cost)){
		errmsg += "Please enter the cost .\n"; 
		submitflag = false;
	}else if(!fnIsNumeric(cost)){
		errmsg += "Please enter the correct cost \n";
		submitflag = false;
	}

	if(submitflag){
		document.generateexpense.action="generateexpenseshow.php";
		document.generateexpense.submit();
	}else{
		alert(errmsg);
		return false;
	}
}

function checkclientexist()
{	
	clientid = document.clientselect.selectclient.value;

	errmsg = "";
	submitflag = true;

	if(!fnMandatory(clientid)){
		errmsg += "Please select the client \n"; 
		submitflag = false;
	}

	if(submitflag){
		document.clientselect.action="generateinvoice.php";
		document.clientselect.submit();
	}else{
		alert(errmsg);
		return false;
	}
}

function checkrecurring()
{	
	frequency = document.generaterecurring.selectfrequency.value;
	date	  = document.generaterecurring.startdate.value;

	errmsg = "";
	submitflag = true;

	if(!fnMandatory(frequency)){
		errmsg += "Please select the frequency \n"; 
		submitflag = false;
	}

	if(!fnMandatory(date)){
		errmsg += "Please select the date \n"; 
		submitflag = false;
	}

	if(submitflag){
		document.generaterecurring.submit();
	}else{
		alert(errmsg);
		return false;
	}
}

function checksupplierexist()
{	
	supplierid = document.supplierselect.selectsupplierid.value;

	errmsg = "";
	submitflag = true;

	if(!fnMandatory(supplierid)){
		errmsg += "Please select the supplier \n"; 
		submitflag = false;
	}

	if(submitflag){
		document.supplierselect.action="generateexpenses.php";
		document.supplierselect.submit();
	}else{
		alert(errmsg);
		return false;
	}
}

function delitem()
{
	if (!confirm("Are you sure you want to delete the item?"))
	 return false; 
}

function exportchecks()
{
	startdate = document.details.fromdate.value;
	enddate	  = document.details.todate.value;
	invoiceno = document.details.invoiceno.value;
	
	
	errmsg = "";
	submitflag = true;
	
	if(!fnMandatory(startdate)){
		errmsg += "Please enter the start date \n";
		submitflag = false;
	}
	
	if(!fnMandatory(enddate)){
		errmsg += "Please enter the end date \n";
		submitflag = false;
	}
	
	
	if(submitflag){
		document.details.submit();
	}else{
		alert(errmsg);
		return false;
	}
	
}

function checkclientaccount()
{
	name		= document.clientdetails.accname.value;
	email		= document.clientdetails.email.value;
	
	errmsg = "";
	submitflag = true;

	if(!fnMandatory(name)){
		errmsg += "Please enter the name.\n"; 
		submitflag = false;
	}
	if(!fnMandatory(email)){
		errmsg += "Please enter the email.\n";
		submitflag = false;
	}else if (!fnEmailValidation(email,"",100)){
			errmsg += "Please enter the correct email.\n";
			submitflag = false;
	}
	
	if(submitflag){
		document.clientdetails.action="createclientaccount.php";
		document.clientdetails.submit();
	}else{
		alert(errmsg);
		return false;
	}

}

function checksupportticket()
{
	hostname	= document.tickets.hostname.value;
	priority	= document.tickets.priorityid.value;
	contactno	= document.tickets.contactno.value;
	issue		= document.tickets.issue.value;
	
	errmsg = "";
	submitflag = true;

	if(!fnMandatory(hostname)){
		errmsg += "Please enter the hostname.\n"; 
		submitflag = false;
	}
	if(!fnMandatory(priority)){
		errmsg += "Please select the priority.\n";
		submitflag = false;
	}	
	if(!fnMandatory(contactno)){
		errmsg += "Please enter the contact no.\n"; 
		submitflag = false;
	}	
	if(!fnMandatory(issue)){
		errmsg += "Please enter the issue details.\n";
		submitflag = false;
	}
	
	if(submitflag){
		document.tickets.action="confirmticket.php";
		document.tickets.submit();
	}else{
		alert(errmsg);
		return false;
	}
}

function openwin(invid)
{
	myurl = "client/advisedetails.php?invoiceid=" + invid;
	window.open (myurl, "w", "scrollbars=0,width=400,height=180");
}
function openwindow(invid)
{
	myurl = "advisedetails.php?invoiceid=" + invid;
	window.open (myurl, "w", "scrollbars=0,width=400,height=180");
}