//Javascript back end Document

/*general functions 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)==" ") 
			strVar=strVar.substring(1,strVar.length); 
			while(strVar.charAt(strVar.length-1)==" ") 
			strVar=strVar.substring(0,strVar.length-1); 			
	 }
	 return strVar; 
}
function isNotAlphabets(str){
		for (var i = 0; i < str.length; i++)
		{
				re = / /gi				//Replace the space between words with no space
				str = str.replace(re,"");
			
				var ch = str.substring(i, i + 1);
				
				if((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) 
				{
					return true;
				}
		}
		return false;
}
function isNotNumeric(str){
		for (var i = 0; i < str.length; i++)
		{
				var ch = str.substring(i, i + 1);
				if((ch < '0' || '9' < ch)) 
				{
					if(ch == "-" || ch == ".") continue;
					return true;
				}
		}
		return false;
}

		function isNotPrice(str)
		{
			for (var i = 0; i < str.length; i++)
			{
				var ch = str.substring(i, i + 1);
				if((ch < '0' || '9' < ch)) 
				{
					if(ch == ".") continue;
					return true;
				}
			}
	    return false;
		}

		function isNotContactList(str)
		{
			for (var i = 0; i < str.length; i++)
			{
				var ch = str.substring(i, i + 1);
				if((ch < '0' || '9' < ch)) 
				{
					return true;
				}
			}
	    return false;
		}		
/*To check the Login ID of the User*/

function isNotID(str){
		for (var i = 0; i < str.length; i++)
		{
				re = / /gi				//Replace the space between words with no space
				str = str.replace(re,"");
			
				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;
}

var testresults
function checkemail(str_email)
{
	var str = str_email;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str))
	testresults=true
	else
	{
		alert("Please input a valid email address!")
		testresults=false
	}
return (testresults)
}

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;
	  }

}
//Check All permissions
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;
	  }

}

//Check user registration page..
function check_register(chk)
{
		
		  var uname=trim(chk.username.value);
		  var len=chk.username.value.length;
		  if(uname == "")
		 {
			alert("Please enter User Name");
			chk.username.focus();
			return false;
		 }
		 
		  if(len<4 || len>15)
		 {
			alert("user names must have 4-15 characters");
			chk.username.focus();
			return false;
		 }
		
		 var email=trim(chk.txtEmail.value);
		 if(email == "")
		 {
			alert("Please enter User Email Address");
			chk.txtEmail.focus();
			return false;
		 }
		 if(checkemail(email) == false)
		 {
			return false;		  
		 }

		/*var name= trim(chk.txtFirstName.value);
		if(name == "")
		 {
			alert("Please enter User First Name.");
			chk.txtFirstName.focus();
			return false;
		}
		if(isNotAlphabets(name))
		 {
			alert("Invalid Characters in First Name.");
			chk.txtFirstName.focus();
			return false;		  
		 }
		 
		var name=trim(chk.txtLastName.value);
		if(name == "")
		 {
			alert("Please enter User's Last Name.");
			chk.txtLastName.focus();
			return false;
		}
		if(isNotAlphabets(name))
		 {
			alert("Invalid Characters in Last Name.");
			chk.txtLastName.focus();
			return false;		  
		 }*/		 				 				 
		
		var pwd=trim(chk.txtPassword.value);
		if(pwd == "")
		 {
			alert("Please enter User's Password.");
			chk.txtPassword.focus();
			return false;
		}
		
		var pwd=trim(chk.txtPassword.value);
		if(pwd.length<6)
		 {
			alert("Password must contain atleast 6 character ");
			chk.txtPassword.focus();
			return false;
		}

		/*var confirmpwd=trim(chk.txtConfirmPassword.value);
		if(confirmpwd !== pwd)
		 {
			alert("Your confirm password is not matching with your password");
			chk.txtConfirmPassword.focus();
			return false;
		}*/

		if(chk.chkTermCondition.checked == false)
		{
			alert("You can not be registered without agree with Terms and Conditions");
			chk.chkTermCondition.focus();
			return false;	
		}	
		
		/*if(chk.rdoSex[0].checked!=true && chk.rdoSex[1].checked!=true)
		{
			alert("Please select your sex");
			chk.rdoSex.focus();
			return false;			
		}*/

		 return true;
    }	

//Check user registration page edit..
function check_register_edit(chk)
{

		var name= trim(chk.txtFirstName.value);
		if(name == "")
		 {
			alert("Please enter User First Name.");
			chk.txtFirstName.focus();
			return false;
		}
		if(isNotAlphabets(name))
		 {
			alert("Invalid Characters in First Name.");
			chk.txtFirstName.focus();
			return false;		  
		 }
		 
		var name=trim(chk.txtLastName.value);
		if(name == "")
		 {
			alert("Please enter User's Last Name.");
			chk.txtLastName.focus();
			return false;
		}
		if(isNotAlphabets(name))
		 {
			alert("Invalid Characters in Last Name.");
			chk.txtLastName.focus();
			return false;		  
		 }		 				 				 
		
	
		if(chk.rdoSex[0].checked!=true && chk.rdoSex[1].checked!=true)
		{
			alert("Please select your sex");
			chk.rdoSex.focus();
			return false;			
		}

		 return true;
    }	


//Check Login Front End
function check_login(chk)
{
	/*txtEmail = trim(chk.txtEmail.value);
	
	if(txtEmail=="")
	{
		alert("Email can not be left blank");
		chk.txtEmail.focus();
		return false;
	}
	
	if(checkemail(txtEmail)== false)
	{
		return false;
	}*/
	
	uname = trim(chk.username.value);
	if(uname=="")
	{
		alert("User Name can not be left blank");
		chk.username.focus();
		return false;
	}

	
	txtPwd = trim(chk.txtPwd.value);
	
	if(txtPwd=="")
	{
		alert("Password can not be left blank");
		chk.txtPwd.focus();
		return false;
	}
	
}	



var pop='';
//Open PopUP Window
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;
}

//Close Window
function closewin()
{
		
	window.close();
}


function check_upload1(chk)
{
      flag=1;
      flag1=1;
      flag2=1;
      txtTitle1 = trim(chk.txtTitle.value);
	if(txtTitle1.length==0)
	{
	
                  document.getElementById('titleid').style.color="red";
		      document.getElementById('Upload1ErrorBox').style.display="block";
		      document.getElementById('Upload1ErrorBox1').style.display="none";
		      document.getElementById('Upload1ErrorBox2').style.display="none";
	           	chk.txtTitle.focus();
		      flag=0;
		      flag1=0;
		      flag2=0;
	}
	else if(txtTitle1.length>0)
	{
	      document.getElementById('Upload1ErrorBox').style.display="none";
            document.getElementById('titleid').style.color="#000000";
            flag1=1;
            flag2=1;
	}

	txtDesc = trim(chk.txtDesc.value);
	if(txtDesc=="")
	{
		document.getElementById('descid').style.color="red";
		if(flag1==1)
		{
		    document.getElementById('Upload1ErrorBox1').style.display="block";
		    document.getElementById('Upload1ErrorBox').style.display="none";
		     document.getElementById('Upload1ErrorBox2').style.display="none";
		}   
		chk.txtDesc.focus();
            flag=0;
            flag1=0;
	}
	else
	{
		document.getElementById('descid').style.color="#000000";
		document.getElementById('Upload1ErrorBox1').style.display="none";
		flag1=1;
	}

	txtTags = trim(chk.txtTags.value);
	if(txtTags=="")
	{
		document.getElementById('tagsid').style.color="red";
		chk.txtTags.focus();
		if(flag1==1 && flag2==1)
		{
		    document.getElementById('Upload1ErrorBox2').style.display="block";
		    document.getElementById('Upload1ErrorBox1').style.display="none";
		    document.getElementById('Upload1ErrorBox').style.display="none";
		}   
            flag=0;
            flag1=0;
	}
	else
	{
		document.getElementById('tagsid').style.color="#000000";
		document.getElementById('Upload1ErrorBox2').style.display="none";
		flag1=1;
	}
	
	var flag2=0;
	var formControls = chk.elements;
	for (var i = 0; i < formControls.length; i++)
        if (formControls[i].type.toLowerCase() == 'radio'&& formControls[i].checked)
	{   
	 	flag2=1;
		document.getElementById('videotxt').style.color="#000000";

	}
	
	if(flag2==0)
	{
		document.getElementById('videotxt').style.color="red";
	     return false;
	}
	/*if(!chk.radio_upload.checked)
	{
		document.getElementById('videocategory').style.color="red";
	      flag=0;
	}
	else
	{
		document.getElementById('videocategory').style.color="#000000";
	}*/
	
	
	if(flag==0)
	{
	     return false;
	}
	else
	{
	    return true;
	}
	
	
}


function check_upload2(chk)
{

      txttxtUpdFile = trim(chk.txtUpdFile.value);
	if(txttxtUpdFile.length==0)
	{
            alert('You must enter a file name!');
            return false;
      }

}
