function getMessageString()
{
	return "Sentimos no poder atender su peticion. Por favor facilite los datos adjuntos abajo.\n";
}

function MM_validateForm() 
{ var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;j=0;
	//	/^([-a-zA-Z0-9._]+@[-a-zA-Z0-9.]+(\.[-a-zA-Z0-9]+)+)$/;
	var regEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;var regBlank = /[^\s]/;var regAlphaNum = /^([a-zA-Z0-9-_#@!^]+)$/;
	var regDate = /^([0-9_]+-[0-9][0-9]+-[0-9][0-9]+)$/;
	var regSpace = /^([a-zA-Z0-9_#@]+)$/;
	var regNumeric = /^([0-9]+)$/; 
	for (i=1; i<(args.length-2); i+=3) 
	{	
		mesg=args[i+1];test=args[i+2]; val=document.forms[""+args[0]].elements[""+args[i]];
	    if (val) 
		{	nm=mesg; noVal = val; val = val.value;
			if(regBlank.test(val)){
				if(test.indexOf('isEqual')!=-1)
				{
					result = trim(val);
				if(result.length==0){
				errors += '- '+nm+' is required.\n'; 
				}else{
					equal_obj_val = test.substring(8,test.indexOf(":"));
					mesg_string =test.substring((test.indexOf(":")+1));
					if(val != document.forms[""+args[0]].elements[""+equal_obj_val].value)
					{
					errors+='- '+nm+' must be same as '+mesg_string+'.\n';
					}

				}
				}
				else if(test.indexOf('isAlphaNum')!=-1)
				{
				result = trim(val);
				if(result.length==0){
				errors += '- '+nm+' requerido.\n'; 
				}else{
					if(!regAlphaNum.test(val))
					{
						errors+='- '+nm+': Solo esta permitido alpha numérico y los caracteres " _ - ".\n';
					}
				}
				}
				else if (test.indexOf('isDate')!=-1) 
				{ 
					p=val.indexOf('-');
					if (p != 4 )
					{
						errors+='- '+nm+' must contain Valid Date YYYY-MM-DD.\n';
					}
					else if(!regDate.test(val))
					{
						errors+='- '+nm+' must contain Valid Date YYYY-MM-DD.\n';
					}
			     }
				 
				 else if(test.indexOf('isSpace')!=-1)
				{
					result = trim(val);
					
					if(result.length==0)
					{
						errors += '- '+nm+' requerido.\n'; 
					}
					else
					{
						if(!regSpace.test(val))
						{
							errors += '- '+nm+' inválido.\n';
						}
					}
				}
				 else if(test.indexOf('isNum')!=-1)
				{
					result = trim(val);
					
					if(result.length==0)
					{
						errors += '- '+nm+' requerido.\n'; 
					}
					else
					{
						if(!regNumeric.test(val))
						{
							errors += '- '+nm+' debe incluir un numero!\n';
						}
					}
				} 
				else if (test.indexOf('isEmail')!=-1) 
				{ 
					p=val.indexOf('@');
					s=val.indexOf('.');
			        if (p<1 || p==(val.length-1))
					{
						errors+='- '+nm+' debe incluir una dirección de correo electrónico.\n';
					}
					else if(!regEmail.test(val))
					{
						errors+='- '+nm+' la dirección de correo electrónico debe ser válida.\n';
					}
			     }
				else if (test.indexOf('isUrl')!=-1) 
				{ 
					p=val.indexOf('http://');
					s=val.indexOf('.');
			        if (p<0 || p==(val.length-1))
					{
						errors+='- '+nm+' must be valid URL e.g. http://www.abc.com\n';
					}
					else if(s<p || s==(val.length-1))
					{
						errors+='- '+nm+' must be valid URL e.g. http://www.abc.com\n';
					}
			     }
				 else if(test.indexOf('isCheckbox')!=-1)//Check is check box is not checked generate error
				{	
					var valueCheckbox = noVal.checked;
					if(!valueCheckbox)
					{
						errors+='- '+' Aceptar términos y condiciones.\n';
					}

				}
				 else if (test.indexOf('isChar')!=-1) 
				 { 
					var first_char;
					first_char= val.charAt(0);
					if(first_char==0||first_char==1||first_char==2||first_char==3||first_char==4||first_char==5||first_char==6||first_char==7||first_char==8||first_char==9){
				 errors+='- '+nm+' must starts with  a char.\n';
	}
			     }
	   			 else if (test!='R') 
				 {
				 result = trim(val);
					if(result.length==0){
					errors += '- '+nm+' is required.\n'; 
					}
				    if (isNaN(val)) errors+='- '+nm+' debe incluir un numero.\n';
					if (test.indexOf('inRange') != -1) 
					{ num = parseFloat(val);
						p=test.indexOf(':');
						min=test.substring(8,p); 
						max=test.substring(p+1);
						if (num<min || max<num) 
						errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
					} 
					if (val.indexOf('-') != -1) 
					{ 
						errors+='- '+nm+' must contain a number without dashes sign.\n';
					} 
					if (val.indexOf('+') != -1) 
					{ 
						errors+='- '+nm+' must contain a number without plus sign.\n';
					}
				}else if (test.charAt(0)=='R')
				{
				result = trim(val);
				if(result.length==0){
				errors += '- '+nm+' requerido.\n'; 
				}
				} 
			}
			else if (test.charAt(0) == 'R'){
				errors += '- '+nm+' requerido.\n'; 
			}
		}
		if(errors !="")
		{	if(j<=0)
			{
				focusitem = document.forms[""+args[0]].elements[""+args[i]];
				j++;
			}	
		}
	} 
//return errors;
  if (errors)
  {
  	var msg = getMessageString();
	alert(msg+'\n'+errors);
	focusitem.focus();
	return false;
   }
   else
	return true;
//  document.MM_returnValue = (errors == '');
}


function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function




//CODE FOR RECURRENCE STUFF
function validateFormRest(formname)
{
	if(MM_validateForm(formname,'restaurantName','Restaurant name','R','restaurantDeliveryPolicy','Restaurant delivery policy','R','restaurantTimeToDeliver_hour','Restaurant hours','R','restaurantTimeToDeliver_minute','Restaurant minutes','R','restaurantTimeToPrepare_hour','Restaurant hours','R','restaurantTimeToPrepare_minute','Restaurant minutes','R','restaurantStartTime_hour','Restaurant hours','R','restaurantStartTime_minute','Restaurant minutes','R','restaurantCloseTime_hour','Restaurant hours','R','restaurantCloseTime_minute','Restaurant minutes','R','restaurantIsDelivery','Restaurant status','R','areaId','Restaurant area name','R','restaurantMinimumDeliveryAmount','Restaurant delivery amount','RisNaN','restaurantPhone','Restaurant phone','isNaN','restaurantMobile','Restaurant mobile','isNaN'))
	{
		return true;
	} 
	else 
	{
		return false;
	} 
}

function validateFormCust(formname)
{
	if(MM_validateForm(formname,'customerTitle','Customer title ','R','customerFName','Customer first name','R','customerLName','Customer last name','R','customerPassword','Customer password','R','customerEmail','Customer email','RisEmail','customerIsActive','Customer status','R','customerPhone','Customer phone','isNaN'))
	{
		return true;
	} 
	else 
	{
		return false;
	} 
}


function validateFormCat(formname)
{
	if(MM_validateForm(formname,'categoryName','Category name','R'))
	{
		return true;
	} 

	else 
	{
		return false;
	} 
}


function validateFormMenu(formname)
{
	if(MM_validateForm(formname,'menuItemName','Menu item name','R','mItemImage','Menu item image','R'))
	{
		return true;
	} 
	else 
	{
		return false;
	} 
}



function validateFormCuisine(formname)

{
	if(MM_validateForm(formname,'cuisineInfoName','Cuisine name','R'))

	{

		return true;

	} 

	else 

	{

		return false;

	

	} 



}



function validateFormRate(formname)

{
	if(MM_validateForm(formname,'price','Price','RisNaN','metricSize','Metric size','R','metricName','Metric name','R'))

	{

		return true;

	} 

	else 

	{

		return false;

	

	} 



}



function chkLogin(formname)

{

	

	if(MM_validateForm(formname,'customerEmail','Member id','RisEmail','customerPassword','Password','R'))

	{

		return true;

	} 

	else 

	{

		return false;

	

	} 



}



function chkForgotFrm(formname)

{

	

	if(MM_validateForm(formname,'customerEmail','Member id','RisEmail'))

	{

		return true;

	} 

	else 

	{

		return false;

	

	} 



}

function chkForgotFrm(formname)

{

	

	if(MM_validateForm(formname,'customerEmail','Member id','RisEmail'))

	{

		return true;

	} 

	else 

	{

		return false;

	

	} 



}


/******************************************
Function Name : validChangePass
Return Type : boolean
Date Created : 27th July 2007
Date Last Modified : 27th July 2007
Author : Rupesh parmar
Last Modified By :
Comments : Function will return the true or error message after validating checkboxes
User Instruction : validChangePass(btnType)
******************************************/
function validChangePass(formname)
{ 
	if(validateForm(formname,'frmAdminOldPassword', 'Current password', 'R'))
	{	
		var flag=confirm('Are you sure you want to change password?')
		if(flag)
		return true;
		else
		return false;
	} 
	else 
	{
		return false;
	} 
}


/***********************************************front end*****************************************************/
//used in login-frm.php 
function validateLoginFrm(formname)
{ 
	if(MM_validateForm(formname,'frmUserName', 'User Name(Email)', 'RisEmail' , 'frmUserPassword', 'Password', 'R' ))
	{	
		return true;
	} 
	else 
	{
		return false;
	} 
}

//used in forgot_password.php
function validateForgotPassFrm(formname)
{
	
	if(MM_validateForm(formname,'frmUserName', 'User Name(Email)', 'RisEmail' ))
	{	
		return true;
	} 
	else 
	{
		return false;
	} 		
}

//Used in reg.php 'frmUserPassword', 'Password','RisEqualfrmCongUserPassword:Confirm Password'
function validateRegFrm(formname)
{
	if(MM_validateForm(formname,'frmUserFirstName', 'Nombre', 'R' , 'frmUserLastName', 'Apellido', 'R' , 'frmUserEmail', 'Nombre de usuario (correo electrónico)', 'RisEmail' ,'frmUserPassword', 'Contraseña', 'RisEqualfrmUserConfPassword:Confirm Password' , 'frmUserConfPassword', 'Confirme Contraseña', 'RisSpace' , 'frmUserIsAgreeTerm', 'terms', 'RisCheckbox' ))
	{	
	    return true;
	} 
	else 
	{
		return false;
	} 		
}


/******************************************
Function Name : validateUserDetail(formnane)
Return Type : boolean
Date Created : 11th March 2008
Date Last Modified : 11th March 2008
Author : Sumit Agrawal
Last Modified By :Sumit Agrawal
Comments : Function will return the true or error message after validating User Details Account
User Instruction :  validateUserDetail(formnane)
******************************************/
function  validateUserDetail(formnane)
{
	//alert(formnane); return false;
	if(MM_validateForm(formnane,'frmUserDetailStreetAddress','Street Address','R','frmUserDetailCity','City','R','frmUserDetailState','State','R','frmUserDetailZip','Zip','RisNum','frmUserDetailCountry','Country','R','frmshipUserDetailStreetAddress','Shipping Street Address','R','frmshipUserDetailCity','Shipping City','R','frmshipUserDetailState','Shipping State','R','frmshipUserDetailZip','Shipping Zip','RisNum','frmshipUserDetailCountry','Shipping Country','R'))
	{
		return true;
	}
	else
	{
		return false;
	}
}


/*****************************
Function name :  validUserChangePass
Return Type : boolean
Date Created : 11th March 2008
Date Last Modified : 11th March 2008
Author : Sumit Agrawal
Last Modified By :Sumit Agrawal
Comments : This is used to validate user password and confirm passwords.
User instruction :  validUserChangePass(formname)
************************************/
function validUserChangePass(formname)
{
	
	if(MM_validateForm(formname,'frmUserOldPassword', 'Contraseña Corriente', 'RisSpace', 'frmUserNewPassword', 'Nueva Contraseña','RisSpace','frmUserConfirmPassword', 'Confirme Contraseña', 'RisEqualfrmUserNewPassword:New Password'))
	{			
		
			return true;
				
	} 
	else 
	{
		return false;
	} 
}


//used in contact_us.php 
function validateContactFrm(formname)
{ 
    if(MM_validateForm(formname,'frmUserFirstName', 'Nombre', 'R' , 'frmUserLastName', 'Last Name', 'R','frmUserEmail','Email' ,'RisEmail'))
    {    
        return true;
    } 
    else 
    {
        return false;
    } 
}

/********************************* check credit card details **********************************/


var d = new Date();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();

function testCreditCard()
 {
			  if (checkCreditCard (document.getElementById('frmUserCardNumber').value,document.getElementById('frmUserCardType').value)) {
				//alert ("Credit card has a valid format");
				return true;
			  } 
			  else {
				//alert (ccErrors[ccErrorNo]);
				return false;
			  }
}
function chkCardDetails()
{
	var errorString = '';
	var boolfocus = false;
	var elementID ;
	var regNumeric = /^([0-9]+)$/; 
	var msg = getMessageString();
	  
	var checkCard=testCreditCard();
	if(!(checkCard))
	{ 
		errorString = ccErrors[ccErrorNo]+'\n';
		boolfocus = true; 
		elementID = 'frmUserCardType';
		//document.getElementById('frmUserCardType').focus();
		//return false;
	
	}

   
	if(document.frmUserCard.frmCardExpiryYear.value==0)
	{
		
		errorString += '- Please select card expiry year!\n';
		 if(!boolfocus )
		  {
			  boolfocus = true; 
			  elementID = 'frmCardExpiryYear';	 
		  }
		
		/*alert( "Please select card expiry year!");
		document.frmUserCard.frmCardExpiryYear.focus();
		return false;*/
	} 
	if(document.frmUserCard.frmCardExpiryYear.value<curr_year)
	{
		
		errorString += '- Expiry year can not be before current year!\n';	 
		  if(!boolfocus )
		  {
			  boolfocus = true; 
			  elementID = 'frmCardExpiryYear';	 
		  }
		/*alert( "Expiry year can not be before current year!");
		document.frmUserCard.frmCardExpiryYear.focus();
		return false;*/
	}

	
	if(document.frmUserCard.frmCardExpiryMonth.value=="")
	{
		
		errorString += '- Please select card expiry month!\n';	 
		  if(!boolfocus )
		  {
			  boolfocus = true; 
			  elementID = 'frmCardExpiryMonth';	 
		  }
		
		/*alert( "Please select card expiry month!");
		document.frmUserCard.frmCardExpiryMonth.focus();
		return false;*/
	}
	else if(document.frmUserCard.frmCardExpiryYear.value==curr_year)
	{
	 	if(document.frmUserCard.frmCardExpiryMonth.value<=curr_month)
		{
				errorString += '- Expiry month can not be before current month!\n';	 
			  if(!boolfocus )
			  {
				  boolfocus = true; 
				  elementID = 'frmCardExpiryMonth';	 
			  }
/*				alert( "Expiry month can not be before current month!");
				document.frmUserCard.frmCardExpiryMonth.focus();
				return false;
*/		}
	}
	
	if(document.frmUserCard.frmUserCardName.value==0)
	{
		 errorString += '- Please enter card owner name!\n';	 
		  if(!boolfocus )
		  {
			  boolfocus = true; 
			  elementID = 'frmUserCardName';	 
		  }
		/*
		alert("Please enter card owner name!");
		document.frmUserCard.frmUserCardName.focus();
		return false;*/
	}
	
	if(document.frmUserCard.frmUserCardSecCode.value==0)
	{
		
		errorString += '- Please enter card security code!\n';	 
		  if(!boolfocus )
		  {
			  boolfocus = true; 
			  elementID = 'frmUserCardSecCode';	 
		  }
		/*alert("Please enter card security code!");
		document.frmUserCard.frmUserCardSecCode.focus();
		return false;*/
	}
	else if(!regNumeric.test(document.frmUserCard.frmUserCardSecCode.value))
	{
		
		errorString += '- Please enter numeric card security code!\n';	 
		  if(!boolfocus )
		  {
			  boolfocus = true; 
			  elementID = 'frmUserCardSecCode';	 
		  }
		/*alert("Please enter numeric card security code!");
		document.frmUserCard.frmUserCardSecCode.focus();
		return false;*/
	}
	else if(document.frmUserCard.frmUserCardSecCode.value.length>4 || document.frmUserCard.frmUserCardSecCode.value.length<3)
	{
		
		 errorString += '- Please enter 3-4 digit card security code!\n';	 
		  if(!boolfocus )
		  {
			  boolfocus = true; 
			  elementID = 'frmUserCardSecCode';	 
		  }
		/*
		alert("Please enter 3-4 digit card security code!");
		document.frmUserCard.frmUserCardSecCode.focus();
		return false;*/
	}
   if(errorString == '')
   {
		return true;  
   }
   else
   {	
   		alert(msg+'\n'+errorString);
		
		if(boolfocus)
		{
		 document.getElementById(elementID).focus();
		}
		return false;   
   }
	return true;
	
}





/****************************************end credit card check**************************************/

/*****************************
Function name :  confirmDelete
Return Type : none
Date Created : 12th March 2008
Date Last Modified : 12th March 2008
Author : Sumit Agrawal
Last Modified By :Sumit Agrawal
Comments : This is used to delete card information of the user after confirmmation.
User instruction :  confirmDelete(userID)
************************************/

function confirmDelete(userID) 
{
	
	var answer = confirm("Do You Want to delete credit card details?")
	if (answer)
	{
		window.location.href = "my_card_act.php?userID="+userID+"&act=delete";
		
	}
	else
	{
		return false;
	}
		
}
/*****************************
Function name :  makeSameShippingDetail
Return Type : none
Date Created : 21th July 2008
Date Last Modified : 21th July 2008
Author : Sumit Agrawal
Last Modified By :Sumit Agrawal
Comments : This is used to make shipping address as same as billing by checked checkbox.
User instruction :  makeSameShippingDetail(formObj)
************************************/
