/* TO CHECK THE USERS EMAIL, ALPHABETS, NAME, PASSWORD, REMOVE SPACES FORM THE NAME THROUGH TRIM() ETC..*/
function trim (strVar) {
	if(strVar.length >0)
	{
		while(strVar.charAt(0)==" ")						//REMOVE LEFT SPACES
			strVar=strVar.substring(1,strVar.length);
		while(strVar.charAt(strVar.length-1)==" ")			//REMOVE RIGHT SPACES
			strVar=strVar.substring(0,strVar.length-1);
	}
	return strVar;
}

/*TO CHECK THE LOGIN ID OF THE USER*/
function isNotID(str){
	for (var i = 0; i < str.length; i++)
	{
		var ch = str.substring(i, i + 1);
		if((ch < '0' || '9' < ch) && ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)))
		{
			if(ch == "_") continue;
			return true;
		}
	}
	return false;
}

function checkLogin(chk)
{
	if(chk.userName.value.length==0)
	{
		alert("User Name can not be left blank");
		chk.userName.focus();
		return false;
	}
	if(chk.password.value.length==0)
	{
		alert("Password can not be left blank");
		chk.password.focus();
		return false;
	}
	return true;
}

/*Used in All Cp Modules*/
function checkAll(checked)
{
	for(var i=0;i<document.removeForm.elements.length;i++)
	{  
		var e = document.removeForm.elements[i];
		if(e.type == "checkbox") e.checked = checked;
	}
}

function checkAllperm(checked)
{
	for(var i=0;i<document.showPermFrm.elements.length;i++)
	{
		var e = document.showPermFrm.elements[i];
		if(e.type == "checkbox") e.checked = checked;
	}
}

function checkAllperm1(checked)
{
	for(var i=0;i<document.permFrm.elements.length;i++)
	{  
		var e = document.permFrm.elements[i];
		if(e.type == "checkbox") e.checked = checked;
	}
}

function checkfrmedit(chk)
{
	if(chk.keyword.value == "")
	{
		alert("Please enter keyword to search");
		chk.keyword.focus();
		return false;
	}

	if(chk.keyword.value.length<3)
	{
		alert("Please enter atleast 3 charaters");
		chk.keyword.focus();
		return false;
	} 
	return true;
}

var pop='';
function openwin(nm,width,height) {
	var name = nm;
	if (pop && !pop.closed) {
		pop.close();
	}

	pop = eval("window.open('"+name+"','NewWIN','chrome[4],toolbar=no,left=5,top=5,width="+width+",height="+height+",directories=no,menubar=no,SCROLLBARS=yes,left=2,right=2')");
	if (!pop.opener) popUpWin.opener = self;
}

function closewin()
{
	window.close();
}

function checkCreateUser(membersForm)
{
	//Members Id validation
	var member = trim(document.membersForm.loginId.value);
	if(member.length == 0)
	{
		alert("Please enter login-ID!");
		document.membersForm.loginId.focus();
		return false;
	}
	var member = document.membersForm.loginId.value;
	if(member.length < 5)
	{
		alert("Login ID should be atleast 5 characters!");
		document.membersForm.loginId.focus();
		return false;
	}
	if(isNotID(member))
	{
		alert("Invalid characters in login-ID!");
		document.membersForm.loginId.focus();
		return false;
	}
	//Password Validation
	var password = trim(document.membersForm.password.value);
	if(password.length == 0)
	{
		alert("Please enter password!");
		document.membersForm.password.focus();
		return false;
	}
	var password = document.membersForm.password.value;
	if(password.length < 6)
	{
		alert("Password should be atleast Six(6) Characters!");
		document.membersForm.password.focus();
		return false;
	}
	if(isNotID(password))
	{
		alert("Invalid characters in password!");
		document.membersForm.password.focus();
		return false;
	}

	//Confirm Password
	var confirm_password = document.membersForm.confirm_password.value;

	if(password != confirm_password)
	{
		alert("Password & confirm password mismatch!");
		document.membersForm.confirm_password.focus();
		return false;
	}
	//First Name
	var fname = trim(document.membersForm.name.value);
	if(fname.length == 0)
	{
		alert("Please enter name!");
		document.membersForm.name.focus();
		return false;
	}

	//Address
	var address_1 = trim(document.membersForm.address_1.value);
	if(address_1.length == 0)
	{
		alert("Please enter address line - 1!");
		document.membersForm.address_1.focus();
		return false;
	}
	//Country		  		  
	var country = document.membersForm.country.value;
	if(country.length == 0)
	{
		alert("Please select users country!");
		document.membersForm.country.focus();
		return false;
	}
	//Email		  
	var email_1 = trim(document.membersForm.email_1.value);
	if(email_1.length>0)
	{
		if(checkEmail(email_1) == false)
		{
			alert("Invalid e-mail address! Please re-enter.");
			document.membersForm.email_1.select();
			return false;		  
		}
	}
	return true;
}

function deleteAlert1()
{
	var records = document.removeForm.records.value;
	var total = 0;
	var msg = "";

	if(records>0)
	{
		for(i=0; i<document.removeForm.length; i++)
		{
			e = document.removeForm.elements[i];
			if (e.type=='checkbox')
			{
				if(eval('e.checked') == true)
				{
					total = total+1;
				}
			}
		}

		if(total >0)
		{
			msg = msg + 'Do you like to remove ' + total +' records ?';
		}
		else
		{
			alert("Please check the items to remove !");
			return false;
		}
		if(msg.length >0)
		{
			var flag;
			flag = confirm(msg + " If Yes Press \"OK\" else Press \"Cancel\".");
			if(flag == true)
				return true;
		}
	}
	return false;
}

function textLimitCheck(thisArea, maxLength, msg)
{
    if (thisArea.value.length > maxLength)
    {
		alert('Restrict your text in '+ maxLength + ' characters');
        thisArea.value = thisArea.value.substring(0, maxLength);
        thisArea.focus();
    }
	document.getElementById(msg).innerText = thisArea.value.length;
}

function setTitle(val, title, desc)
{
	title = document.getElementById(title);
	desc = document.getElementById(desc);
	title.value=val
	desc.value=val;
}

//DATE VALIDATION
function dateValid(dd, mm, yyyy)
{
	if(dd.length==0 || mm.length==0 || yyyy.length==0)
		return false;
	if(isNaN(dd)) 
		return false;
	if(isNaN(mm)) 
		return false;
	if(isNaN(yyyy)) 
		return false;
	if(yyyy.length<4)
	{
		alert("Please Enter Year in four digit!");
		return false;
	}
	if(mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12)
	{
		if(dd>31)
			return false;
	}
	else 
	{
		if(mm==4 || mm==6 || mm==9 || mm==11)
		{
			if(dd>30)
				return false;
		}
		else
		{
			var f_day = isLeap(yyyy);
			if(dd>f_day) return false;
		}
	}
	return true;
}

//CHECK DAY IN FEBRUARY MONTH
function isLeap(year)
{
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

//CHECK DATE DIFFERENCE BETWEEN TO DATES
function dateDiff(dd1, mm1, yy1, dd2, mm2, yy2)
{
	var date1 = yy1+mm1+dd1;
	var date2 = yy2+mm2+dd2;
	if(date1>date2) return false;
	return true;
}

function checkEmail(email) 
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))
	{
		return (true);
	}
	return false;
}

function notValidsite( str )
{
	mailRE = new RegExp( );
	mailRE.compile( '^[A-Za-z]+://[A-Za-z0-9-]+\.[A-Za-z0-9]+', 'gi');
	return !(mailRE.test( str.value ));
}
function checkSearchForm(theForm)
{
	var v1 = theForm.keyword.value;
	var v2 = theForm.cat.value;
	
	if(v1.length==0 && v2.length==0)
	{
		alert("You must fill at least one Field for searching.");
		theForm.keyword.focus();
		return false;
	}
	return true;
}
function checkAnnounce(theForm)
{
	var email_to = theForm['email_to[0]'];
	var email_from = trim(theForm.email_from.value);
	var name_from = trim(theForm.name_from.value);
	email_to = trim(email_to.value);
	var email1 = theForm['email_to[1]'];
	var email2 = theForm['email_to[2]'];
	var email3 = theForm['email_to[3]'];
	var email4 = theForm['email_to[4]'];
	email1 = trim(email1.value);
	email2 = trim(email2.value);
	email3 = trim(email3.value);
	email4 = trim(email4.value);
	var req_imageCode = trim(theForm.req_imageCode.value);
	if(email_to.length==0)
	{
		alert("Please enter your friend's e-mail!");
		document.catForm['email_to[0]'].focus();
		return false;
	}
	if(checkEmail(email_to) == false)
	{
		alert("Invalid e-mail address! Please re-enter.");
		document.catForm['email_to[0]'].select();
		return false;		  
	}
	if(email1.length>0)
	{
		if(checkEmail(email1) == false)
		{
			alert("Invalid e-mail address! Please re-enter.");
			document.catForm['email_to[1]'].select();
			return false;		  
		}
	}
	if(email2.length>0)
	{
		if(checkEmail(email2) == false)
		{
			alert("Invalid e-mail address! Please re-enter.");
			document.catForm['email_to[2]'].select();
			return false;		  
		}
	}
	if(email3.length>0)
	{
		if(checkEmail(email3) == false)
		{
			alert("Invalid e-mail address! Please re-enter.");
			document.catForm['email_to[3]'].select();
			return false;
		}
	}
	if(email4.length>0)
	{
		if(checkEmail(email4) == false)
		{
			alert("Invalid e-mail address! Please re-enter.");
			document.catForm['email_to[4]'].select();
			return false;		  
		}
	}

	if(email_from.length==0)
	{
		alert("Please enter your e-mail!");
		theForm.email_from.focus();
		return false;
	}
	if(checkEmail(email_from) == false)
	{
		alert("Invalid your e-mail! Please re-enter.");
		theForm.email_from.select();
		return false;		  
	}
	if(name_from.length==0)
	{
		alert("Please enter your name!");
		theForm.name_from.focus();
		return false;
	}
	if(req_imageCode.length==0)
	{
		alert("Please enter image code!");
		theForm.req_imageCode.focus();
		return false;
	}
	return true;
}

function checkAlert(theForm)
{
	var name = trim(theForm.name.value);
	var email= trim(theForm.email.value);
	if(name.length==0)
	{
		alert("Please enter your name!");
		theForm.name.focus();
		return false;
	}
	if(email.length==0)
	{
		alert("Please enter your e-mail!");
		theForm.email.focus();
		return false;
	}
	if(checkEmail(email) == false)
	{
		alert("Invalid email! Please re-enter.");
		theForm.email.select();
		return false;
	}
	return true;
}

/*FUNCTION FOR ERROR REPORTING*/
function checkRptForm(theForm)
{
	var issue = trim(theForm.issue.value);
	var name = trim(theForm.name.value);
	var email = trim(theForm.email.value);
	var detail = trim(theForm.detail.value);
	var req_imageCode = trim(theForm.req_imageCode.value);
	
	if(issue.length==0)
	{
		alert("Please select issue!");
		theForm.issue.focus();
		return false;
	}
	if(name.length==0)
	{
		alert("Please enter name!");
		theForm.name.focus();
		return false;
	}
	if(email.length==0)
	{
		alert("Please enter email!");
		theForm.email.focus();
		return false;
	}
	if(checkEmail(email) == false)
	{
		alert("Invalid e-mail address! Please re-enter.");
		theForm.email.select();
		return false;		  
	}

	if(detail.length==0)
	{
		alert("Please enter detail!");
		theForm.detail.focus();
		return false;
	}
	if(req_imageCode.length==0)
	{
		alert("Please enter image code!");
		theForm.req_imageCode.focus();
		return false;
	}
	return true;
}


/* contact form validation*/
function checkContactForm(theForm)
{
	var email_from = trim(theForm.email_from.value);
	var name_from = trim(theForm.name_from.value);
	var country = trim(theForm.country.value);
	var message = trim(theForm.message.value);
	var req_imageCode = trim(theForm.req_imageCode.value);
	var phone = trim(theForm.phone.value);
	if(name_from.length==0)
	{
		alert("Please enter your name!");
		theForm.name_from.focus();
		return false;
	}
	if(email_from.length==0)
	{
		alert("Please enter your e-mail!");
		theForm.email_from.focus();
		return false;
	}
	if(checkEmail(email_from) == false)
	{
		alert("Invalid your e-mail! Please re-enter.");
		theForm.email_from.select();
		return false;		  
	}
	if(phone.length>0)
	{
		if(isNaN(phone))
		{
			alert("Please enter valid phone number!");
			theForm.phone.focus();
			return false;
		}
	}
	if(country.length==0)
	{
		alert("Please enter your country!");
		theForm.country.focus();
		return false;
	}
	if(message.length==0)
	{
		alert("Please enter your name!");
		theForm.message.focus();
		return false;
	}
	if(req_imageCode.length==0)
	{
		alert("Please enter image code!");
		theForm.req_imageCode.focus();
		return false;
	}
	return true;
}
