function trimAll(TxtValue)
{
	try
	{
		while (TxtValue.substring(0,1) == ' ')
		{
			TxtValue = TxtValue.substring(1, TxtValue.length);
		}
		while (TxtValue.substring(TxtValue.length-1, TxtValue.length) == ' ')
		{
			TxtValue = TxtValue.substring(0,TxtValue.length-1);
		}
		return TxtValue;
	}
	catch(e)
	{
		alert(e.toString())
		alert(e.message.toString())
		alert('1')
		return false
	}
}

function ClearSpan(Id)
{
	for(x in Id)
	{
		try
		{
			document.getElementById('span_'+Id[x]).innerHTML = ''
		}
		catch(e)
		{}
	}
}

function ClearSpanText(Id)
{
	try
	{
	if(document.getElementById('span_' + Id) != null)
		{
		document.getElementById('span_'+Id).innerHTML = ''
		}
	}
	catch(e)
	{}
}

function CreateSpan(Id,ErrorValue,Css)
{
	try
	{
		if(document.getElementById('span_' + Id) != null)
		{
			try
			{
				document.getElementById('span_'+Id).innerHTML = ''
				if(Css != null)
					document.getElementById('span_'+Id).className = Css
				else
					document.getElementById('span_'+Id).className = ''
				if(ErrorValue != null)
					document.getElementById('span_'+Id).innerHTML = '&nbsp;' + ErrorValue
				else
					document.getElementById('span_'+Id).innerHTML = '&nbsp;*'
			}
			catch(e)
			{
				alert(e.toString());
				alert(e.message.toString());
				alert('2')
				return false
			}
		}
		else
			throw e
	}
	catch(e)
	{
		var ObjSpan = document.createElement('span')
		var ObjParent = document.getElementById(Id).parentNode
		ObjParent.appendChild(ObjSpan)
		ObjSpan.id = 'span_' + Id
		if(Css != null)
			ObjSpan.className = Css
		else
			ObjSpan.className = ''
		if(ErrorValue != null)
			ObjSpan.innerHTML = '&nbsp;' + ErrorValue
		else
			ObjSpan.innerHTML = '&nbsp;*'
	}
}

function RequiredField(Id, ErrorValue, Css)
{
	isvalid = true
	for(x in Id)
	{
		try
		{
			if(document.getElementById(Id[x]) == null)
			{
				alert(e.toString())
				alert('3')
				throw e
			}
		}
		catch(e)
		{
			alert('Control name "' + Id[x] + '" not found');
			return false;
		}		
		if(trimAll(document.getElementById(Id[x]).value) == '')
		{
			try
			{				
				CreateSpan(Id[x],ErrorValue,Css)
			}
			catch(e)
			{
				alert(e.toString());
				alert(e.message.toString());
				alert('4')
			}
			finally
			{
				isvalid = false
			}
		}
		else
			ClearSpanText(Id[x])
		
	}
	return isvalid
}
function CreateBorder(Id)
{
	try
	{
		document.getElementById(Id).className='inputText_Valid_Wrong';
	}
	catch(e)
	{
		
	}
}
function ClearBorder(Id)
{
	try
	{
		document.getElementById(Id).className='inputText';
	}
	catch(e)
	{
		
	}
}
function RequiredFieldWithBorder(Id)
{
	isvalid = true
	for(x in Id)
	{
		try
		{
			if(document.getElementById(Id[x]) == null)
			{
				alert(e.toString())
				alert('3')
				throw e
			}
		}
		catch(e)
		{
			alert('Control name "' + Id[x] + '" not found');
			return false;
		}		
		if(trimAll(document.getElementById(Id[x]).value) == '')
		{
			try
			{				
				CreateBorder(Id[x])
			}
			catch(e)
			{
				alert(e.toString());
				alert(e.message.toString());
				alert('4')
			}
			finally
			{
				isvalid = false
			}
		}
		else
			ClearBorder(Id[x])
		
	}
	return isvalid
}

function CompareField(Id, Id1, ErrorValue, Css)
{
	isvalid = true;
	
	for(x in Id1)
	{
		try
		{
			document.getElementById('span_'+Id1[x]).innerHTML = ''
		}
		catch(e)
		{}
	}
	for(x in Id)
	{
		try
		{
			if(document.getElementById(Id[x]) == null)
				throw e
		}
		catch(e)
		{
			alert('Control name "' + Id[x] + '" not found');
			return false;
		}
		try
		{
			if(document.getElementById(Id1[x]) == null)
			throw e
		}
		catch(e)
		{
			alert('Control name "' + Id1[x] + '" not found');
			return false;
		}
		if(document.getElementById(Id[x]).value != document.getElementById(Id1[x]).value)
		{
			try
			{
				CreateSpan(Id1[x],ErrorValue,Css)
			}
			catch(e)
			{
				alert(e.toString());
				alert(e.message.toString());
				alert('5')
			}
			finally
			{
				if(isvalid)
					isvalid = false
			}
		}
	}
	return isvalid
}

function CheckTextLength(Id, ErrorValue, Css, Length)
{
	try
	{
		if(document.getElementById(Id) == null)
			throw e
	}
	catch(e)
	{
		alert('Control name "' + Id + '" not found');
		return false;
	}
	
	Value = trimAll(document.getElementById(Id).value)
	
	if(Value == '')
	{
		CreateSpan(Id, 'Required', Css)
		return false
	}
	ClearSpanText(Id)
	
	if(Value.length < Length)
	{
		try
		{
			CreateSpan(Id,ErrorValue,Css)
		}
		catch(e)
		{
			alert(e.toString());
			alert(e.message.toString());
			alert('6')
		}
		finally
		{
			return false
		}
	}
}

function CheckExactTextLength(Id, ErrorValue, Css, Length)
{
	try
	{
		if(document.getElementById(Id) == null)
			throw e
	}
	catch(e)
	{
		alert('Control name "' + Id + '" not found');
		return false;
	}
	
	Value = trimAll(document.getElementById(Id).value)
	
	if(Value == '')
	{
		CreateSpan(Id, 'Required', Css)
		return false
	}
	
	ClearSpanText(Id)
	
	if(Value.length != Length)
	{
		try
		{
			CreateSpan(Id,ErrorValue,Css)
		}
		catch(e)
		{
			alert(e.toString());
			alert(e.message.toString());
			alert('7')
		}
		finally
		{
			return false
		}
	}
}

function ValidateEmail(Id)
{
	try
	{
		var reg1 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
		var reg2 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
		
		Value = trimAll(document.getElementById(Id).value)
		
//		if(Value == '')
//		{
//			CreateSpan(Id, '*', 'SpanStyle')
//			return false
//		}
		
//		ClearSpanText(Id)
		if(Value != '')
		{
		if(! reg1.test(document.getElementById(Id).value) || reg2.test(document.getElementById(Id).value))
		{
			CreateSpan(Id, 'Invalid', 'SpanStyle')
			return false
		}
		}
		return true
	}
	catch(e)
	{
		alert(e.toString())
		alert(e.message.toString())
		alert('8')
		return false 
	}
}

function ValidatePhoneNumber(Id)
{
	try
	{
		var values = trimAll(document.getElementById(Id).value)
				
		if(values == '')
		{
			CreateSpan(Id, 'Required', 'SpanStyle')
			return false
		}
		ClearSpanText(Id)
		
		var objRegExp=/^\d{3}-\d{3}-\d{4}$/
		var objRegExp1=/^\d*$/
		
		if(objRegExp.test(values))
			return true
		
		if(objRegExp1.test(values))
			return true
		
		CreateSpan(Id, 'Invalid phone number', 'SpanStyle')
		return false
}
	catch(e)
	{
		alert(e.toString())
		alert(e.message.toString())
		alert('9')
		return false 
	}
}

function IsNumber(Id)
{
	try
	{
		var Value = trimAll(document.getElementById(Id).value)
		
		if(Value == '')
		{
			CreateSpan(Id, '*', 'SpanStyle')
			return false
		}
		
		ClearSpanText(Id)
		
		if(isNaN(Value) || Value < 0)
		{
			CreateSpan(Id, 'Invalid', 'SpanStyle')
			return false
		}
		else
			return true
	}
	catch(e)
	{
		alert(e.toString())
		alert(e.message.toString())
		alert('10')
		return false
	}
}

function IsDecimal(Id)
{
	try
	{
		var Value = trimAll(document.getElementById(Id).value)
		
		if(Value == '')
		{
			CreateSpan(Id, '*', 'SpanStyle')
			return false
		}
		
		ClearSpanText(Id)
		
		if(!isFinite(Value) || Value < 0)
		{
			CreateSpan(Id, 'Invalid', 'SpanStyle')
			alert('a');
			return false
		}
		else
			return true
	}
	catch(e)
	{
		alert(e.toString())
		alert(e.message.toString())
		alert('10')
		return false
	}
}

function CreditCardValidation(Id)
{
	try
	{
		var ccNumber = document.getElementById(Id).value
		var bOdd = true
		var sum = new Number(0)
		var num = new Number(0)
		
		if(ccNumber == '')
		{
			CreateSpan(Id, 'Required', 'SpanStyle')
			return false
		}
		
		ClearSpanText(Id)

		
		// check for a valid length
		if (ccNumber.length <13 || ccNumber.length> 16)
		{
			CreateSpan(Id, 'Invalid', 'SpanStyle')
			return false
		}
		
		// beginning with the rightmost digit, 
		// multiply the number by 1 if
		// the position is odd, or 2 if the position is even.
 		// if the product is greater than 9, the units and tens 
		// position are added together. 
		// if the final answer is divisble by 10, it's good...
		
		for (var i = ccNumber.length - 1; i >= 0; i--)
		{
			num = Number(ccNumber.substr(i,1));
			if ( bOdd ) 
				sum = sum + (num * 1);
			else
			{
				num = (num * 2);
				var tString = String(num);
				var n = (tString.length -1);
				for (n; n>=0; n--)
					sum = sum + Number(tString.substr(n,1))
			}
		
			bOdd = !bOdd;
		}
		
		if ((sum % 10) != 0)
		{
			CreateSpan(Id, 'Invalid creditcard number', 'SpanStyle')
			return false
		}
		
		// once we know the number is good, loop the card types
		// for unmatched numbers, return a blank 
		// for valid cards, return the card name 
		
		if (Number(ccNumber.length) == 16 && ccNumber.substring(0,2)>="51" && ccNumber.substring(0,2)<="55")
			return true //return "MASTER";
		if (Number(ccNumber.length) == 16 && ccNumber.substring(0,1)=="4")
			return true	//return "VISA";
		if (Number(ccNumber.length) == 13 && (ccNumber.substring(0,1)=="4")) 
			return true	//return "VISA";
		if (Number(ccNumber.length) == 16 && ccNumber.substring(0,4)=="6011") 
			return true	//return "DISCOVER";
		if (Number(ccNumber.length) == 15 && (ccNumber.substring(0,2)=="34" || ccNumber.substring(0,2)=="37"))
			return true	//return "AMEX";
		if (Number(ccNumber.length) == 14 && (ccNumber.substring(0,2)=="36" || ccNumber.substring(0,2)=="30" || ccNumber.substring(0,2)=="38"))
			return true	//return "DINERSCLUB";
		
		CreateSpan(Id, 'Invalid', 'SpanStyle')
		return false // return ""; // unknown credit card type
	}
	catch(e)
	{
		alert(e.toString())
		alert(e.message.toString())
		alert('11')
		return false
	}
}

function ABAValidation(Id)
{
	try
	{
		var Value = trimAll(document.getElementById(Id).value)		
		n = 0;		
		if(Value == '')
		{
			CreateSpan(Id, 'Required', 'SpanStyle')
			return false
		}
			
		ClearSpanText(Id)
		
		
		for (i = 0; i < Value.length; i += 3)
			n += parseInt(Value.charAt(i),10) * 3 + parseInt(Value.charAt(i + 1), 10) * 7 + parseInt(Value.charAt(i + 2), 10)

		if (n != 0 && n % 10 == 0)
			return true
		else
		{
			CreateSpan(Id, 'Invalid', 'SpanStyle')
			return false
		}
	}
	catch(e)
	{
		alert(e.toString())
		alert(e.message.toString())
		alert('12')
		return false
	}
}

function checkDate(Id, Id1)
{
	try
	{
		Date1 = document.getElementById(Id).value
		Date2 = trimAll(document.getElementById(Id1).value)
		
		if(Date2 == '')
		{
			CreateSpan(Id, 'Required', 'SpanStyle')
			return false
		}
		
		ClearSpanText(Id1)

		if(Date1.length > 0)
		{
			var SerDate = new Date(Date1);
			var CliDate = new Date(Date2);
			
			if(validateUSDate(Date2))
			{
				if(SerDate > CliDate)
				{
					CreateSpan(Id1, 'Activation Date must be greater than or equal to Current date', 'SpanStyle')
					return false;
				}			
				else
					return true
			}
			else
				alert('Date is not correct format');
		}
	}
	catch(e)
	{
		alert(e.toString())
		alert(e.message.toString())
		alert('13')
		return false
	}
}	

function Check_H_ChkList(Id)
{
	try
	{
		ClearSpanText(Id)
		
		for(var i=0; i<document.getElementById(Id).rows[0].cells.length;i++)
		{
			if(document.getElementById('ChkLstEdit_' + i).checked)
				return true
		}
		
		CreateSpan(Id, 'Required', 'SpanStyle')
		return false
	}
	catch(e)
	{
		alert(e.toString())
		alert(e.message.toString())
		alert('14')
		return false
	}
}

function FillText(Id, Value)
{
	try
	{
		for(x in Id)
		{
			try
			{
				if(document.getElementById(Id[x]) == null)
					throw e
			}
			catch(e)
			{
				alert('Control name "' + Id[x] + '" not found');
				return false;
			}
			
			document.getElementById(Id[x]).value = Value
			document.getElementById(Id[x]).disabled = true
			document.getElementById(Id[x]).className = 'ReadOnly'
		}
	}
	catch(e)
	{
		alert(e.toString())
		alert(e.message.toString())
		alert('15')
		return false
	}
}

function ClearText(Id)
{
	try
	{
		for(x in Id)
		{
			try
			{
				if(document.getElementById(Id[x]) == null)
					throw e
			}
			catch(e)
			{
				alert('Control name "' + Id[x] + '" not found');
				return false;
			}
			
			document.getElementById(Id[x]).value = ''
			document.getElementById(Id[x]).disabled = false
			document.getElementById(Id[x]).className = ''
		}
	}
	catch(e)
	{
		alert(e.toString())
		alert(e.message.toString())
		alert('16')
		return false
	}
}

function ClearDemoText(Id, Text)
{
	try
	{		
		for(x in Id)
		{
			try
			{
				if(document.getElementById(Id[x]) == null)
					throw e
			}
			catch(e)
			{
				alert('Control name "' + Id[x] + '" not found');
				return false;
			}
			
			if(document.getElementById(Id[x]).value.toUpperCase() == Text)
			{
				document.getElementById(Id[x]).value = ''
				document.getElementById(Id[x]).disabled = false
				document.getElementById(Id[x]).className = ''
			}
		}
	}
	catch(e)
	{
		alert(e.toString())
		alert(e.message.toString())
		alert('17')
		return false
	}
}

function validateUSDate(strValue) 
{
	try
	{		
		var syed = new Date(strValue)
		var tdt = ((syed.getMonth()/1)+1) +"/" + syed.getDate()+"/" + syed.getFullYear()

		if (tdt!=strValue) 
			return false
		else
			return true
	}
	catch(e)
	{
		alert(e.toString())
		alert(e.message.toString())
		alert(e.number)
		return false
	}
}
function ValidateUKPhoneNumber(Id)
{
	try
	{
		var values = trimAll(document.getElementById(Id).value)
				
		if(values == '')
		{
			CreateSpan(Id, 'Required', 'SpanStyle')
			return false
		}
		ClearSpanText(Id)
		
		var objRegExp=/^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$/;
		
		if(objRegExp.test(values))
			return true
		
		
		alert('Invalid phone number');
		CreateBorder(Id);
		//CreateSpan(Id, 'Invalid phone number', 'SpanStyle')
		return false
    }
	catch(e)
	{
		alert(e.toString())
		alert(e.message.toString())
		
		return false 
	}
}
function ValidateUKMobileNumber(Id)
{
	try
	{
		var values = trimAll(document.getElementById(Id).value)
				
		if(values == '')
		{
			CreateSpan(Id, 'Required', 'SpanStyle')
			return false
		}
		ClearSpanText(Id)
		
		var objRegExp=/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/;
		
		if(objRegExp.test(values))
			return true
		
		
		alert('Invalid phone number');
		CreateBorder(Id);
		//CreateSpan(Id, 'Invalid phone number', 'SpanStyle')
		return false
    }
	catch(e)
	{
		alert(e.toString())
		alert(e.message.toString())
		
		return false 
	}
}