/******************************************
Module name : Js Function file
Comments : The functions_js.js file contains various functions related to the web directory project.
******************************************/	


/****************COMMMOM ADMIN FUNCTION IS NEEDED FOR THE ADMIN LOGIN & LOGOUT & SETTINMG OF THE ADMIN ****************/


/*****************************
Function name : resetDate
Return type : none
Comments : This function is used to reset the date of a form
User instruction : resetDate()
************************************/
function resetDate()
{
 document.forms[0].frmDate.value = "From";
 document.forms[0].frmTodate.value = "To";
}

/*****************************
Function name : dateCompare
Return type : boolean
Comments : This function is used to validate the date compare form and to date.[ From date should be less than to date. ]
User instruction : dateCompare(formname)
************************************/
function dateCompare(formname)
{
 var sliptdate = document.getElementById(formname).frmTodate.value.split("-");
    var FromDate  = document.getElementById(formname).frmDate.value.split("-");
 /*********************** From Date *****************/
 var TY = FromDate[0];  //Year
 var TM = FromDate[1];  //Month
 var TD = FromDate[2];  //Date
 /******************* To Date *********************/
 var sY=sliptdate[0];  //Year
 var sM=sliptdate[1];  //Month
 var sD=sliptdate[2];  //Date
   
 if(document.getElementById(formname).frmDate.value != 'From' && document.getElementById(formname).frmTodate.value != 'To')
 {
  if(sY<TY ) 
  {
   alert("'To' date should be greater than 'From' date.");
   return false;   
  }
  else if(sM==TM && sD<TD && sY==TY) 
  { 
   alert("'To' date should be greater than 'From' date.");
   return false;
  }
  else if(sM<TM && sY==TY) 
  { 
   alert("'To' date should be greater than 'From' date.");
   return false;
  }
 }
 
 if(validateForm(formname, 'frmSearchOrderPrice', 'Order Price', 'isNaN'))
 { 
  return true;
 } 
 else 
 {
     return false;
 } 
 
}

/******************************************
Function name : showSearchBox
Return type : None
Comments : Function is used to show hide the seacch box
User instruction : showSearchBox()
******************************************/
function showSearchBox(varDocumentID, varShow)
{
	if(varShow == 'show')
	{
	 document.getElementById(varDocumentID).style.display = 'block';	
	}
	else
	{
	  document.getElementById(varDocumentID).style.display = 'none';
	}
	
}

/*****************************
Function name : checkCapsLock
Return type : none
Comments : This function is used to display alert message when caps lock is on.
User instruction : checkCapsLock( e , FieldID) 
************************************/
function checkCapsLock(e , FieldID) 
{
	var myKeyCode=0;
	var myShiftKey=false;
	var myMsg='Caps Lock is ON.\n\nTo prevent entering your password incorrectly,\nYou should press Caps Lock to turn it OFF.';

	// Internet Explorer 4+
	if ( document.all ) {
		myKeyCode=e.keyCode;
		myShiftKey=e.shiftKey;

	// Netscape 4
	} else if ( document.layers ) {
		myKeyCode=e.which;
		myShiftKey=( myKeyCode == 16 ) ? true : false;

	// Netscape 6
	} else if ( document.getElementById ) {
		myKeyCode=e.which;
		myShiftKey=( myKeyCode == 16 ) ? true : false;

	}
	
	if(document.getElementById(FieldID).value.length==0) {

		// Upper case letters are seen without depressing the Shift key, therefore Caps Lock is on
		if ( ( myKeyCode >= 65 && myKeyCode <= 90 ) && !myShiftKey ) {
			alert( myMsg );
			//return;
	
		// Lower case letters are seen while depressing the Shift key, therefore Caps Lock is on
		} else if ( ( myKeyCode >= 97 && myKeyCode <= 122 ) && myShiftKey ) {
			alert( myMsg );
		//	return;
		}
		return false;
	}
}
/******************************************
Function name : checkUserName
Return type : None
Comments : Function is used to login check using ajax.The ajax login check is a combination of functions all are using to check login system.
User instruction : checkUserName()
******************************************/
/* AJAX LOGIN CHECK CODE START FROM HERE */
function checkUserName() 
{
	var alphaNum = /^[0-9a-zA-Z_@.]+$/;
	var Usermail = document.getElementById('frm_login').frmAdminUserName.value;

	var charArray = new Array();
	var tString = "";
	for(i = 0; i < Usermail.length; i++) 
	{
		charArray[i] = Usermail.charAt(i);
	}

	for(i = 0; i < charArray.length; i++) 
	{
		if (charArray[i].match(alphaNum))
		{
			tString += charArray[i];
		}
	}	
	
	if (tString != "")
	{
		checkUserEmail(tString);
	}	
}
function checkUserEmail(mailID)
{ 
	
	doAjax('ajax_act.php','type=signUp&userEmail='+mailID,'showUserEmail','GET');
}
function showUserEmail(item)
{
		
	if(item)
	{
			
		document.getElementById('showUserName').style.display = 'none';	
	}
	else
	{
		document.getElementById('showUserName').style.display = 'inline';
	}

}

/*****************************
Function name : validateAdminForm
Return type : integer
Comments : This is used to check admin login authentications.
User instruction : validateAdminForm(charToCheck)
************************************/
function validateAdminLogin(formname)
{
	if(validateForm(formname,'frmAdminUserName','Username(Email)','R', 'frmAdminPassword','Password','R'))
	{	
		return true;
	} 
	else 
	{
		return false;
	} 
}

/******************************************
Function name : validator
Return type : boolean
Comments : Function will return the true or error message after validating checkboxes
User instruction : validator(btnType)
******************************************/
function validateForm() 
{ 
	var i,p,q,nm,test,num,min,max,errors='',args=validateForm.arguments;
	j=0;
	
	var regEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	var regBlank = /[^\s]/;
	
	//var regSpace = /^([a-zA-Z0-9_\!#@^&*%~-]+)$/;
	var regSpace = /^([a-zA-Z0-9-/_!#@]+)$/;
    var regAlphaNum = /^([a-zA-Z0-9_#@]+)$/;
	var regDate = /^([0-9_]+-[0-9][0-9]+-[0-9][0-9]+)$/; 
  	var regChar = /^([a-zA-Z]+)$/;
	var regNumeric = /^([0-9]+)$/; 
	//var regDecimal = /^([0-9]+|(\.?)[0-9]+)$/;
	var regDecimal = /^([0-9]{0,20}\.?[0-9]{1,2})$/;
	
	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+' and '+mesg_string+' must be same.\n';
						}
					}
				}
				else if(test.indexOf('isAlphaNum')!=-1)
				{
					result = trim(val);
					if(result.length==0){
					errors += '- '+nm+' is required.\n'; 
					}else{
						if(!regAlphaNum.test(val))
						{
							errors += '- '+nm+' is not valid.\n';
						}
					}
				
				}
				else if(test.indexOf('isNumeric')!= -1)
				{
						if(!regNumeric.test(val))
						{
							errors += '- '+nm+' must contain a numeric value.\n';
							
						}
				}
				else if(test.indexOf('isDecimal') != -1)
				{
					
					if(!regDecimal.test(val))
					{
						errors += '- '+nm+' must contain a number upto 2 decimal places.\n';
					}
				}
			
				
				else if(test.indexOf('isSpace')!=-1)
				{
					result = trim(val);
					
					if(result.length==0)
					{
						errors += '- '+nm+' is required.\n'; 
					}
					else
					{
						if(!regSpace.test(val))
						{
							errors += '- '+nm+' is not valid.\n';
						}
					}
				}
				else if (test.indexOf('isEmail')!=-1) 
				{ 
					p=val.indexOf('@');
					s=val.indexOf('.');
			        if (p<1 || p==(val.length-1))
					{
						errors+='- '+nm+' must contain an e-mail Address.\n';
		
					}
					else if(!regEmail.test(val))
					{
						errors+='- '+nm+' must contain a valid e-mail Address.\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('isChar')!=-1) 
				 { 
					var first_char;
					
					if(val.match(regChar)==null)
					{
					 	errors+='- '+nm+' must contain a character.\n';
					}
			     }
				else if(test.indexOf('isCheckbox')!=-1)//Check is check box is not checked generate error
				{	
					var valueCheckbox = noVal.checked;
					if(!valueCheckbox)
					{
						errors+='- '+' Accept terms and condition.\n';
					}
				}
				else if (test.charAt(0)=='R')
				{
					result = trim(val);
					if(result.length==0){
						
					errors += '- '+nm+' is required.\n'; 
					}
				} 
			
		}
		else if (test.charAt(0)=='R')
		{
			result = trim(val);
				if(result.length==0){
					
				errors += '- '+nm+' is required.\n'; 
				}
		}
		
		 if (test.indexOf('isDate')!=-1) 
				{ 
					//alert("vineet");
					p=val.indexOf('-');
			       // alert(test.indexOf('isDate'));
			       	var sliptdate	= val.split("-");
					
					/*******************Added by rupesh Date is not before current date and month*********************/
					var today=new Date(),TY=today.getFullYear(),TM=today.getMonth(),TD=today.getDate(),TH=today.getHours();
					TM+=1;			
					if(TM<=9) 
					{	
						TM='0'+TM;
					}
					if(TD<=9)
					{
						TD='0'+TD;
					}
					/*******************Added by rupesh*********************/
					
					var sY=sliptdate[0];
					var sM=sliptdate[1];
					var sD=sliptdate[2];
					
					/*******************Added by rupesh*********************/
					//alert(TM);
				  if(sY>1)
				   {     
						
						if(sY<TY )
						{
						    errors+='- '+nm+' should be greater than current date.\n';
						}
						else if(sM==TM && sD<TD && sY==TY) 
						{ 
	
							errors+='- '+nm+' should be greater than current date.\n';
							
						}
						else if(sM<TM && sY==TY) 
						{ 
	
							errors+='- '+nm+' should be greater than current date.\n';
					    }
				   }
				
				}
		if(errors !="")
		{	if(j<=0)
			{
				focusitem = document.forms[""+args[0]].elements[""+args[i]];
				j++;
			}	
		}
		}
		
	} 
	
  if (errors)
  {
	var MasterString = getMasterString();
	alert(MasterString+'\n'+errors);
	focusitem.focus();
	return false;
   }
   else
	return true;

  document.MM_returnValue = (errors == '');
}
/*****************************
Function name : validateEmailChange
Return type : boolean
Comments : This function is used to validate admin notification email id.
User instruction : validateEmailChange(charToCheck)
************************************/
function validateEmailChange(formname)
{
	if(validateForm(formname, 'frmAdminEmail','Email','RisEmail'))
	{			
		var flag=confirm('Are you sure you want to change notification E-mail?')
		if(flag)
		return true;
		else
		return false;		
	} 
	else 
	{
		return false;
	} 
}
/*****************************
Function name : validateChangePassword
Return type : boolean
Comments : This is used to validate admin password and confirm passwords.
User instruction : validateChangePassword(formname)
************************************/
function validateChangePassword(formname)
{
	if(validateForm(document.getElementById(formname).id,'frmAdminOldPassword', 'Current Password', 'RisSpace', 'frmAdminNewPassword', 'New Password','RisSpace','frmAdminConfirmPassword', 'Confirm New Password', 'RisEqualfrmAdminNewPassword:New Password'))
	{			
		var flag=confirm('Are you sure you want to change password?')
		if(flag)
		{
			return true;
		}
		else
		{
			return false;		
		}
	} 
	else 
	{
		return false;
	} 

}

/*****************************
Function name : validateResetPassword
Return type : boolean
Comments : This is used to validate admin password and confirm passwords.
User instruction : validateResetPassword(formname)
************************************/
function validateResetPassword(formname)
{
	if(validateForm(document.getElementById(formname).id, 'frmNewPassword', 'New Password','RisSpace','frmConfirmNewPassword', 'Confirm New Password', 'RisEqualfrmNewPassword:New Password'))
	{	
		return true;
	} 
	else 
	{
		return false;
	} 

}

/*****************************
Function name : validateAdminForgotPassword
Return type : none
Comments : This function is used to validate forgot password form.
User instruction : validateAdminForgotPassword(formname)
************************************/
function validateForgotPassword(formname)
{
	if(validateForm(formname,'frmUserName','Username (E-mail) ','RisEmail','frmSecurityCode','Verification code','R'))
	{	
		return true;
	} 
	else 
	{
		return false;
	} 
}

/******************************************
Function name : ltrim
Return type : string
Comments : Function will return the main string after removing white spaces from the left
User instruction : ltrim(str)
******************************************/
function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
/******************************************
Function name : rtrim
Return type : string
Comments : Function will return the main string after removing white spaces from the right
User instruction : rtrim(str)
******************************************/
function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}
/******************************************
Function name : trim
Return type : string
Comments : Function will return the main string after removing white spaces from the right and left of the main string
User instruction : trim(str)
******************************************/
function trim(str) {
	return ltrim(rtrim(str));
}
/******************************************
Function name : isWhitespace
Return type : integer
Comments : Function will return the index of white space encounter in the string.
User instruction : isWhitespace(charToCheck)
******************************************/
function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}
/******************************************
Function name : checkError
Return type : boolean
Comments : Function will return the true or false acording to form validation
User instruction : checkError(error)
******************************************/
function checkError(error)
{
 var flag=false;
 var MasterString = getMasterString();
 
 if(error != "")
 {
  MasterString = MasterString + error;
  flag=true;
 }
 
 if(flag == true)
 {
  alert(MasterString);
  return false;
 }
 else
  return true;
}
/******************************************
Function name : getMasterString
Return type : boolean
Comments : Function will return the main string
User instruction : getMasterString()
******************************************/
function getMasterString()
{
 return "Sorry, we can not complete your request.\nKindly provide us the missing or incorrect information shown below.\n";
}
/******************************************
Function name : toggleOption
Return type : None
Comments : Function will toggle the select all checkbox option.
User instruction : toggleOption(spanChk)
******************************************/
 
function toggleOption(spanChk)
{
 
 var xState=spanChk.checked;
 var theBox=spanChk;
 
 elm=theBox.form.elements;
 
 for(i=0;i<elm.length;i++)
 {
  	
	if(elm[i].type=="checkbox" && elm[i].id!=theBox.id && !elm[i].disabled)
  	{
  		if(xState == false)
    		elm[i].checked = false;
   		else
    		elm[i].checked = true;
  	}
 }
}
/******************************************
Function name : deSelectCheckbox
Return type : None
Comments : Function will deselect the main checkbox
User instruction : deSelectCheckbox(spanChk)
******************************************/
function deSelectCheckbox(formname)
{
 document.getElementById('Main').checked = false;
}
 
/******************************************
Function name : setvalidAction
Return type : boolean
Comments : Function will ask for confirmation of updating records
User instruction : setValidAction(value, formname,listname)
******************************************/
function  setValidAction(value, formname, listname)
{
	if(value == 'Delete' || value.indexOf ('Delete')>-1)
	 {
	  	message = "delete selected "+listname;  
	 }
	 else
	 {
	  	message = "change status of selected "+listname;
	 }
	 var flag = validator(message,formname);   
	 if(flag)
	 {   
	  	formname.submit();
	 }
	 else
	 {
		formname.frmChangeAction.value=''; 
	  	document.getElementById('Main').checked = false;
	  	if(listname == 'Message(s)' || listname =='Customer(s)' || listname =='Order(s)'|| listname =='Subscriber(s)' ||  listname == 'Category(s)' ||  listname == 'Competitor(s)' ||  listname == 'Loyalty(s)' ||  listname =='Product Accessory(s)' ||  listname =='Product(s)')
	  	{
	   		document.forms[1].Main.checked=false; 
	   		elm=document.forms[1].elements;
	  	}
	 	else
	  	{
		   document.forms[0].Main.checked=false; 
		   elm=document.forms[0].elements; 
	  	}
	  
		for(i=0;i<elm.length;i++)
		{
		   //alert(elm[i].type);
		   if(elm[i].type == "checkbox" )
		   {   
				elm[i].checked = false;
		   }
		}
	 	return false;
	 }
}
/******************************************
Function name : validator
Return type : boolean
Comments : Function will return the true or error message after validating checkboxes
User instruction : validator(btnType)
******************************************/
var btnType;
function validator(btnType,formname)
{
 
 var obj = formname;
 var error="", flagCheck=0;
 
 var len = obj.elements.length; 
 var i=0;
 for(i=0;i<len;i++) 
 {
  if(obj.elements[i].type=='checkbox')
  {
   if(obj.elements[i].checked)
   {
    //if(btnType == 'Delete')
     return askConfirm(btnType);
    //else
     //return true;
   }
   else
    flagCheck = 1;
  }
 }
 
 if(flagCheck == 1)
  error += "\n Please select at least one record.";
   
 return checkError(error);
}
/******************************************
Function name : askConfirm
Return type : boolean
Comments : Function will return the true or false after asking for confirmation
User instruction : askConfirm(type)
******************************************/
function askConfirm(type)
{ 
 var sen = "Are you sure you want to "+type+"?";
 if(confirm(sen))
 {
  return true;
 }
 else
 {
  return false;
 }
}

/****************************************NEWSLETTER SECTION JS START******************************************/
/**************************************************************************
Function name : validateNewsletterForm
Return type : boolean
Comments : This function is used to show validate Newsletter form..
User instruction : validateNewsletterForm(formname)
***************************************************************************/
function validateNewsletterForm(formname)
{
	
	if(validateForm(formname,'frmNewsletterTitle','Title','R'))
	{	
		return true;
	} 
	else 
	{
		return false;
	} 
}
/*****************************
Function name : NewsletterReciever
Return type : none
Comments : This function is used to show hide newsletter reciever.
User instruction : NewsletterReciever(formname)
************************************/
function NewsletterReciever(value)
{
	if(value == 'AllSubscribers')
	{
		document.getElementById('frmSubscriberID[]').disabled=true;
	}
	if(value=='Subscribers')
	{
		document.getElementById('frmSubscriberID[]').disabled=false;
	}
}
/**************************************************************************
Function name : validateSendNewsletter
Return type : boolean
Comments : This function is used to show validate newsletter send form..
User instruction : validateSendNewsletter(formname)
***************************************************************************/
function validateSendNewsletter(formname)
{
	if(document.getElementById('frmSendToSubscribers').checked)
	{
		if(validateForm(formname,'frmSubscriberID[]','Subscriber ','R','frmNewsletterSubject','Subject','R'))
		{	
			flag = confirm('Are you sure you want to send this newsletter to selected Subscriber(s)?');
			if(flag)
			{
				return true;
			}
			else
			{
				return false;
			}
		} 
		else 
		{
			return false;
		} 
	}
	if(document.getElementById('frmSendToAll').checked)
	{
		if(validateForm(formname,'frmNewsletterSubject','Subject','R'))
		{	
			flag = confirm('Are you sure you want to send this newsletter to all Subscribers?');
			if(flag)
			{
				return true;
			}
			else
			{
				return false;
			}
		} 
		else 
		{
			return false;
		} 
	}
}
/****************************************NEWSLETTER SECTION JS END******************************************/


/*****************************
Function name : validateCustomer
Return type : none
Comments : This function is used to validate the Domain form
User instruction :  validateUser(formname)
************************************/

function validateCustomer(formname)
{
	if(document.getElementById(formname).frmPasswordIsSet.value == 'No' && document.getElementById(formname).frmConPasswordIsSet.value == 'No')
	{   
		if(validateForm(formname, 'frmCustomerFirstName', 'First Name', 'R', 'frmCustomerLastName', 'Last Name', 'R', 'frmCustomerUserName', 'Username', 'RisEmail', 'frmCustomerPassword', 'Password', 'RisSpace', 'frmCustomerConfirmPassword', 'Confirm Password', 'RisEqualfrmCustomerPassword:Password','frmCustomerAlternativeEmail','Alternative Email', 'isEmail', 'frmCustomerCountry', 'Country', 'R', 'frmCustomerState', 'State', 'R', 'frmCustomerCity', 'City', 'R', 'frmCustomerZipcode', 'Zipcode', 'isAlphaNum', 'frmCustomerPhone', 'Phone', 'R'))
		{  
			return true;
		} 
		else 
		{
			return false;
		}
	}
	else
	{
		if(document.getElementById(formname).frmPasswordIsSet.value == 'Yes' && document.getElementById(formname).frmConPasswordIsSet.value == 'Yes')
		{
			if(validateForm(formname, 'frmCustomerFirstName', 'First Name', 'R', 'frmCustomerLastName', 'Last Name', 'R', 'frmCustomerUserName', 'Username', 'RisEmail','frmCustomerAlternativeEmail','Alternative Email', 'isEmail', 'frmCustomerCountry', 'Country', 'R', 'frmCustomerState', 'State', 'R', 'frmCustomerCity', 'City', 'R', 'frmCustomerZipcode', 'Zipcode', 'isAlphaNum', 'frmCustomerPhone', 'Phone', 'R'))
			{  
				return true;
			} 
			else 
			{
				return false;
			}
		}		
	}
}


/***********************ORDER SECTION JS START****************************************************************/

/*****************************
Function name : changePaymentStatus
Return type : integer
Comments : This function is used to change and validate order status to paid or unpaid.
User instruction : changePaymentStatus(payment,recordID)
************************************/
function changePaymentStatus(payment,recordID)
 {
   
   if(payment == "Paid")
   {
	var flag=confirm('Are you sure you want to change payment status?')
	 if(flag) 
	  {
	    
		document.forms[1].action='order_action.php?RecordID='+recordID;
		document.forms[1].submit();
	    return true;  
	  }
	 else
	  {
		var PaymentStatus = 'frmPaymentStatus'+recordID;
      
		document.getElementById(PaymentStatus).value='Unpaid';
	  
		return false;    
      }
	}
  }

/*****************************
Function name : changeOrderStatus
Return type : integer
Comments : This function is used to change order status.
User instruction : changeOrderStatus(OrderStatus,recordID)
************************************/
function changeOrderStatus(OrderStatus,recordID)
{
	
  if(OrderStatus != "Pending")
   {
	 var flag=confirm('Are you sure you want to change order status?')
	 if(flag) 
	  {
	    
		document.forms[1].action='order_action.php?OrderStatus='+OrderStatus+'&RecordID='+recordID;
		document.forms[1].submit();
	    return true;  
	  }
	 else
	  {
		var OrderStatus = 'frmOrderStatus'+recordID;
		
		document.getElementById(OrderStatus).value='Pending';
	    return false;    
      }
	}
}
  
/******************************************
Function name : showPriceField
Return type : boolean
Comments : Function will call the ajax function.
******************************************/
function showPriceField(FieldValue)
{
	if(FieldValue=='')
	{
		document.getElementById('frmPrice').disabled=true;
	}
	else
	{
		document.getElementById('frmPrice').disabled=false;
	}
	
}

/**********************ORDER SECTION JS END*******************************************************/


/**************************************************************************
Function name : validateCategoryForm
Return type : boolean
Comments : This function is used to validate category form..
User instruction : validateCategoryForm(formname)
***************************************************************************/
function validateCategoryForm(formname)
{
	
	if(validateForm(formname,'frmCategoryName','Category Name','R'))
	{	
		return true;
	} 
	else 
	{
		return false;
	} 
}

/*****************************
Function name : validateCMSForm
Return type : bollean
Comments : This function is used to validate the CMS form.
User instruction : validateCMSForm(formname)
************************************/
function validateCMSForm(formname)
{
	if(validateForm(formname,'frmPageTitle','Page Title','R','frmPageContents','Contents','R','frmPageKeywords','Meta Keywords','R','frmPageDescription','Meta Description','R'))
	{	
		return true;
	} 
	else 
	{
		return false;
	} 
}

/*****************************
Function name : validateAdvantageForm
Return type : bollean
Comments : This function is used to validate the CMS form.
User instruction : validateCMSForm(formname)
************************************/
function validateAdvantageForm(formname)
{
	if(validateForm(formname,'frmAdvantageTitle','Advantage Title','R'))
	{	
		return true;
	} 
	else 
	{
		return false;
	} 
}
/*****************************
Function name : getProductName
Return type : boolean
Comments : This function is used to get the product name on the bases of id passed using ajax
User instruction : getProductName(categoryid)
************************************/

function getProductName(categoryid)
{
	doAjax('ajax_act.php','categoryID='+categoryid,'showProductResponse','GET',1);
}
function showProductResponse(item)
{   
   	document.getElementById('productfirst').innerHTML = item;
	document.getElementById('productfirst').style.display = 'block';
}

/*****************************
Function name : validateProducts
Return type : boolean
Comments : This function is used to validate the product form
User instruction : validateProducts(formname)
************************************/
function validateProducts(formname)
{
if(validateForm(formname,'frmfkCategoryID', 'Product Category', 'R','frmProductName','Product Name','R','frmProductCode','Product Code','R','frmProductQuantity','Product Set of Quantity','RisNumeric',/*'frmProductQuantityAvailable','Product Quantity Available','RisNumeric',*/'frmProductDescription','Product Description','R','frmProductSpecifications', 'Product Specifications', 'R','frmProductCost','Product Unit Cost','RisDecimal'))
	{  
		
		return true;
		
	} 
	else 
	{
		return false;
	}
}

/*****************************
Function name : validateInventoryForm
Return type : boolean
Comments : This function is used to validate the tax and discount form
User instruction : validateTaxDiscount(formname)
************************************/
function  validateTaxDiscount(formname)
{
	/*var bolResult = validateForm(formname,'frmTaxDiscountType','Type','R');
	alert(bolResult);*/
	
	if(validateForm(formname,'frmTaxDiscountType','Type','R','frmTaxDiscountName','Name','R','frmTaxDiscountValue','Percentage','RisDecimal'))
	{
		var percentageVal = document.getElementById('frmTaxDiscountValue').value;
		if(percentageVal > 100)
		{
			errmsg = '- Percentage should not be greater than 100.';
		}
		if(errmsg)
		{
			var MasterString = getMasterString();
			alert(MasterString+'\n'+errmsg);
			return false;
		}
		else
		{
			return true;
		}
	} 
	else 
	{
		return false;
	}	
}

/*****************************
Function name : getTaxAmount
Return type : None
Comments : This function is used to call ajax fucntion for tax ammount
User instruction : getTaxAmount(formname)
************************************/
function getTaxAmount(formid)
{
	formid = 'frmProductAdd';
	if(!validateForm(formid,'frmProductCost', 'Product Cost', 'RisDecimal'))
	{  
		return false;
			
	}
	var varObject = document.getElementById(formid).frmTaxID;
	var arrSelectedTaxID = new Array();
		j = 0 ;
		for(var i = 0; i < varObject.length; i++)
		{
			if(varObject.options[i].selected == true && varObject.options[i].value != '')	
			{
				arrSelectedTaxID[j] = varObject.options[i].value;
				j++;
			}
			
		}
	var varSelectedOfferCount = arrSelectedTaxID.length;
		
	//MAKE COMMA SEPERATED VALUES FROM AN ARRAY.
	var varCommaSeperatedTaxID = arrSelectedTaxID.join(',');
		
	var discount = document.getElementById('frmDiscountAmount').value;
	
	var productcost = document.getElementById(formid).frmProductCost.value;
	if(varCommaSeperatedTaxID != '')
	{
		doAjax('ajax_act.php','taxid='+varCommaSeperatedTaxID+'&cost='+productcost+'&discount='+discount,'showTaxAmount','GET',1);
	}
    else
    {
       document.getElementById('frmTaxID').value = "";
	   document.getElementById('frmTaxAmount').value = '0';
	}
}

/*****************************
Function name : showTaxAmount
Return type : HTML
Comments : This function is used display the total tax ajax response for the price 
User instruction : showTaxAmount(formname)
************************************/
function showTaxAmount(item)
{   
	document.getElementById('frmTaxDisplay').innerHTML = item;
	document.getElementById('frmTaxDisplay').style.display = 'block';
}




/*****************************
Function name : getDiscountAmount
Return type : none
Comments : This function is used to call ajax fucntion for tax ammount
User instruction : getDiscountAmount(formname)
************************************/
function getDiscountAmount(formid)
{
	if(!validateForm(formid,'frmProductCost', 'Product Cost', 'RisDecimal'))
	{  
		return false;
			
	}
	var varObject = document.getElementById(formid).frmDiscountID;
	var arrSelectedDiscountID = new Array();
		j = 0 ;
		for(var i = 0; i < varObject.length; i++)
		{
			if(varObject.options[i].selected == true && varObject.options[i].value != '')	
			{
				arrSelectedDiscountID[j] = varObject.options[i].value;
				j++;
			}
			
		}
		var varSelectedDiscountCount = arrSelectedDiscountID.length;
		
		//MAKE COMMA SEPERATED VALUES FROM AN ARRAY.
		var varCommaSeperatedDiscountID = arrSelectedDiscountID.join(',');
	
	var productcost = document.getElementById(formid).frmProductCost.value;
	
	if(varCommaSeperatedDiscountID != '')
	{
		doAjax('ajax_act.php','discountid='+varCommaSeperatedDiscountID+'&cost='+productcost,'showDiscountAmount','GET',1);
	}
	else
    {
       document.getElementById('frmDiscountID').value = "";
	   document.getElementById('frmDiscountAmount').value = '0';
	}
	
}

/*****************************
Function name : showDiscountAmount
Return type : HTML
Comments : This function is used display the total discount for the product ajax response for the price 
User instruction : showDiscountAmount(formname)
************************************/
function showDiscountAmount(item)
{   
	document.getElementById('frmDiscountDisplay').innerHTML = item;
	document.getElementById('frmDiscountDisplay').style.display = 'block';
}

/*****************************
Function name : setProductPrice
Return type : float
Comments : This function is used set total price for the product 
User instruction : setProductPrice()
************************************/
function setProductPrice()
{   
	var productcost = document.getElementById('frmProductCost').value;
	var tax = document.getElementById('frmTaxAmount').value;
	var discount = document.getElementById('frmDiscountAmount').value;
	if(productcost != '')
	{
		total = (productcost - discount) + parseFloat(tax);
		var strtotal = total.toString();
		if(strtotal.indexOf ('.') > 0)
		{
			arrDecimal = strtotal.split(".");
	 		strtotal = arrDecimal[0]+'.'+arrDecimal[1].substring(0,2);
		}
		if(tax == '' && discount == '')
		{
			document.getElementById('frmProductActualCost').value = productcost;
		}
		else
		{
			document.getElementById('frmProductActualCost').value = strtotal;
		}
	}
}


//*************************FRONT END JS FUNCTION STARTS FROM HERE***************************
/*****************************
Function name : validateRegisterForm
Return type : boolean
Comments : This function is used to validate user Register Form
User instruction : 
************************************/
function validateRegisterForm(formname)
{
	if(validateForm(formname,'frmCustomerFirstName', 'First Name', 'R', 'frmCustomerLastName', 'Last Name', 'R', 'frmCustomerEmailID', 'Username(Email)','RisEmail', 'frmCustomerPassword', 'Password', 'R', 'frmCustomerConfirmPassword', 'Confirm Password', 'RisEqualfrmCustomerPassword:Password', 'frmCustomerAdressOne', 'Address', 'R', 'frmCustomerCounty', 'State/County', 'R','frmCustomerCountry', 'Country', 'R','frmCustomerZipcode', 'Zip/Postal Code', 'isAlphaNum', 'Terms and condition','isCheckbox'))
	{
		return true;
	}
	else
	{
		return false;
	}
}	 



/*****************************
Function name : validateRegisterForm
Return type : boolean
Comments : This function is used to validate user Register Form
User instruction : 
************************************/
function validatefrmProfRegisterForm(formname)
{
	if(validateForm(formname,'frmProfCustomerFirstName', 'First Name', 'R', 'frmProfCustomerLastName', 'Last Name', 'R', 'frmProfCustomerEmailID', 'Email','RisEmail', 'frmProfCustomerAdressOne', 'Address', 'R', 'frmProfCustomerCountry', 'Country', 'R'))
	{
		return true;
	}
	else
	{
		return false;
	}
}	 

/******************************************
Function name : showCityWithAjax
Return type : None
Comments : Function is used to show hide the City box
User instruction : showCityBox()
******************************************/
function showState(countryID)
{
	doAjax('ajax_act.php','type=showState&countryID='+countryID, 'showCountryState', 'GET');
}

/******************************************
Function name : showCountryState
Return type : None
Comments : Function is used to show hide country state
User instruction : showCountryState()
******************************************/
function showCountryState(item)
{
   document.getElementById('StateText').innerHTML = item;
}
/**************************************************************************
Function name : confirmMessage
Return type : boolean
Comments : This function is used to show validate subscribers Email .
User instruction : confirmMessage(formname)
***************************************************************************/
function validateSubscriberEmail(formname)
{
	var Email = document.getElementById('frmSubscriberEmail').value;
	if(Email == 'Enter your email to subscribe')
	{
		$varMsg = getMasterString()+'\n - Email is required.'
		alert($varMsg);
		return false;
	}
	else
	{
		if(validateForm(formname,'frmSubscriberEmail','Email','RisEmail', 'frmAdminPassword','Password','R'))
		{	
			return true;
		} 
		else 
		{
			return false;
		}
	}
}


/*****************************
Function name : validateLoginForm
Return type : none
Comments : This function is used to validate Login form.
User instruction : validateLoginForm(charToCheck)
************************************/
function validateLoginForm(formname)
{
	if(validateForm(formname,'frmCustomerEmailID','Username','RisEmail','frmCustomerPassword','Password','R'))
	{	
		
		return true;
	} 
	else 
	{
			
		return false;
	} 
}

/*****************************
Function name : validateForgotPasswordForm
Return type : boolean
Comments : This function is used to validate user forgot password form
User instruction : validateEmailID(charToCheck)
************************************/
function validateForgotPasswordForm(formname)
{
	
	if(validateForm(formname,'frmCustomerEmailID', 'Username', 'RisEmail', 'frmVerificationCode', 'Verification Code', 'R'))
	{
		return true;
	}
	else
	{
		return false;
	}
}
/*****************************
Function name : validateEditForm
Return type : boolean
Comments : This function is used to validate user Register Form
User instruction : 
************************************/
function  validateEditForm(formname)
{
	if(validateForm(formname,'frmCustomerFirstName', 'First Name', 'R', 'frmCustomerLastName', 'Last Name', 'R', 'frmCustomerAdressOne', 'Street Address 1', 'R','frmCustomerCountry', 'Country', 'R','frmCustomerState','State','R', 'frmCustomerCity', 'City', 'R', 'frmCustomerZipcode', 'Zip/Postal Code', 'isAlphaNum'))
	{
		return true;
	}
	else
	{
		return false;
	}
}	 

/*****************************
Function name : validateUserChangePassword
Return type : boolean
Comments : This is used to validate User change password form
User instruction : validChangePass(charToCheck)
************************************/
function validateUserChangePassword(formname)
{
	if(validateForm(document.getElementById(formname).id,'frmUserOldPassword', 'Current Password', 'RisSpace', 'frmUserNewPassword', 'New Password','RisSpace','frmUserConfirmPassword','Confirm New Password','RisEqualfrmUserNewPassword:New Password'))
	{			
		var flag=confirm('Are you sure you want to change password?')
		if(flag)
		{
			return true;
		}
		else
		{
			document.getElementById('frmUserOldPassword').value='';
			document.getElementById('frmUserNewPassword').value='';
			document.getElementById('frmUserConfirmPassword').value='';
			document.getElementById('frmUserOldPassword').focus();
			return false;		
		}
	} 
	else 
	{
		document.getElementById('frmUserOldPassword').value='';
		document.getElementById('frmUserNewPassword').value='';
		document.getElementById('frmUserConfirmPassword').value='';
		document.getElementById('frmUserOldPassword').focus();
	    return false;
	} 
}
/*****************************
Function name : confirmDelete
Return type : integer
Comments : This function is used to validate the User.
User instruction : validateUser(formname)
************************************/  
function confirmDelete(formname) 
{
	var flagVal  = carValidator('remove the product(s) from Shopping Cart',formname);
   	if (flagVal)
	{
		document.frmShoppingCart.frmProcess.value = "DeletefromCart";
		document.frmShoppingCart.action = "shopping_cart_action.php";
		document.frmShoppingCart.submit();
	}
	else
	{
		
		var obj = document.forms[0];
	    var error="", flagCheck=0;
	    var len = obj.elements.length; 
	    var i=0;
	    for(i=0;i<len;i++) 
	    {
		  if(obj.elements[i].type=='checkbox')
		  {
			if(obj.elements[i].checked)
			{
				obj.elements[i].checked = false;
			}
			
		 }
	  }
		
		return false;
	}
}

/*****************************
Function name : confirmDelete
Return type : integer
Comments : This function is used to validate the User.
User instruction : validateUser(formname)
************************************/  
var btnType;
function carValidator(btnType,formname)
{
	var obj = document.forms[formname];
	var error="", flagCheck=0;
	var len = obj.elements.length;
	var i=0;
	for(i=0;i<len;i++) 
	{
		if(obj.elements[i].type=='checkbox')
		{
			if(obj.elements[i].checked)
			{
				//if(btnType == 'Delete')
					if(askConfirm(btnType))
					{
						return true;
					}
					else
					{
						for(i=0;i<len;i++) 
						{
							if(obj.elements[i].type == 'checkbox')
							{
								if(obj.elements[i].checked)
								{
									obj.elements[i].checked = false;
								}
							}
						}
						return false;

					}
				//else
					//return true;
			}
			else
				flagCheck = 1;
		}
	}
	if(flagCheck == 1)
		error += "\n - Please select at least one record.";
	return checkError(error);


}
/*****************************
Function name : validqyantity
Return type : integer
Comments : This function is used to validate the User.
User instruction : validateUser(formname)
************************************/  
function validQuantity(val, formname)
{
	var isOK = true;
	var obj = document.forms[formname];
	var lent = obj.elements.length;
	var regNumeric = /^([0-9]+)$/; 
	with(val)
	  { 
		for (var i = 0; i < lent ; i++)
		{ 
		
		  if ((document.forms[formname].elements[i].type == "text") )
		  { 
			if (document.forms[formname].elements[i].value == 0 || (!regNumeric.test(document.forms[formname].elements[i].value)))      
			{ 
			  isOK = false;
			 
			} 
		   } 
		} 
	 }
	if(!(isOK))
	{	
		error ='\n - Please enter valid quantity.';
		checkError(error);
		return false;
		
	}
	else
	{
	document.frmShoppingCart.frmProcess.value = "UpdateCart"
	document.frmShoppingCart.action="shopping_cart_action.php";
	document.frmShoppingCart.submit();
	return true;
	}
}
/*****************************
Function name : confirmSpecial
Return type : none
Comments : This function is used to conform whether an user wants to make a product special.
User instruction : validateUser(formname)
************************************/  
function confirmSpecial(proName)
{ 
	  var specialoffer = document.getElementById('frmProductIsSpecailOffer').value;
	  if(specialoffer == 'Yes')
	  {
		  var flag= confirm('The product '+proName+' is currently marked as a Special Product. Would you like to make this product as special product');
		  if(!flag)
		  {
			  document.getElementById('frmProductIsSpecailOffer').value = 'No'; 
		  }
	  }
}
	
/*****************************
Function name : validateContactusForm
Return type : boolean
Comments : This is used to check validate form.
User instruction : validateComment()
************************************/

function validateContactusForm(formname)
{
	if(validateForm(formname, 'frmEmail', 'Email','RisEmail','frmName', 'Name', 'R', 'frmSubject', 'Subject', 'R', 'frmMessage', 'Message', 'R'))
	{	
		return true;
	} 
	else 
	{
		
		return false;
	} 
}

function startDownload()  
{  
	var url='http://www.avongroups.in/delivery/nutrigro/components/test.zip';    
	mywindow=window.open(url,'Download', "location=1,status=1,scrollbars=1,width=50,height=50");
  	mywindow.moveTo(0,0);
}  

function openPdf()  
{  
	var url='http://www.avongroups.in/delivery/nutrigro/common/pdf/nutrigro.pdf';    
	mywindow=window.open(url,'Nutrigro', "location=1,status=1,scrollbars=1");  
}  

/******************************************
Function name : copyBillingValue
Return type : none
Comments : Function will copy all the Billing informations in shipping informations.
User instruction : copyBillingValue(id)
******************************************/
function copyBillingValue(id)
{
	if(document.getElementById('frmAccountcheck').checked)
	{
		document.getElementById('frmCustomerShippingAddressOne').value = document.getElementById('frmCustomerAddressOne').value;
		document.getElementById('frmCustomerShippingAddressOne').disabled = true;
		/*document.getElementById('frmCustomerShippingAddressTwo').value = document.getElementById('frmCustomerAddressTwo').value;
		document.getElementById('frmCustomerShippingAddressTwo').disabled = true;*/
		document.getElementById('frmCustomerShippingCountry').value = document.getElementById('frmCustomerCountry').value;
		document.getElementById('frmCustomerShippingCountry').disabled = true;
		//document.getElementById('frmCustomerShippingState').value = document.getElementById('frmCustomerState').value;
		//document.getElementById('frmCustomerShippingState').disabled = true;
		document.getElementById('frmCustomerShippingCity').value = document.getElementById('frmCustomerCity').value;
		document.getElementById('frmCustomerShippingCity').disabled = true;
		document.getElementById('frmCustomerShippingZipcode').value = document.getElementById('frmCustomerZipcode').value;
		document.getElementById('frmCustomerShippingZipcode').disabled = true;
		document.getElementById('frmCustomerShippingPhone').value = document.getElementById('frmCustomerPhone').value;
		document.getElementById('frmCustomerShippingPhone').disabled = true;
		
	}
	else
	{
		document.getElementById('frmCustomerShippingAddressOne').value = '';
		document.getElementById('frmCustomerShippingAddressOne').disabled = false;
		/*document.getElementById('frmCustomerShippingAddressTwo').value = '';
		document.getElementById('frmCustomerShippingAddressTwo').disabled = false;*/
		document.getElementById('frmCustomerShippingCountry').value = '';
		document.getElementById('frmCustomerShippingCountry').disabled = false;
		//document.getElementById('frmCustomerShippingState').value = '';	
		//document.getElementById('frmCustomerShippingState').disabled = false;
		document.getElementById('frmCustomerShippingCity').value = '';
		document.getElementById('frmCustomerShippingCity').disabled = false;
		document.getElementById('frmCustomerShippingZipcode').value = '';
		document.getElementById('frmCustomerShippingZipcode').disabled = false;
		document.getElementById('frmCustomerShippingPhone').value = '';
		document.getElementById('frmCustomerShippingPhone').disabled = false;
		
	}
}

/******************************************
Function name : validateAccountDetails
Return type : boolean
Comments : Function will validate account details from 
User instruction :validateAccountDetails(formname)
******************************************/
function validateAccountDetails(formname, hdnProcess)
{ 
   return chkCardDetails(hdnProcess);
}

/******************************************
Function name : deleteImage
Return type : boolean
Comments : Function will validate account details from 
User instruction :validateAccountDetails(formname)
******************************************/
function deleteImage(ProID)
{
	 var flag=confirm('Are you sure you want to delete the image?');
	 if(flag)
	 {
		location.href='product_action.php?pid='+ProID+'&action=delete';
  	 }
	 else
	 {
		return false;
	 }
}