//Writen by S Boylen - Copyright Re-Scan Ltd 2008 
//Open a new window at the anchor passes in the params
function opennewWindow(pURL,pAnchor){
	newWindow = window.open(encodeURI(pURL + '#' + pAnchor),'newWin','toolbar=no,location=no,scrollbars=yes,width=400,height=450')
}


//Open a new window and gets it ready for the print job
function openPrintWindow(){
	newWindow = window.open('print_window.html','newWin','toolbar=no,location=no,scrollbars=yes,width=600,height=450')
}
//Same as above but the php page
function openPrintWindow2(aParam){
	newWindow = window.open('print_window2.php?' + encodeURI(aParam),'newWin','toolbar=no,location=no,scrollbars=yes,width=600,height=450')
}
//Same as above but more flexable
function openPrintArea(aParam){

	newWindow = window.open('print_area.php?' + encodeURI(aParam),'newWin','toolbar=no,location=no,scrollbars=yes,width=600,height=450')
}

//the mouse over action for the nav tabs
function tabMouseOver(pTabNo){
	try
	{
	    var currentClass = document.getElementById('tab' + pTabNo).className;
	    var classOver    = ' navTabOver';
		document.getElementById('tab' + pTabNo).className		=   currentClass + classOver;
	}
   catch(e){}
}

//the mouse out action for the nav tabs
function tabMouseOut(pTabNo){
	try
	{
	    var currentClass = document.getElementById('tab' + pTabNo).className;
		var classArray   = currentClass.split(' ');
		var classOver    = 'navTabOver';
		currentClass='';
		//build class list
		for (var i=0; i<=classArray.length-1; i++)
		{
		    if ((classArray[i] != classOver) && (classArray[i] != '')) 
			{
		        currentClass += classArray[i] + ' ';
			}
		 }
		    
		document.getElementById('tab' + pTabNo).className		=   currentClass;
	}
   catch(e){}	
}

//Checks if the input is a int number
function isNumeric(aItem){
	if ((isNaN(aItem.value)) || (aItem.value<0) || (aItem.value>9999) || (aItem.value.length==0) )
	{
		alert('The quantity must be a valid positive number.\n');
		aItem.value = '0';
		return false;
	}
	else
	{
	   // check that its a whole number	
	   if (aItem.value.indexOf('.') != -1)
	   {
		   alert('The quantity must be a whole number.\n');
		   try
		   {
		      aItem.value = parseInt(aItem.value);
	       }catch (e){
		       aItem.value = 0;
	       }
		   return false;   
	   }else{	
	       return true;
       }
    }
}

//Checks that the passwords match and are ok length
function passwordCheck(passwordOld,passwordNew,passwordRetype){
	try
	{
		var error = '';
		
		if (passwordOld.value.length == 0)
		{
			error = 'Please enter your current password into the space provided.\n';
		}
		if (passwordNew.value.length == 0)
		{
			error = 'Please enter your new password into the space provided.\n';
		}
		if (passwordRetype.value.length == 0)
		{
			error = 'Please re-type your new password into the space provided.\n';
		}
		if (passwordRetype.value != passwordNew.value)
		{
			error = 'Your new password does NOT match the re-typed one.\n';
		}
		if (passwordOld.value == passwordNew.value)
		{
			error = 'The new password can NOT be the same as the old one.\n';
		}
		if (passwordNew.value.length <= 3)
		{
			error = 'New password must be longer the 4 characters.\n';
		}
	
		if (error.length != 0)
		{
			alert(error);
		    return false;	
		}else{
		    return true;	
		}
	}
	catch(e)
	{
		return true;
	}
}

function confirmComplete(aQtn){
	return confirm(aQtn);
}

//check register info
function registerCheck(){
	try
	{
		var error = '';

		if (document.rm_register.custcode.value.length == 0)
		{
			error = 'Please enter a your company name.\n';
		}
		if (document.rm_register.acontact.value.length == 0)
		{
			error = 'Please enter a contact name.\n';
		}
		if (document.rm_register.tel1.value.length == 0)
		{
			error = 'Please enter a contact telephone number.\n';
		}
		if (document.rm_register.postcode.value.length == 0)
		{
			error = 'Please enter your postcode.\n';
		}
		if (document.rm_register.town.value.length == 0)
		{
			error = 'Please enter your town.\n';
		}
		if (document.rm_register.add1.value.length == 0)
		{
			error = 'Please enter complete address line 1.\n';
		}
	
		if (error.length != 0)
		{
			alert(error);
		    return false;	
		}else{
		    return true;	
		}
	}
	catch(e){ return true;	}
}

//Adds aNumber to aQtyFild (if aNumber is neg the it is subtracted)
function qtyUpdate(aQtyFild,aNumber){
	if (isNumeric(aQtyFild))
	{
		var l_qty = parseInt(aQtyFild.value) + aNumber;
		if (l_qty >= 0)
		{
			aQtyFild.value = l_qty;
		}else{
		    aQtyFild.value = '0';	
		}
		
		aQtyFild.focus();
	}
}


// Checks there is info in the ref
function checkRef(aField){
	if (aField.value.length <= 4)
	{
		alert('The reference code should be 4 or more characters in length');
		return false;
	}else{ 
		return true; 
	}
}

//Sets the items per page cookie, to show the users requested ipp
function itemsPerPage(ipp){
	if ((isNaN(ipp)) || (ipp>200) || (ipp<5))
	{
	    ipp = 50;	
	}
	
	    var date = new Date();
		date.setTime(date.getTime()+(365*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
        document.cookie = 'cIpp='+ipp+expires+"; path=/";
        alert('Default items per page changed to ' + ipp + '.\nYou must refresh your browser for this to take effect');
}


//swap the img src with the one passed in the params
function imgSwap(imgName,imgID){
   //Get the Img Element
    if(document.getElementById) {
	    var divObj = document.getElementById(imgID);
    } else if (document.all){
	    var divObj = document.all[imgID];
    }
    if (divObj != null){
	    currentImg = divObj.src;
	    i = currentImg.lastIndexOf('/'); 
	    if (i != -1)
	    {
		    currentImg = currentImg.substring(0,i);
	        divObj.src = currentImg + '/' + imgName;  
        }   
    }
}