/*
File: formfunctions.js
Author: Nathan Davies
Created: 16/05/2007	 
Ammened: ND 17/05/2007 Added populateExtra()
         ND 18/05/2007 Added hideOrShowInput()
*/	

/*------------------------------------------------------------
	GENERIC AJAX FUNCTIONS
------------------------------------------------------------*/
function writeResponse() 
{ 	   
	if (goXMLHTTP.readyState==4 || goXMLHTTP.readyState=="complete")
	{ 
		if (goXMLHTTP.status == 200)
		{
			document.getElementById(gcItemID).innerHTML=goXMLHTTP.responseText ;  
			// set glIsValid based on the return from dataCheck.php
			glIsValid = goXMLHTTP.responseText.search(/warninglabel/i)>0? false:true ; 
		}
	} 
} 

/*------------------------------------------------------------*/

function GetXmlHttpObject()
{ 
	if (window.XMLHttpRequest)
	{
		goXMLHTTP=new XMLHttpRequest() ;
	}
	else if (window.ActiveXObject)
	{
		goXMLHTTP=new ActiveXObject("Microsoft.XMLHTTP") ;
	}	 
	if (goXMLHTTP==null)
	{
		alert ("Browser does not support HTTP Request")	;
	 	return	;
	}
} 
																		 
function validateItem(pcID,pcItem,pcDisplay,pnType,plPopulateExtra,pnNumberOfItems)	
{	
	var lcRegExp, llIsValid	;
	llIsValid = false ;
	llIsSecurity = false ;
	switch (pnType)
	{
		case 1:
			pcItem = pcItem.toUpperCase(); 	
			lcRegExp = '^[A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA$' ;
			break;
		case 2:
			lcRegExp = '^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$' ;
			break;
		case 3:
			lcRegExp = '^[0-9]{6,11}$' ;
			break;
		case 6: // security text
			lcRegExp = '^[A-Za-z0-9 ]{6,10}$' ;	
			llIsSecurity = true ;
			break ;	 
	}
	if (pnType == 1||pnType == 2||pnType==3||pnType==6)
	{
		if (pcItem.match(lcRegExp))
		{	
			llIsValid = true ;
		}
		else
		{			  
			llIsValid = false ;
		}
	}	
	else
	{				 
		if (pcItem == null||pcItem == ""||pcItem.length <= 2)
		{
			llIsValid = false ;
		}
		else
		{
			llIsValid = true ;
		}	 
	}	
	
	if(llIsSecurity)
	{ 
		if(llIsValid) // test against $_SESSION
		{
			GetXmlHttpObject() ; // sets the global variable goXMLHTTP
			gcItemID = pcID ;
			gcUrl = "checkSecurity.php?tocheck=" + pcItem ;
				goXMLHTTP.onreadystatechange = function() 
				{ 
					// use of ghost function enables the system to execute each item in the correct order
					writeResponse();
					if (gnValidationCount >= 1)
					{
						gnValidationCount = gnValidationCount - 1 ;
					}	 
				};
			goXMLHTTP.open("GET",gcUrl,true) ;
			goXMLHTTP.send(null) ;
		}
		else
		{
			document.getElementById(pcID).innerHTML = "<p class='warning'>"+pcDisplay+"</p>" ;
			if (gnValidationCount <= pnNumberOfItems)
			{
				gnValidationCount = gnValidationCount + 1 ;
			}
		}
	}
	else // standard validation
	{		
		if (llIsValid)								
		{				
			document.getElementById(pcID).innerHTML = pcDisplay ;
			if (gnValidationCount >= 1)
			{
				gnValidationCount = gnValidationCount - 1 ;
			}
		}
		else
		{	
			document.getElementById(pcID).innerHTML = "<p class='warning'>"+pcDisplay+"</p>" ;
			if (gnValidationCount <= pnNumberOfItems)
			{
				gnValidationCount = gnValidationCount + 1 ;
			}
		}
	}	 
	if (gnValidationCount == 0)	
	{
		hideOrShowInput("register",false);	
	}
	else
	{
		hideOrShowInput("register",true);	
	}	
	if (plPopulateExtra)
	{
		 populateExtra(lcSource, lcDestination, true, true)	;
	}
}			

function populateExtra(pcSourceFields, pcDestinationFields, pcPopulate, pcCallOnChange)
{								  
var laSource      = pcSourceFields.split(',');
var laDestination = pcDestinationFields.split(',') ;
var lcSourceId, lcDestinationId	   ;
// this is a global variable to control form usability
glCopyDetails = pcPopulate ;
	
	if (laSource.length == laDestination.length)
	{ 
		var lnArrayCntr   = 0;	
		var lnTargetCount = laSource.length - 1 ;
		for (lnArrayCntr  = 0;lnArrayCntr<=lnTargetCount;lnArrayCntr++) 
		{	 
			if(pcPopulate)
			{
				document.getElementById(laDestination[lnArrayCntr]).value = document.getElementById(laSource[lnArrayCntr]).value ;
				document.getElementById(laDestination[lnArrayCntr]).disabled = true ;
			}
			else
			{																													
				document.getElementById(laDestination[lnArrayCntr]).disabled = false ;
			}																  
			if (pcCallOnChange && typeof(document.getElementById(laDestination[lnArrayCntr]).onchange == "function"))
			{											  
				document.getElementById(laDestination[lnArrayCntr]).onchange();
			}
		}
	}
}  

function hideOrShowInput(pcID, plDisabled)
{	
	document.getElementById(pcID).disabled = plDisabled ;
}	  

function setQuantity(pcID, pnOperation, pnMin, pnMax)
{
var lnCurrentQuantity = document.getElementById(pcID).value	; 							  
var lnNewQuantity     = lnCurrentQuantity ; 
	switch(pnOperation)
	{				   
		case 1: // increase quantity by 1 
		lnNewQuantity++ ;
		if(lnNewQuantity > pnMax)
		{								
			lnNewQuantity = lnCurrentQuantity ;
		}
		break;
		case 2:// decrease quantity by 1
		lnNewQuantity--	;
		if(lnNewQuantity < pnMin)
		{
			lnNewQuantity = lnCurrentQuantity ;
		}
		break;
		default:// ensuring a typed quantity remains within the parameters defined
		if (lnCurrentQuantity > pnMax || lnCurrentQuantity < pnMin)
		{									 
			alert("Please set the quantity between " + pnMin + " and " + pnMax + '\n' + "The quantity will now be set to 1") ;
			lnNewQuantity = 1 ;
		}
		break;
	}
document.getElementById(pcID).value = lnNewQuantity ;
}