// JavaScript Document
function confirmDelete(strType)
{	
	var del;
	del = confirm("Are you sure you want to delete " + strType + "(s)?");
	if(del == true)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function validate_multiple_checkbox(objField)
{
	
	if (typeof objField.length != 'undefined')
	{
		intNumEntries = objField.length;
		for (i = 0; i < intNumEntries; i++)
		{
			if (objField[i].checked == true)
			{
				return true;
			}
		}
	}
	else
	{
		if (objField.checked == true)
		{
			return true;
		}
	}
	return false;
	
}


function confirm_multiple_delete(objForm, objField)
{		
	if (validate_multiple_checkbox(objField))
	{
		if (confirm('Are you sure you want to delete the selected items?') == 1)
		{
			objForm.mode.value = 'del';
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		alert('Please choose the items that you want to delete!');
		return false;
	}
	return false;
}

function confirm_multiple_checkbox(objForm, objField, strOperation)
{	
	blnOk = false;	
	if (validate_multiple_checkbox(objField))
	{		
		if (confirm('Are you sure you want to '+ strOperation +' the selected items?') == 1)
		{
			objForm.mode.value = strOperation;
			blnOk =  true;
		}		
	}
	else
	{
		alert('Please choose the items that you want to '+ strOperation +'!');
	}
	return blnOk;
}

/* Validate Forms */
function validate_text(objField, this_value, this_name) 
{
	if (this_value == '') 
	{	
		if (objFocus == null || objFocus == '') objFocus = objField;
		return this_name+'\n';
	} 
	else 
	{
		return '';
	}
}

function validate_select(objField, this_value, this_name, this_error_choice) 
{
	if (this_value == this_error_choice) 
	{
		if (objFocus == null || objFocus == '') objFocus = objField;
		return this_name+'\n';
	}
	else 
	{
		return '';
	}
}

function validate_radio(objField, this_name) 
{
	intNum = objField.length;
	for(i = 0; i < intNum; i++)
	{
		if (objField[i].checked == true)	
		{
			return '';
			break;
		}
	}
	if (objFocus == null || objFocus == '') objFocus = objField;
	return this_name+'\n';
}


function validate_select_multiple(objField, intMin, strName) 
{
	intLenOption = objField.length;
	if (intLenOption < intMin)
	{
		return strName + '\n';	
	}	
	else
	{
		return '';
	}
}

function validate_email(this_value) {
	var re = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	if (!re.test(this_value)) {
		return 'Invalid email address \n';
	} else {
		return '';
	}
}

function with_space(strValue)
{
	var i, isWithSpace, strValue, intStrLength, chrToken

	isWithSpace  = false;
	strValue     = strValue.toString()
	intStrLength = strValue.length
	for (i = 0; i < intStrLength; i++) {
		chrToken = strValue.charAt(i)
		if (chrToken == " ") 
		{
			isWithSpace = true
		}
	}
	return isWithSpace
}

function validate_password(strPasswd, strConfirmPasswd) 
{
	strErrorMsg = ''
	if (strPasswd != strConfirmPasswd) 
	{
        strErrorMsg = 'Password doesn\'t match \n'
	}
	else if (strPasswd.length < 4)
	{
		strErrorMsg = 'Password should not be less than 4 characters \n'
	}
	else if (with_space(strPasswd))
	{
		strErrorMsg = 'Password should not be contain spaces \n'
	}
	else
	{
		strErrorMsg = ''
	}
	return strErrorMsg
}

function is_integer(value) {
	var blnInt = true;
	inputStr = value.toString();
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i);			
		if ((oneChar < "0" || oneChar > "9")) {
			blnInt = false;
		}
	}
	return (blnInt);
}

function is_float(value)
{
     var template = "x.ff";
     new_value = value/1;
     if(isNaN(new_value)) 
	 {	
	 	return false
	 }
     if ((pos = value.indexOf(".")) > 0)
     {
          templatepos = template.indexOf(".");
          tmpf = template.length-templatepos-1;
          valf = value.length-pos-1;
          if(valf > tmpf) return false;
          else return true;
     }
     return true;
}

function display_error_msg(strErrorMsg) 
{
	alert('Error on the following field/s: \n' + strErrorMsg);
	if (objFocus != null && objFocus != '') objFocus.focus();
	return true;
}

objFocus = null;

function validate_contact(objForm)
{
	strErrorMsg = ''
	strErrorMsg += validate_text(objForm.txtFirstName, objForm.txtFirstName.value, 'First Name');
	strErrorMsg += validate_text(objForm.txtFirstName, objForm.txtLastName.value, 'Last Name');
	strErrorMsg += validate_email(objForm.txtEmailAddress.value);

	strErrorMsg1 = validate_text(objForm.txtContactNo1, objForm.txtContactNo1.value, 'Contact No.');
	if (strErrorMsg1 != '')
		strErrorMsg += validate_text(objForm.txtContactNo2, objForm.txtContactNo2.value, 'Contact No.');
	else
		strErrorMsg  += strErrorMsg1;

	strErrorMsg += validate_text(objForm.txtMessage, objForm.txtMessage.value, 'Message');
	strErrorMsg += validate_text(objForm.txtSecurityCode, objForm.txtSecurityCode.value, 'Security Code');

	if (strErrorMsg != '') 
	{
		display_error_msg(strErrorMsg);
		return false
	}
	else
	{
		return true
	}	

}

function validate_comments(objForm)
{
	strErrorMsg = ''
	strErrorMsg += validate_text(objForm.txtName, objForm.txtName.value, 'Name');
	if (objForm.txtEmail.value != '') strErrorMsg += validate_email(objForm.txtEmail.value);
	strErrorMsg += validate_text(objForm.txtComments, objForm.txtComments.value, 'Comments');

	if (strErrorMsg != '') 
	{
		display_error_msg(strErrorMsg);
		return false
	}
	else
	{
		return true
	}	

}
