/*
Created by: Wasiq Ashfaq
Purpose:	Provides different type of validations
Usage:		define the following attributes in any tag you want to validate
			validate = 0, 1		mandatory	this marks the object for validation
			allownull = 0, 1	mandatory	tells whether value can by null or not
			allowzero = 0, 1	optional	tells if zero is allowed if the data type is numeric
			nametag	=			mandatory	tells the name of the field, is used only for error messages
			maxlength			optional	tells the maximum length the field can hold

			onkeypress = ValidateDocument('')	optional	validates key codes against a particular event
			onpaste = ValidateDocument('')		optional	validates key codes against a particular event

			if(ValidateDocument==false)		mandatory	make this call when you want to validate the document
				return
*/


var NullError = " cannot be empty.";
var AlphaNumericError = " should be alpha numeric.";
var GreaterThanZeroError = " should be greater than zero.";
var InvalidNumberError = " is not a valid number.";
var NegativeError = " should not be less than zero.";
var NaNError = " should be a number.";

function ValidateInput(InputType){
	

	var sKey = String.fromCharCode(window.event.keyCode)
	if(InputType == "alpha"){
		if(!((window.event.keyCode>=65 && window.event.keyCode<=90) || (window.event.keyCode>=97 && window.event.keyCode<=122) || (window.event.keyCode==32)))
		{
			window.event.returnValue=false			
		}
	}
	else if(InputType == "alphanumeric"){
		if(!((window.event.keyCode>=64 && window.event.keyCode<=90) || (window.event.keyCode>=97 && window.event.keyCode<=122) || (window.event.keyCode==32)|| (window.event.keyCode>=48 && window.event.keyCode<=57) || (window.event.keyCode >= 44 && window.event.keyCode <=47) || (window.event.keyCode == 37)))
		{
			window.event.returnValue=false			
		}
	}else if(InputType == "numericdec"){ 
		if(!((window.event.keyCode>=48 && window.event.keyCode<=57) || (window.event.keyCode == 46)))
		{
			window.event.returnValue=false			
		}
		else if(isNaN(window.event.srcElement.value + '' + sKey))
			window.event.returnValue=false
	}else if(InputType == "numeric"){
		if(!((window.event.keyCode>=48 && window.event.keyCode<=57)))
		{
			window.event.returnValue=false			
		}
	}
}

function CheckEmpty(obj){  
	if(obj.value == "" ){
		alert(obj.nametag + " cannot be empty or zero")
		obj.select()
		SetFocus(obj)
		return false
	}
	return true
}

function SetFocus(obj)
{
try
	{
	if(obj.disabled == false && obj.style.display!='none')
		{
		obj.focus()
		obj.select()
		}
	}
catch(e)
	{}
}

function CheckAlpha(obj)
{ 
	var bolFound=false, j=0;
	if(obj.datatype == "alpha")	//Proceed if alpha
		{
		if(obj.allownull == "0")		//Proceed if null is not allowed
			{

			if(obj.value == "")			//If value is null then prompt and exit with error
				{

				alert(obj.nametag + NullError)
				SetFocus(obj)
				return false
				}
			}
		}
	return true
}

function CheckAlphaNumeric(obj)
{ 
var bolFound=false, j=0;
if(obj.datatype == "alphanumeric")	//Proceed if alphanumeric
	{
	if(obj.allownull == "0")		//Proceed if null is not allowed
		{
		if(obj.value == "")			//If value is null then prompt and exit with error
			{
			alert(obj.nametag + NullError)
			SetFocus(obj)
			return false
			}
		}
	if(obj.value != "")
		{
		if(isNaN(obj.value) == false)	//If only a number then prompt and exit with error
			{
			alert(obj.nametag + AlphaNumericError)
			SetFocus(obj)
			return false
			}
		bolFound = false
		for(j=0;j<obj.value.length;j++)	//Check if there is any alpha character
			{
			if(!isNaN(obj.value.charAt(j)))
				{
				bolFound = true
				break
				}
			}
		if(bolFound == false)
			{
			alert(obj.nametag + AlphaNumericError)
			SetFocus(obj)
			return false					
			}
		}
	}
return true
}

function CheckNumeric(obj)
 {  
var bolFound=false
if(obj.datatype == "numeric")
	{
	if(obj.allownull == "0")		//Proceed if null is not allowed
		{
		if(obj.value == "")			//If value is null then prompt and exit with error
			{
			alert(obj.nametag + NullError)
			SetFocus(obj)
			return false
			}//end value is ""
		}//end allownull = zero

	if(obj.value != "")
		{
		if(obj.allowzero == null || obj.allowzero == 0)		//if zero is not allowed
			{
			if(parseFloat(obj.value)<=0)
				{
				alert(obj.nametag + GreaterThanZeroError)
				SetFocus(obj)
				return false
				}
			if(isNaN(obj.value))
				{
				alert(obj.nametag + InvalidNumberError)
				SetFocus(obj)
				return false
				}					
			}
		else
			{
			if(parseFloat(obj.value)<0)
				{
				alert(obj.nametag + NegativeError)
				SetFocus(obj)
				return false
				}
			}
		if(isNaN(obj.value))
			{
			alert(obj.nametag + NaNError)
			SetFocus(obj)
			return false
			}
		}
	}
return true
}

function CheckNumericdec(obj)
 {  
var bolFound=false
if(obj.datatype == "numericdec")
	{
	if(obj.allownull == "0")		//Proceed if null is not allowed
		{
		if(obj.value == "")			//If value is null then prompt and exit with error
			{
			alert(obj.nametag + NullError)
			SetFocus(obj)
			return false
			}//end value is ""
		}//end allownull = zero

	if(obj.value != "")
		{
		if(obj.allowzero == null || obj.allowzero == 0)		//if zero is not allowed
			{
			if(parseFloat(obj.value)<=0)
				{
				alert(obj.nametag + GreaterThanZeroError)
				SetFocus(obj)
				return false
				}
			if(isNaN(obj.value))
				{
				alert(obj.nametag + InvalidNumberError)
				SetFocus(obj)
				return false
				}					
			}
		else
			{
			if(parseFloat(obj.value)<0)
				{
				alert(obj.nametag + NegativeError)
				SetFocus(obj)
				return false
				}
			}
		if(isNaN(obj.value))
			{
			alert(obj.nametag + NaNError)
			SetFocus(obj)
			return false
			}
		}
	}
return true
}


function ValidateDocument()
{
var z=0;
for(z=0;z<document.all.length;z++)
	{
	var obj = document.all(z)	
		
	if(obj.validate != null && obj.validate == "1")
		{
						
		if(CheckAlpha(obj) == false)
			return false
		if(CheckAlphaNumeric(obj) == false)
			return false
		if(CheckNumeric(obj) == false)
			return false
		if(CheckNumericdec(obj) == false)
			return false
						
		}
	}
	
	return CheckColors()
}

function CheckColors()
{
	
	
//must declare the following line globally with the correct path before calling this function
//ColorValidatePath = "../../../Controls/ValidateColors.asp"
var z=0, bolFound = true
var ColorDoc = new ActiveXObject("Msxml2.DOMDocument")
ColorDoc.loadXML("<Pantones></Pantones>")
for(z=0;z<document.all.length;z++)
	{
	var obj = document.all(z)
	if(obj.validate != null && obj.validate == "1" && obj.datatype == "color")
		{
			if(obj.allownull == "0")		//Proceed if null is not allowed
				{
				if(obj.value == "")			//If value is null then prompt and exit with error
					{
					alert(obj.nametag + NullError)
					SetFocus(obj)
					return false
					}
				}
			if(obj.value != "")				//Only proceed if there is a value
				{
				var ColorNode = ColorDoc.createElement("PantoneNo")	
				ColorNode.text = obj.value
				ColorNode.setAttribute("Index", z)
				ColorDoc.documentElement.appendChild(ColorNode)			
				}
		}
	}
	
if(ColorDoc.documentElement.childNodes.length > 0)
	{
	var objHTTP = new ActiveXObject("MSXML2.XMLHTTP"), objXML = new ActiveXObject("MSXML2.DOMDocument")
	objHTTP.open("GET", ColorValidatePath + "?strColors=" + ColorDoc.xml, false)
	objHTTP.send()
	if(objHTTP.responseText != "")
		{
		objXML.loadXML(objHTTP.responseText)
		var InvalidColors = ""
		var nodeInvalid = objXML.documentElement.selectNodes("PantoneNo[@Valid='0']")
		if(nodeInvalid.length > 0)
			{
			for(z=0;z<nodeInvalid.length;z++)
				InvalidColors += "Color: " + nodeInvalid.item(z).text + "\r\n"
			if(nodeInvalid.length == 1)
				InvalidColors += "is invalid"
			else if(nodeInvalid.length > 1)
				InvalidColors += "are invalid"
			alert(InvalidColors)
			SetFocus(document.all(parseInt(nodeInvalid(0).getAttribute("Index"))))
			return false
			}
		return true
		}
	}
return true
}