//====================================================================================================
//	File Name		:	user.js
//	Author			: 	Himanshu Ahuja
//----------------------------------------------------------------------------------------------------
//	Purpose			:	Client side validation in JavaScript.
//====================================================================================================

//====================================================================================================
//	Function Name	:	Form_Submit()
//	Purpose			:	This function will check inputted field by user are valid or not.
//	Return			:	none
//----------------------------------------------------------------------------------------------------

function Add_Form_Submit(frm)
{
	
	with(frm)
    {
		if(!IsEmpty(login_name, 'Please, enter the Login Name.'))
		{
			return false;
		}
		if(!IsEmail(login_name, 'Please, enter the Valid Login Name.'))
		{
			return false;
		}
		if(!IsEmpty(password, 'Please, enter the Password.'))
		{
			return false;
		}
		if(!IsEmpty(first_name, 'Please, enter the first name.'))
		{
			return false;
		}
		if(!IsEmpty(last_name, 'Please, enter the last name.'))
		{
			return false;
		}
		if(!IsEmpty(phone, 'Please, enter the phone.'))
		{
			return false;
		}
		
		Submit.value='Save';
		submit();
		return true;
    }
}


function Add_Form_Submit_myaccount(frm)
{
	
	with(frm)
    {
		if(!IsEmpty(first_name, 'Please, enter the first name.'))
		{
			return false;
		}
		if(!IsEmpty(last_name, 'Please, enter the last name.'))
		{
			return false;
		}
		if(!IsEmpty(phone, 'Please, enter the phone.'))
		{
			return false;
		}
		Submit.value='Save';
		submit();
		return true;
    }
}

function IsEmpty(fld,msg)
{
	if((fld.value == "" || fld.value.length == 0) && (msg == ''))
	{
		return false;
	}
	if(fld.value == "" || fld.value.length == 0)
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

function IsEmail(fld,msg)
{
	var regex = /^[\w]+(\.[\w]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/ ;
	if(!regex.test(fld.value))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

function IsValidString(fld,msg)
{
	var regex = /^[_]*[a-zA-Z_]+[a-zA-Z0-9_]*$/;
	if(!regex.test(fld.value))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

function Form_Submit_login(frm)
{
	with(frm)
    {
		//alert("sdfsdf");
		if(!IsEmpty(login_name, 'Please, enter the Login Name.'))
		{
			return false;
		}
		if(!IsEmpty(password, 'Please, enter the Password.'))
		{
			return false;
		}

		/*if(!IsEmpty(security_answer, 'Please, enter the Security Answer.'))
		{
			return false;
		}		
		if(newsletter.checked)
		{
			if(!IsEmpty(email_name, 'Please Enter Email.'))
			{
				return false;    
			}
			if(!IsEmail(email_name, 'Please Enter Valid Email.'))
			{
				return false;
			}
		}*/
		Submit.value='Login';
		submit();
		return true;
    }
}
//====================================================================================================
//	Function Name	:	Show_Click()
//----------------------------------------------------------------------------------------------------
function Show_Click(User_Auth_Id)
{
	with(document.frmUser)
	{
		user_auth_id.value = User_Auth_Id;
		Action.value = "Show";
		submit();
	}
}
//====================================================================================================
//	Function Name	:	Add_Click()
//----------------------------------------------------------------------------------------------------
function Add_Click()
{
	with(document.frmUser)
	{
		Action.value = "Add";
		submit();
	}
}
//====================================================================================================
//	Function Name	:	Edit_Click()
//----------------------------------------------------------------------------------------------------
function Edit_Click(User_Auth_Id)
{
	with(document.frmUser)
	{
		Action.value = "Update";
		user_auth_id.value = User_Auth_Id;		
		submit();
	}
}
//====================================================================================================
//	Function Name	:	Delete_Click()
//----------------------------------------------------------------------------------------------------
function Delete_Click(User_Auth_Id)
{
	with(document.frmUser)
	{
		if(confirm('Are you sure you want to delete this user?'))
		{	
			user_auth_id.value = User_Auth_Id;
			Action.value = "Delete";
			submit();
		}
	}
}
//====================================================================================================
//	Function Name	:	Print_Click()
//----------------------------------------------------------------------------------------------------

function Print_Click(User_Auth_Id)
{
	with(document.frmUser)
	{
		popupWindowURL("user.php?Action=Print&user_auth_id="+User_Auth_Id, 'PageContent', 650, 500, false, false, true);
	}
}

//====================================================================================================
//	Function Name	:	Update_Click()
//----------------------------------------------------------------------------------------------------

function Update_Click(User_Login_Id,ApprovalStatus)
{
	with(document.frmUser)
	{
		auth_login_id.value = User_Login_Id;
		approval_status.value = ApprovalStatus;
		Action.value = "Modify";
		submit();
	}
}

//====================================================================================================
//	Function Name	:	DeleteChecked_Click()
//----------------------------------------------------------------------------------------------------
function DeleteChecked_Click()
{
	with(document.frmUser)
	{
		var flg=false;

		if(document.all['user_list[]'].length)
		{
			for(i=0; i < document.all['user_list[]'].length; i++)
			{
				if(document.all['user_list[]'][i].checked)
					flg = true;
			}
		}
		else
		{
			if(document.all['user_list[]'].checked)
				flg = true;
		}
		if(!flg)
		{
			alert('Please, select the User you want to delete.');
			return false;
		}
			
		if(confirm('Are you sure you want to delete selected Users?'))
		{
			Action.value = 'DeleteSelected';
			submit();
		}
	}
}

function calendarCallback(date, month, year)
{
	//date = date + '/' + month + '/' + year;
	if(month <= 9)
	{ month = '0'+month }
	if(date <= 9)
	{ date = '0'+date }
	
	date =  year +'-' + month + '-' + date ;
	document.all.reg_date.value = date;	// name of the form itself is forms[0]
	document.all.reg_date.focus();
}
