
function CheckCategorySelection(FormName,FieldName)
{
	var elementLength,isChecked;
	isChecked = false;
	elementLength = eval('document.'+FormName+'.elements.length');
	for(var i=0; i<elementLength; i++)
	{
		if(document.formmain.elements[i].type == 'checkbox' && document.formmain.elements[i].name == FieldName && document.formmain.elements[i].checked)
		{
			isChecked = true;
			break;
		}
	}
	if(!isChecked)
	{
		alert('Please select atleast one list');
		return false;
	}  
	return true;	
}

function CheckDeleteSelection(FormName,FieldName)
{
	var elementLength,isChecked;
	isChecked = false;
	elementLength = eval('document.'+FormName+'.elements.length');
	for(var i=0; i<elementLength; i++)
	{
		tt1=eval('document.'+FormName+'.elements[i].type');
		tt2=eval('document.'+FormName+'.elements[i].name');
		tt3=eval('document.'+FormName+'.elements[i].checked');
		
		if(tt1 == 'checkbox' && tt2 == FieldName && tt3)
		{
			isChecked = true;
			break;
		}
	}
	if(!isChecked)
	{
		alert('Please select atleast one checkbox');
		return false;
	}  
	return true;	
}

function popuppage(pgname)
{
	window.open(pgname, 'PopupWindow', 'width=200,height=200,scrollbars=no,resizable=no');
}

function Trim(str) 
{
	return str.replace(/\s/g,"");
}

function isNumeric(frm, num)
{	
	myfld = eval("frm."+num)
	if(myfld.value.search(/[^0-9]/) != -1) // only number checking
	{
		alert('The field must contain number only');
		myfld.value="";
		myfld.focus();
		return false;			
	}
	return true;
}


function isPrice(frm, num)
{	
	myfld = eval("frm."+num)
	
	if(!isNaN(myfld.value))
	{
		alert('The field must contain Valid Amount');
		myfld.value="";
		myfld.focus();
		return false;			
	}
	return true;
}



function isRate(obj)
{	
	if((obj.value.search(/[^0-9.]/) != -1) || (dotCheck(obj.value) > 1))  
	{
		return false;			
	}
	return true;
}

function dotCheck(val)
{
	count = 0;
	for(dot=0; dot<val.length; dot++)
	{
		if(val.substring(dot,dot+1) == '.') 
			count++;
	}
	return count;
}

function dateformat(obj)
{
	var f = eval(obj); 
	var f_val = f.value;
	ind1 = f_val.charAt(2);
	ind2 = f_val.charAt(5);
	if(f_val!= "" && ind1!="-" && ind2!="-")
	{
		if(f_val.length < 8)
		{
			alert("Date must have at least 8 digits (MMDDYYYY).")	
			f.focus();
		}
		else
			f.value = f_val.substring(0,2) +"-"+ f_val.substring(2,4) +"-"+ f_val.substring(4,f_val.lenght);
	}	
}

/*	Just pass the object	*/

function spacevalidation(obj)
{
	if(Trim(obj.value)=="")
	{
		return false;
	}
	return true;
}


function delYesNo1()
{
	if(!confirm("Are you sure you want to delete?")) return false;
	return true;
}

function delYesNo2(frmObj)
{
	if(!confirm("Are you sure you want to delete?")) return false;
	else
	{
		frmObj.submit();
		return false;
	}
}

function subForm(frmObj)
{
	if(!confirm("Are you sure?")) return false;
	else
	{
		frmObj.submit();
		return false;
	}
}

function delYesNo3(frmObj)
{
	if(confirm("Are you sure?"))
	{
		frmObj.upflg.value=2;
		frmObj.submit();
	}
	else	return false;	
	return true;	
}

function winOpen(file,width,height,top,left,scrollbars,resizable,queryString)
{
	window.open(''+file+''+queryString,'','width='+width+',height='+height+',top='+top+',left='+left+',resizable='+resizable+',scrollbars='+scrollbars);
}

function Check_Clear_All(frmObj,Field,Toggle) //function for checking all checkbox & clearing all checkbox of delete column
{
	if(frmObj.elements.length > 1)
	{
		for(I=0; I<frmObj.elements.length; I++)
			frmObj.elements[I].checked = Toggle;
	}
	else frmObj.elements[0].checked = Toggle;
}

function isUsername(obj)
{	
	if(obj.value.search(/[^0-9^a-z^A-Z]/) != -1)  
	{
		alert('Value should be alphanumeric.');
		obj.value="";
		obj.focus();
		return false;			
	}
	return true;
}

function dotCheck(val)
{
	count = 0;
	for(dot=0; dot<val.length; dot++)
	{
		if(val.substring(dot,dot+1) == '.')  count++;
	}
	return count;
}

function updownlist(direction,frmObj,frmField)
{
	updown = eval("window.document."+frmObj+"."+frmField);	
   	selectedindex = updown.selectedIndex;
	
	if( selectedindex == -1 )return;
		
	//if deadline reached then return
	if( ( direction == 1 ) && ( selectedindex == ( updown.length - 1 ) ) )return;
	
	//if deadline reached then return
	if( ( direction == -1 ) && ( selectedindex == 0 ) )return;
		
	text = updown[selectedindex].text;
	value = updown[selectedindex].value;
		
	updown[selectedindex].text  = updown[ selectedindex + direction ].text ;
	updown[selectedindex].value = updown[ selectedindex + direction ].value ;
	
	updown[ selectedindex + direction ].text = text ;
	updown[ selectedindex + direction ].value = value ;

	updown[ selectedindex + direction ].selected = true;
}

function righttrn(frmObj,LeftField,RightField)
{
	right = eval("document."+frmObj+"."+LeftField);
	left = eval("document."+frmObj+"."+RightField);
	if(right.selectedIndex == -1 )return;		
	left.options.length=left.options.length+1;		
	selectedindex = right.selectedIndex;
	if( selectedindex == -1 )return;
	left.options[left.options.length-1].text=right[selectedindex].text;
	left.options[left.options.length-1].value=right[selectedindex].value;
	for(var i=selectedindex; i<right.options.length-1; i++)
	{
		right.options[i].text=right.options[i+1].text;
		right.options[i].value=right.options[i+1].value;
	}
	right.options.length=right.options.length-1;		
}

function lefttrn(frmObj,LeftField,RightField)
{		
	right = eval("document."+frmObj+"."+LeftField);
	left = eval("document."+frmObj+"."+RightField);
	if(left.selectedIndex == -1 )return;
	right.options.length=right.options.length+1;		
	selectedindex = left.selectedIndex;
	if( selectedindex == -1 )return;
	right.options[right.options.length-1].text=left[selectedindex].text;
	right.options[right.options.length-1].value=left[selectedindex].value;
	for(var i=selectedindex; i<left.options.length-1; i++)
	{
		left.options[i].text=left.options[i+1].text;
		left.options[i].value=left.options[i+1].value;
	}
	left.options.length=left.options.length-1;
}

// check file extension 
function extCheck(fileObj)
{
	file_ext = fileObj.value.substring(fileObj.value.lastIndexOf('.'),fileObj.value.length);
	file_ext = file_ext.toLowerCase();
	if(file_ext == ".gif" || file_ext == ".jpg" || file_ext == ".jpeg" || file_ext==".jpe" || file_ext==".pjpeg") 
	{}
	else
	{
		alert("File Extension '"+file_ext+"' is not allowed");
		fileObj.focus();
		return false;		
	}
	return true;
}

function isDate(Month,Day,Year) // MM DD YYYY
{
	// Dhaval Dave : Few lines are commented by me after consulting with Rakeshbhai.
	
	var month_array = new Array("January","February","March","April","May","June","July","Augest","September","October","November","December");
	if(Month == 2)
	{
		if(Day>28 && (Year%4) >0)
		{
//			alert("February of " +Year+ "  has only 28 days");			
			return false;
		}       
		else if(Day>29)               
		{
//			alert("February of " +Year+ "  has only 29 days");			
			return false;	
		}
	}
	if((Month==4 || Month==6 || Month==9 || Month==11) && Day==31)
	{
//		alert(month_array[Month-1]+" has only 30 days");		
		return false;		
	}
	return true;
}

function blankOptField(obj) //for drop-box
{
	if(obj.options.selectedIndex==0)
	{
	  alert("Field should be selected.");
	  obj.focus();
	  return false;
	}
	return true;
}
//count chars. being entered in the field
function calcCharLeft(obj1,obj2,maxLength)
{
	if(obj1.value.length > maxLength)
	{
		obj1.value = obj1.value.substring(0,maxLength);
		charleft = 0;
	}
	else
	{
		charleft = maxLength - obj1.value.length;
	}
	if(obj2 != '') obj2.value = charleft;
}


function validateEmail(fld)
{
	var my=fld.value;
	var attherate=my.indexOf("@");
	var lastattherate = my.lastIndexOf("@")
	var dotpos=my.lastIndexOf(".");
	var posspace = my.indexOf(" ");
	var totallen = my.length;
	
	if (attherate<=0 || dotpos<=0 || attherate > dotpos || (dotpos-attherate)<=1 || (dotpos == totallen-1) || posspace > -1 || attherate!=lastattherate)
		return false;
	else
		return true;
}	
function validateForm(frm,arr_compulsoryfields,arr_friendlynames)
{
	for(i=0;i<arr_compulsoryfields.length;i++)
	{
		myfld = eval("frm."+arr_compulsoryfields[i])
		
		if(isTrim(myfld.value) == "")
		{
			alert(arr_friendlynames[i]+" is not entered. Please enter "  + arr_friendlynames[i] + ".");			
			myfld.focus();
			return false;
		}		
	}
	return true;
}
function Numeric_check(myfld)
{
		if(isNaN(myfld.value))
		{
			alert("Only Numeric value allowed");	
			myfld.value="";
			myfld.focus();
			return false;
		}		
	return true;
}


/*	FUNCTION TO CHECK IMAGE EXTATION	

	required to parameter
	
	form 	:	name of the form or "this.form"
	file	:	value of browser box	"this.form.uploadfile"

*/

function checkImage(form, file) 
{
	file_name=file;	
	file=file.value;	
	
	if (file.length==0) {
		alert ("Please, select file");
		file_name.focus();
		return false;
	}
	
	extArray = new Array(".gif", ".jpg", ".jpeg");
	
	allowSubmit = false;
	if (!file) return;
	while (file.indexOf("\\") != -1)
	file = file.slice(file.indexOf("\\") + 1);
	ext = file.slice(file.lastIndexOf(".")).toLowerCase();	
	for (var i = 0; i < extArray.length; i++) 
	{
		if (extArray[i] == ext) { allowSubmit = true; break; }
	}
	if (allowSubmit) {
		return true;
	}
	else
	{
		alert("Please only upload files that end in types:  " 
		+ (extArray.join("  ")) + "\nPlease select a new "
		+ "file to upload and upload again.");
		file_name.focus();
		return false;
	}
}


/*	FUNCTION TO CHECK AUDIO EXTATION	

	required to parameter
	
	form 	:	name of the form or "this.form"
	file	:	value of browser box	"this.form.uploadfile"

*/

function checkAudio(form, file) 
{
	file_name=file;	
	file=file.value;	
	
	if (file.length==0) {
		alert ("Please, select file");
		file_name.focus();
		return false;
	}
	
	extArray = new Array(".mp3", ".wav", ".wma", ".au", ".sam", ".smp", ".mp2", ".ram", ".rm");
	
	allowSubmit = false;
	if (!file) return;
	while (file.indexOf("\\") != -1)
	file = file.slice(file.indexOf("\\") + 1);
	ext = file.slice(file.lastIndexOf(".")).toLowerCase();	
	for (var i = 0; i < extArray.length; i++) 
	{
		if (extArray[i] == ext) { allowSubmit = true; break; }
	}
	if (allowSubmit) {
		
		return true;
	}
	else
	{
		alert("Please only upload files that end in types:  " 
		+ (extArray.join("  ")) + "\nPlease select a new "
		+ "file to upload and upload again.");
		file_name.focus();
		return false;
	}
}

/*	FUNCTION TO CHECK VIDEO EXTATION	

	required to parameter
	
	form 	:	name of the form or "this.form"
	file	:	value of browser box	"this.form.uploadfile"

*/

function checkVideo(form, file) 
{
	file_name=file;	
	file=file.value;
	
	if (file.length==0) {
		alert ("Please, select file");
		file_name.focus();
		return false;
	}
	
	extArray = new Array(".mp2", ".mpeg", ".dat", ".mov", ".avi", ".ram", ".rm", ".3gp");
	
	allowSubmit = false;
	if (!file) return;
	while (file.indexOf("\\") != -1)
	file = file.slice(file.indexOf("\\") + 1);
	ext = file.slice(file.lastIndexOf(".")).toLowerCase();	
	for (var i = 0; i < extArray.length; i++) 
	{
		if (extArray[i] == ext) { allowSubmit = true; break; }
	}
	if (allowSubmit) {
		return true;
	}
	else
	{
		alert("Please only upload files that end in types:  " 
		+ (extArray.join("  ")) + "\nPlease select a new "
		+ "file to upload and upload again.");
		file_name.focus();
		return false;
	}
}

// Dhaval Dave : 31 May 2005. Client side validation for checking of min length.
// e.g. validate_length (frm.zip, 5, "Postal Code")
function validate_length(obj, minlen, objname)
{
	if (obj.value.length < minlen)
	{
		alert (objname + " should be atleast " + minlen + " character long");
		obj.focus();
		return false;
	}
	return true;
}

function validate_range(obj, minlen, maxlen, objname)
{
	
	if (obj.value.length < minlen || obj.value.length > maxlen)
	{
		alert (objname + " should minimum of " + minlen + " and maximum of " + maxlen + " character long");
		obj.focus();
		return false;
	}
	return true;
}