// JavaScript Document


// JavaScript Document
function makeObject(){
	var x; 
	var browser = navigator.appName; 
	//detect the client browser
	if(browser == "Microsoft Internet Explorer"){
		x = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else{
		x = new XMLHttpRequest();
	}
	
	return x;
}
	//call the function makeObject()
		//call the function makeObject()	
	//call the function makeObject()
	var request = makeObject();
var md="";
var popupwindow;

function isValid() {
	
 var un=document.getElementById('un').value;
  var pw=document.getElementById('pw').value;
  
  var msg='Please Enter ';
  var stat=1;
 if (un==''){
	document.getElementById('un').className='error'; 
	msg=msg + 'username ';
	var stat=0;
	
 // 	 
 }
if (pw==''){
	document.getElementById('pw').className='error'; 
	if(un==''){msg= msg + '&';}
	msg= msg + ' password';
	var stat=0;
	
 // return false;	 
 }
 
 if(stat==1) {
	
	 // document.getElementById('frm_login').action='users/process.php?val=login'; 
	 document.frm_login.submit;
 }
 else {
	 document.getElementById('error').innerHTML=msg; 
	 document.getElementById('error').style.display='inline'; 
   return false;
 }
}



function details(ls,ne,mode){
	
	if((ls=="edit_user")){popupwindow=window.open('edit_user.php?val='+ne+'&cat='+mode+'&mode='+ls,'popup','status=yes,height=750,width=450,scrollbars=yes');}
	
	if((ls=="edit_password")){popupwindow=window.open('edit_password.php?val='+ne+'&cat='+mode+'&mode='+ls,'popup','status=yes,height=250,width=400,scrollbars=yes');}

if((ls=="edit_picture")){popupwindow=window.open('edit_image.php?val='+ne+'&cat='+mode+'&mode='+ls,'popup','status=yes,height=300,width=450,scrollbars=yes');}
	popupwindow.focus();
}
function delete_comment(id){
	
	if(confirm("Are you sure you want to delete this comment ?")){

		request.open('get', 'process.php?val=delete_comment&id='+id);
	     	request.onreadystatechange = delete_fetch; 
			request.send('');	
			
	}
		
}
function delete_fan(id){
	
	if(confirm("Are you sure you want to delete this from your favorits list ?")){

		request.open('get', 'process.php?val=delete_fan&id='+id);
	     	request.onreadystatechange = delete_fetch; 
			request.send('');	
			
	}
		
}
	
function delete_fetch(){
	if (request.readyState == 1) {//alert(responseText);
		
		
	}
	if (request.readyState == 4) {

    	if (request.status == 200) {

				if(request.responseText!=""){
					alert(request.responseText);
				}
		}
		else {
    		alert('There was a problem with the request.');
		}
	}		
}
function contact_submit(){
	//alert("ffffffffff");
if((chk_empty_new('fname',"please Enter Your name",'name_error'))&&(checkEmailNew('email',"please Enter Your email",'email_error'))){
	//alert('function');
	document.send_contact.action="contact-sinhala-cinema-films.php?val=send_contact";
	document.send_contact.submit();
	return true;
	
}
	else{return false;}	

}
function contact_advertise(){
	//alert("ffffffffff");
if((chk_empty_new('fname',"please Enter Your name",'name_error'))&&(checkEmailNew('email',"please Enter Your email",'email_error'))){
	//alert('function');
	document.send_advertise.action="sinhala-cinema-advertising.php?val=send_advertise";
	document.send_advertise.submit();
	return true;
	
}
	else{return false;}	

}

function clear_me(bx,ax,er){
		document.getElementById(bx).className=er; 
		document.getElementById(ax).innerHTML=''; 
		document.getElementById(bx).focus();
return true;	
}

function chk_empty_new(bx,ax,er){
	if(document.getElementById(bx).value==""){
		document.getElementById(bx).className='error2'; 
		document.getElementById(er).innerHTML=ax; 
		document.getElementById(bx).focus();
		return false;
	}
	else{return true;}	
}

function checkEmailNew(bx,ax,er) {
   if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById(bx).value)){
   return (true);
   }
	document.getElementById(bx).className='error2'; 
		document.getElementById(er).innerHTML=ax; 
		document.getElementById(bx).focus();
	return (false);
}
function chk_pass(pass,user){
	// alert(user);
	//if(confirm("Are you sure you want to delete this from your favorits list ?")){
		 var pass=pass.value;
         var user= document.getElementById('user').value;
		  //alert(user);
		
		request.open('get', 'process.php?val=get_password&user='+user+'&pass='+pass);
	     	request.onreadystatechange = get_pass; 
			request.send('');	
			
	//}
		
}
function get_pass(){
	if (request.readyState == 1) {//alert(responseText);
	 document.getElementById('msg').innerHTML="Loading.."; 
	}
	if (request.readyState == 4) {
    	if (request.status == 200) {
				if(request.responseText!=""){
					 document.getElementById('msg').innerHTML=request.responseText; 
					return false;
				}else {
					document.getElementById('msg').innerHTML="Correct Password"; 
					return true;
				}		
		}
		else {
    		alert('There was a problem with the request.');
		}
	}		
}

//***************************Users***********************//
function change_pass(){
	//alert("ffffffffff");
if((chk_empty_mgs(document.edit.pass_now,"Current Password"))&&(checkPw(document.edit.pass_new,document.edit.pass_new2))){
	//alert('function');
	document.edit.action="process.php?val=EDIT_PASSWORD";
	document.edit.submit();
	return true;
	
}
	else{return false;}	

}
function edit_users(){
	//alert("ffffffffff");
if((chk_empty_mgs(document.edit.firstname,'First name'))&&(checkEmail(document.edit.email))){
	//alert (document.edit.register_sms.checked);
	 var all2=1;
		if(document.edit.register_sms.checked==true){
			if(document.edit.register_mobile.value==""){
				 all2=0;
				 alert("Please Enter Mobile Number If you want to receive SMS alerts");    
				 return false;
			}	
		}
		
	if(all2!=0){
	document.edit.action="process.php?val=EDIT_USER";
	document.edit.submit();
	return true;
	}
}
	else{return false;}	

}

function edit_image(){

if((chk_empty_mgs(document.edit.myimage,'Profile Image'))){
		
		var all_jpg=1;
		var img1_emty=1;
			var name='myimage';
				if (document.getElementById(name).value==""){
					img1_emty=0;
			    }
			
				if (document.getElementById(name).value!=""){
						  var filename = document.getElementById(name).value;
						  var fileext = filename.substring(filename.lastIndexOf('.')+1);
												
							var b = String(fileext).toUpperCase();
						 if((b != 'JPG')&&(b != 'JPEG')){
							  all_jpg=0;
							alert("You can only upload .jpg & .jpeg images , Image \'"+name+"\' is invalid ");                 	   
							return false; 
							}
			   } 
			if(all_jpg!=0){	
			document.edit.action="process.php?val=EDIT_IMAGE";
			document.edit.submit();
			return true;
			}
	}
	else{return false;}	
}



function add_users(){
	//alert("ffffffffff");
if((chk_empty(document.register.register_firstname))&&(checkEmail(document.register.register_email))&&(checkFormPw(document.register.register_person,document.register.register_log,document.register.register_logre))&&(chk_empty_mgs(document.register.image_name,document.register.message))&&(is_agree(document.register.terms))){
	
	 var all2=1;
		if(document.register.register_sms.checked==true){
			if(document.register.register_mobile.value==""){
				 all2=0;
				 alert("Please Enter Mobile Number If you want to receive SMS alerts");    
				 return false;
			}	
		}
	if(all2!=0){
	document.register.action="process.php?val=ADD_USER";
	document.register.submit();
	return true;
	}
}
	else{return false;}	

}


function fogot_pass(){
	//alert("ffffffffff");
if((chk_empty_both(document.pass.un,document.pass.email,"Enter username or Email"))){

	document.pass.action="process.php?val=FOGOT_PASS";
	document.pass.submit();
	return true;
	}

	else{return false;}	

}


function chk_empty_both(bx,ax,msg){
	if ((bx.value=="")&&(ax.value=="")){
		alert(msg);
        //alert(ax);
		bx.focus();
		return false;
	}
	else{return true;}	
}

//-------------------------------------Security_image--------------------------------
function check_securitycodeagency(bx){
	request.open('get', 'process.php?val=chk_securitycodeuser&user='+bx.value);
	request.onreadystatechange = chk_duplicatecodeagency; 
	request.send('');	
}

function chk_duplicatecodeagency(){
	//alert(request.responseText);
	if (request.readyState == 1) {
		document.all.aj_msg2.innerHTML="Loading.....";
		document.all.aj_button.disabled="Disabled";
	}
	if (request.readyState == 4) {
		request.responseText!=""
		document.all.aj_msg2.innerHTML="&nbsp;";
		document.all.aj_button.disabled="Disabled";
    	if (request.status == 200) {
				if(request.responseText!=""){
					document.all.aj_button.disabled=false;
					document.all.aj_msg2.innerHTML=request.responseText;
				}
				else{
					document.all.aj_msg2.innerHTML="You haven't entered the correct code yet..";
				}
		}
		else {
    		alert('There was a problem with the request.');
		}
	}		
}	

function check_duplicate_un_front(bx){
	request.open('get','process.php?val=chk_un&prsn='+bx.value);
	request.onreadystatechange = chk_duplicate; 
	request.send('');	
}

function chk_duplicate(){
	if (request.readyState == 1) {
		document.all.aj_msg.innerHTML="Loading.....";
		document.all.aj_button.disabled="Disabled";
		
	}
	if (request.readyState == 4) {
		document.all.aj_msg.innerHTML="&nbsp;";
		document.all.aj_button.disabled=false;
    	if (request.status == 200) {
			if(request.responseText!=""){
					document.activeElement.select();
					document.all.aj_button.disabled="Disabled";
					alert(request.responseText);
				}
				
				
		}
		else {
    		alert('There was a problem with the request.');
		}
	}		
}	
function checkEmail(cho) {
   if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(cho.value)){
   return (true);
   }
	alert("Invalid E-mail Address! Please re-enter.");
	cho.focus();
	return (false);
}
function chk_empty(bx){
	if((bx.value=="")||(bx.value==" ")){
		var nm=bx.name.split("_");
		var b_nm=nm[1].toUpperCase();
		alert("The field \'"+b_nm+"\' cannot be empty. Please enter a valid value");
		bx.focus();
		return false;
	}
	else{return true;}	
}
function chk_empty_mgs(bx,ax){
	if(bx.value==""){
		alert("The field \'"+ax+"\' cannot be empty. Please enter a valid value");
        //alert(ax);
		bx.focus();
		return false;
	}
	else{return true;}	
}
function is_selected(cbo){
	if(cbo.value=="EMPTY"){
		var nm=cbo.name.split("_");
		var b_nm=nm[1].toUpperCase();
		alert("The field \'"+b_nm+"\' cannot be empty. Please Select one from drop down");
		cbo.focus();
		return false;
	}
	else{return true;}	
}
function is_select_mgs(bx,ax){
	//alert(bx.value);
	if(bx.value=="EMPTY"){
		alert(ax.value);
		bx.focus();
		return false;
	}
	else{return true;}	
}
function is_agree(cbo){
	if(cbo.checked==false){
		
		alert("Agree to terms and conditions");
		cbo.focus();
		return false;
	}
	else{return true;}	
}
function is_chk(ax,bx){
	if(ax.checked==false){
		alert(bx.value);
		ax.focus();
		return false;
	}
	else{return true;}	
}
function refresh_main_page(){
	opener.location.reload();
	return true;
	
}
function radio_button_checker(ax,bx)
{
// set var radio_choice to false
var radio_choice = false;

// Loop from zero to the one minus the number of radio button selections
	for (counter = 0; counter < ax.length; counter++)
	{
	// If a radio button has been selected it will return true
	// (If not it will return false)
	if (ax[counter].checked)
	radio_choice = true; 
	}
	
	if (!radio_choice)
	{
	// If there were no selections made display an alert box 
	alert(bx.value)
	return (false);
	}
	return (true);
}

function checkPw(password,repassword)
  {
   

    if(password.value != "" && password.value == repassword.value) {
      if(password.value.length < 6) {
        alert("Error: Password must contain at least six characters!");
        password.focus();
        return false;
      }
    
    } else {
      alert("Error: Please check that you've entered and confirmed your NEW password!");
      password.focus();
      return false;
    }

    //alert("You entered a valid password: " + addemployer.addemployer_password.value);
    return true;
  }


function checkFormPw(username,password,repassword)
  {
    if(username.value == "") {
      alert("Error: Username cannot be blank!");
      adduser.adduser_username.focus();
      return false;
    }
    re = /^\w+$/;
    if(!re.test(username.value)) {
      alert("Error: Username must contain only letters, numbers and underscores!");
     adduser.adduser_username.focus();
      return false;
    }
	if(username.value.length < 6) {
        alert("Error: Username must contain at least six characters!");
        adduser.adduser_username.focus();
        return false;
      }

    if(password.value != "" && password.value == repassword.value) {
      if(password.value.length < 6) {
        alert("Error: Password must contain at least six characters!");
        password.focus();
        return false;
      }
      if(password.value == username.value) {
        alert("Error: Password must be different from Username!");
        password.focus();
        return false;
      }
    
    } else {
      alert("Error: Please check that you've entered and confirmed your password!");
      password.focus();
      return false;
    }

    //alert("You entered a valid password: " + addemployer.addemployer_password.value);
    return true;
  }

