///////////////////////////////////////////////////////////
// Turboshop 4 Validate functions
// Digivate limited
// Pete Darling 25/05/2002 - updated 21/10/2004 pAd
// File Version: $Revision: 1.4 $
//
// ** Please do not modify! **
//////////////////////////////////////////////////////////

// **** onKeyPress Validate functions for use with onKeyPress
//
// WARNING :
//         Must always be used with KeyPress event - not onKeyDown or onKeyUp,
//         if you have to use it with onKeyUp and Down then you have to call both and it donb't work under NS.

/**
*   Allow Numbers only
*   update to allow for number pad 05/12/2007
*/
function onKeyNumOnly(e)
{
   var allowKeys = '1234567890';
   var key = 0;

   if (e.which) key = e.which;
   else key = e.keyCode;

	//allow tab and edit control keys
	if ((key == 9) || (key == 8) || (key == 46) || (key == 37) || (key == 39) ||
	(key>95 && key<106 && allowKeys.indexOf(String.fromCharCode(key))==-1)) return true;
	//allows chars in allowKeys string
   else if (allowKeys.indexOf(String.fromCharCode(key))>-1) return true;
   else return false;
}

/**
*   Allow CardNumbers only
*/
function onKeyCardNumOnly(e)
{
   var allowKeys = '1234567890 ';
   var key = 0;

   if (e.which) key = e.which;
   else key = e.keyCode;

	//allow tab and edit control keys
	if ((key == 9) || (key == 8) || (key == 46) || (key == 37) || (key == 39)) return true;
	//allows chars in allowKeys string
   else if (allowKeys.indexOf(String.fromCharCode(key))>-1) return true;
   else return false;
}

/**
*   Allow CardNumbers only
*/
function onKeyAlphaNumOnly(e)
{
   var allowKeys = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';
   var key = 0;

   if (e.which) key = e.which;
   else key = e.keyCode;

	//allow tab and edit control keys
	if ((key == 9) || (key == 8) || (key == 46) || (key == 37) || (key == 39)) return true;
	//allows chars in allowKeys string
   else if (allowKeys.indexOf(String.fromCharCode(key))>-1) return true;
   else return false;
}


/**
*   Allow Numbers only
*/
function onKeyNumOnlyMin(obj, min)
{
 	var num = parseInt(obj.value);

	if (num=='NaN')
	{
		num = 1;
		obj.value = num;
	}

	if (num<min)
	{
		num = min;
		obj.value = num;

	}

	return true;
}

/**
*   Allow Numbers only
*/
function onBlurNumOnlyMin(obj, min)
{
 	var num = parseInt(obj.value);

	if (num=='NaN' || obj.value=='')
	{
		num = 1;
		obj.value = num;
	}

	if (num<min)
	{
		num = min;
		obj.value = num;

	}

	return true;
}


/**
*   only allows max len chars - used with textarea boxes
*/
function onKeyMaxLen(obj, maxLen)
{
   if (obj.value.length>maxLen)
   {
   	 obj.value = obj.value.substr(0, maxLen);
   	 return false;
   }
	else
	{
		return true;
	}
}



/**
*  Disallow dubquotes, backslashes and | These are all used as delimiters,
*  Should be used on all input form elements without a validate function
*/
function noDubQuotes(obj, e)
{
	var disallowKeys = '"|\\'; // "
   var key = 0;

  if (e.which) key = e.which;
  else key = e.keyCode;


	if ((key == 9) || (key == 8)) return true;
	//exclude chars listed in disallowKeys
   else if (disallowKeys.indexOf(String.fromCharCode(key))>-1) return false;
   return true;
}



/**
* Allow only chars that are valid for an email address
**/
function onKeyEmail(obj,e)
{
	var allowKeys = "1234567890.qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM�-=@_";
   var key = 0;

   if (e.which) key = e.which;
   else key = e.keyCode;

	//alert(key+'='+String.fromCharCode(key));

	if ((key == 9) || (key == 8)) return true;
	//include chars listed in allowKeys
   else if (allowKeys.indexOf(String.fromCharCode(key))>-1) return true;
   else return false;
}


function onKeyURL(obj, e)
{
   var allowKeys = "1234567890.qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM%[],=-_,&~#:/?&";
   var key = 0;

   if (e.which) key = e.which;
   else key = e.keyCode;

  	if ((key == 9) || (key == 8)) return true;
	//include chars listed in allowKeys
   else if (allowKeys.indexOf(String.fromCharCode(key))>-1) return true;
   else return false;
}



// TSparseInt() - does not return 0 for number like 09 or 07 etc

function TSparseInt(anum)
{
	var t1 = ''+anum;
	if (t1.substring(0, 1) == '0') return parseInt(t1.substring(1));
	else return parseInt(t1);
}

/**
*   This one will allow numbers only up to 999
*/
function onKeyTS4UnitNumOnly(e, value)
{
	if (value.length==4) return false;
	else
	{
		var allowKeys = '1234567890.';
	   var key = 0;

	   if (e.which) key = e.which;
   	else key = e.keyCode;


		if (key==46)
	   {
	   	if (value.indexOf('.')!=-1) return false;
		   else return true;
	   }
	   else if ((key == 9) || (key == 8) || (key == 46) || (key == 37) || (key == 39)) return true;
	   else if (allowKeys.indexOf(String.fromCharCode(key))>-1) return true;
	   else return false;
	}
}

/**
*   This one will allow numbers, brackets and plus symbol
*/
function onKeyPhoneNumOnly(e)
{
	var allowKeys = '1234567890()+- ';//chars only - for other keys use key code
   var key = 0;

   if (e.which) key = e.which;
  	else key = e.keyCode;

  	//allow edit control keys
	if ((key == 9) || (key == 8) || (key == 46) || (key == 37) || (key == 39)) return true;
   else if (allowKeys.indexOf(String.fromCharCode(key))>-1) return true;
   else return false;
}

// **** onKeyUp validators - disallow invalid chars

function onKeyIntValidate(d)
{
 /*var t = ''+d.value.charAt(d.value.length-1);
 if (!t.match(/^\d{1,}/))
 {
  d.value = d.value.substring(0, d.value.length-1);
 }*/

 if (!d.value.match(/^\d{1,}$/))
 {
		d.value = d.value.replace(/[^\d]/, "");
 }
}


function onKeyIntPosValidate(d)
{
 if (!d.value.match(/^\d{1,}$/))
 {
		d.value = d.value.replace(/[^\d]/, "");
 }

 if (parseInt(d.value) <= 0) d.value = '1';
}


function onKeyPriceValidate(d)
{
 var t = ''+d.value.charAt(d.value.length-1);
 if (!t.match(/^\d{1,}|\.{1}/))
 {
  d.value = d.value.substring(0, d.value.length-1);
 }


 if ((d.value.match(/\.\d{1,}/)) && (d.value.lastIndexOf('.') != d.value.indexOf('.')))
 {
  if (!d.value.match(/^\d{1,}\.\d{1,}$/)) d.value = d.value.substring(0, d.value.lastIndexOf('.'));
 }

 if (d.value.match(/^\d{1,}\.\d{1,}/))
 {
 	var a1 = d.value.length;
 	var b1 = d.value.indexOf('.')+3;
 	if (a1 > b1) d.value = d.value.substring(0, d.value.indexOf('.')+3);
 }

	if (d.value.match(/[^\d\.{1}]/))
	{
		d.value = d.value.replace(/[^\d\.{1}]/, "");
	}

	if (d.value.match(/[\.]{2,}/))
	{
		d.value = d.value.substring(0, d.value.indexOf('.'))+ d.value.substring(d.value.lastIndexOf('.'), d.value.length);
	}


}

function onKeyMysqlDateValidate(d)
{
 var t = ''+d.value.charAt(d.value.length-1);

 ////* std strip code -- see code below
 if (!t.match(/^\d{1,}|-{1}/))
 {
  d.value = d.value.substring(0, d.value.length-1);
 }

 /* auto - insert at char 5 and 8 letting the user only add numbers
 * some would say this may confuse the user and is bad interface design not sure myself so chould use org code above
 */
/* var l  = d.value.length;
 if ((l<5) || ((l>5)&&(l<8)) || (l>8))
 {
 	if (!t.match(/^\d{1,}/)) d.value = d.value.substring(0, d.value.length-1);

 	if ((d.value.length==4)|| (d.value.length==7)) d.value = d.value+'-';
 }*/


}

function onKeyNoLineReturn(obj)
{
	obj.value = obj.value.replace(/\r\n/, ' ');
}



// ****** field validators - return false if invalid

function MysqlDateValidate(d)
{

	if (!d.value.match(/[0-9]{4}\-[0-9]{2}\-[0-9]{2}/)) return false;


	var dateparts = d.value.split('-');

	for(var i=0; i<dateparts.length; i++)
	{
		if (!(TSparseInt(dateparts[i]) > 0)) return false;
	}

	return true;

}


function emailCheck(obj)
{
 var emailStr = obj.value;
 var emailPat = /^[\'_\.0-9A-Za-z-]+@([0-9A-Za-z-]+[0-9A-Za-z-\.]+)\.([A-Za-z]){2,4}$/;
 var email = emailStr.toLowerCase();
 var matchArray = email.match(emailPat);
 if (matchArray==null)
 {
      alert("Please enter a valid email address");
      return (false);
 }
 //obj.value = email;
 return (true);
}

function isAnEmail(obj)
{
 var emailStr = obj.value;
 var emailPat = /^[\'_\.0-9A-Za-z-]+@([0-9A-Za-z-]+[0-9A-Za-z-\.]+)\.([A-Za-z]){2,4}$/;
 var email = emailStr.toLowerCase();
 var matchArray = email.match(emailPat);
 if (matchArray==null)
 {
      return (false);
 }
 else return (true);
}

function onKeyPhoneValidate(d)//use onKeyPhoneNumOnly not this func
{
 	var valid = "0123456789"
	var ok = 1;
	var temp = '';
	var newstr = '';

	for (var i=0; i<d.value.length; i++)
	{
		if (i > 30) break;
		temp = ''+d.value.substring(i, i+1);
		if (valid.indexOf(temp)!=-1)
		{
			newstr += temp;
		}
	}

	d.value = newstr;
}

function onKeyMobileValidate(d)
{
 	var valid = "0123456789"
	var ok = 1;
	var temp = '';
	var newstr = '';

	for (var i=0; i<d.value.length; i++)
	{
		if (i > 16) break;
		temp = ''+d.value.substring(i, i+1);
		if ((i==0) && (temp!= '0')) continue;
		if ((i==1) && (temp!= '7')) continue;

		if (valid.indexOf(temp)!=-1)
		{
			newstr += temp;
		}
	}

	d.value = newstr;
}


function phoneValidate(field)
{
	var valid = " +()0123456789 -"
	var ok = 1;
	var temp = '';

	for (var i=0; i<field.length; i++)
	{
		temp = ''+field.substring(i, i+1);
		if (valid.indexOf(temp)==-1)
		{
			ok = 0;
			break;
		}
	}
	return ok;
}

function mobileValidate(field)
{
	var valid = "0123456789 "
	var ok = 1;
	var temp;

	if (field.substring(0, 2)!='07')  ok = 0;
	else
	{
		for (var i=0; i<field.length; i++)
		{
			temp = "" + field.substring(i, i+1);
			if (valid.indexOf(temp)==-1)
			{
				ok = 0;
				break;
			}
		}
	}
	return ok;
}


function inputBoxIsEmpty(obj)
{
	var ret = false;

	if (obj!=null && obj.disabled!=false)
	{
		if (obj.value.trim()=='')
		{
			ret = true;
		}
	}

	return ret;
}

function selectBoxNotSelected(obj)
{
	var ret = false;

	if (obj.disabled!=false)
	{
 		if (obj.selectedIndex < 0)
 		{
	  		ret = true;
 		}
 		else
 		{
 			if (obj.options[obj.selectedIndex].text == "" || obj.options[obj.selectedIndex].value == "-" || obj.options[obj.selectedIndex].value == "")
 			{
	  			ret = true;
 			}
 		}
 	}

 	return ret;
}

function radiosNotChecked(obj)
{
	var ret = false;

	if (obj.disabled!=false)
	{
		for(var i=0; i<obj.length; i++)
		{
			if (obj[i].checked==true)
			{
				ret = true;
				break;
			}
		}
	}

	return ret;

}


//check postcode format is valid - used on country (add7) fields ONLY - must be a dropdown
function postcodeCheck(obj){ 

	
//Do not test postcode if outside of UK	, UK needs to be first in the country list
/*if(obj2)
{
    if ( obj2.selectedIndex != null )
    {
	    if (obj2.selectedIndex != 1)
	    {
	    	return true ;
	    }
    }
}
else
{*/
	var tbName = 'ts_'+obj.id.substr(3, 4)+'add7';
	if (tsGetElementById(tbName)!=null)
	{
		if (tsGetElementById(tbName).selectedIndex != 1)
		{
			return true ;
		}
	}

		
		
//Change to uppercase	
 postcode = obj.value; 
 
 
 //check if field is empty
 if(postcode === "")
 {
      alert("Please enter your postcode");
      obj.focus();
      return false;  
 }
 
 size = postcode.length
 postcode = postcode.toUpperCase(); 
 





 //Strip leading spaces
 while (postcode.slice(0,1) == " ") 
  {postcode = postcode.substr(1,size-1);size = postcode.length
  }

 //Strip trailing spaces
 while(postcode.slice(size-1,size)== " ") 
  {
  	postcode = postcode.substr(0,size-1);size = postcode.length
  }
 
 //write back to form field
 obj.value = postcode; 
 
 //Code length rule
 if (size < 6 || size > 8){ 
  alert(postcode + " is not a valid postcode - wrong length");
  obj.focus();
  return false;
  }
 
 //leftmost character must be alpha character rule
 if (!(isNaN(postcode.charAt(0)))){ 
   alert(postcode + " is not a valid postcode - cannot start with a number");
   obj.focus();
   return false;
  }
 
 //first character of inward code must be numeric rule
 if (isNaN(postcode.charAt(size-3))){ 
   alert(postcode + " is not a valid postcode - alpha character in wrong position");
   obj.focus();
   return false;
  }

  //second character of inward code must be alpha rule
 if (!(isNaN(postcode.charAt(size-2)))){ 
   alert(postcode + " is not a valid postcode - number in wrong position");
   obj.focus();
   return false;
  }

 //third character of inward code must be alpha rule
 if (!(isNaN(postcode.charAt(size-1)))){ 
   alert(postcode + " is not a valid postcode - number in wrong position");
   obj.focus();
   return false;
  }

 //space in position length-3 rule
 if (!(postcode.charAt(size-4) == " ")){
   alert(postcode + " is not a valid postcode - no space or space in wrong position");
   obj.focus();
   return false;
   }
   
 count1 = postcode.indexOf(" ");count2 = postcode.lastIndexOf(" ");
 
 //only one space rule
 if (count1 != count2){
   alert(postcode + " is not a valid postcode - only one space allowed");
   obj.focus();
   return false;
  }

return true;

}


function phoneValidation(obj)
{
    phoneNo = obj.value; 

    if( phoneNo.length < 10 )
    {
        alert("Please enter a valid telephone number");
        obj.focus();
        return false;
    }
    return true;
}
