//Ajax request function
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

//Ajax loader lightbox 
var curr_lb_div;
var is_modal = false;

function ShowLightBox(lb_div, isModal)
{
    document.getElementById(lb_div).style.display='block';
    document.getElementById('fade').style.display='block';
    curr_lb_div = lb_div;
    if (isModal)
        is_modal = true;
    else is_modal = false;
}

function HideLightBox()
{
    if (document.getElementById(curr_lb_div))
    {
         document.getElementById(curr_lb_div).style.display='none';
         document.getElementById('fade').style.display='none';
         curr_lb_div = '';
    }
}


//limit characters in the text area or textboxes
function CountCharactersGeneral(sourceTextBox, displayControl, maxLength) {
	if(sourceTextBox != null && displayControl != null) {
		sourceTextBox = document.getElementById(sourceTextBox);
		displayControl = document.getElementById(displayControl);
		if(sourceTextBox != null)
		{
			var len = sourceTextBox.value.length
			//alert(len); alert(maxLength);
			if (len<=maxLength) {
				rem = parseInt(maxLength) - parseInt(len);
				displayControl.innerHTML = "<span id='success'>"+ rem +" Character(s) remaining.</span>";
			}
			else {
				sourceTextBox.value = sourceTextBox.value.substring(0, maxLength);
				return false;
			}
		}
	}
}

//add class to field
function addClass(element, value) {
	if(!element.className) {
		element.className = value;
	} else {
		newClassName = element.className;
		newClassName+= " ";
		newClassName+= value;
		element.className = newClassName;
	}
}

//remove class to field
function removeClass(element) {
	if(element.className) {
		element.className="";
	}
}

// show error message
function showError(id,msg) {		
		document.getElementById(id).innerHTML = msg;
}

function enableSubmit() {
	var accept = document.getElementById("signup_accept");
	var btnSubmit = document.getElementById("btnFrmSubmit");
	if(accept.checked==true) {
		btnSubmit.disabled=false;
	} else {
		btnSubmit.disabled=true;
	}
}

function funSmtReg() {
	var frm = document.getElementById("frmRegister");
	document.getElementById("hidRegister").value = "Register";
	if(validateSignup()) { frm.submit(); }
}

function validateSignup() {
	var fullName = document.getElementById("full_name");
	var email = document.getElementById("email");
	var pass = document.getElementById("password");
	var cpass = document.getElementById("confirm_password");
	var err=0;
	if(fullName.value=="") {
		alert("Enter your full name");
		fullName.focus();
		err = 1; return false;			
	}
	if(email.value=="") {
		alert("Enter your email address");
		email.focus();
		err = 1; return false;		
	}
	 if(email.value != '')
	 {
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value))
		{
			  var err=0;
		}
		else 
		{
			alert("Invalid E-mail Address! Please re-enter.");				
			email.value="";
			email.focus();
			err = 1; return false;	
		}
	 }
	if(pass.value=="") {
		alert("Enter your password");
		pass.focus();
		err = 1; return false;			
	}
	if(cpass.value=="") {
		alert("Enter confirm password");
		cpass.focus();
		err = 1; return false;			
	}
	if(pass.value!=cpass.value) {
		alert("Both password should be same.");
		cpass.focus();
		err = 1; return false;			
	}
	
	if(err==0) { return true; }
	else { return false; }
}


function getSubcat(val,divId){
	 document.getElementById("loader-div").innerHTML="<img src='images/busyblu.gif'> Please wait..";
	 xmlHttp = GetXmlHttpObject();  //create object for http request
	 if (xmlHttp==null)  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  }	  
	 var url1="get_subcategory.php"
	 url1=url1+"?id="+val	 
	 xmlHttp.open("POST", url1, true);
	 xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	 xmlHttp.send(null);		 
	 xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) { 
		document.getElementById("loader-div").innerHTML="";
		document.getElementById(divId).innerHTML=xmlHttp.responseText;
		}
	 }	 	
}
function getCountry(val1,divId){
	 document.getElementById("spn-country").innerHTML="<img src='images/busyblu.gif'> Please wait..";
	 xmlHttp = GetXmlHttpObject();  //create object for http request
	 if (xmlHttp==null)  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  }	  
	 var url1="get_country.php"
	 url1=url1+"?id="+val1	 
	 xmlHttp.open("POST", url1, true);
	 xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	 xmlHttp.send(null);		 
	 xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) { 
		document.getElementById("spn-country").innerHTML="";
		document.getElementById(divId).innerHTML=xmlHttp.responseText;
		}
	 }	 	
}


function rating(videoId,rate,divId){
	 document.getElementById(divId).innerHTML="<img src='images/busyblu.gif'> Please wait..";
	 xmlHttp = GetXmlHttpObject();  //create object for http request
	 if (xmlHttp==null)  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  }	  
	 var url="rating.php";
	 url=url+"?id="+videoId+"&rate="+rate; 
	 xmlHttp.open("GET", url, true);
	 xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	 xmlHttp.send(null);		 
	 xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) { 		
			//alert(xmlHttp.responseText);
			document.getElementById(divId).innerHTML=xmlHttp.responseText;
			document.getElementById("rating-text").innerHTML="Thanks for rating.";
		}
	 }	 	
}


// upload functions

function funVideoUpload() {
	var frm = document.getElementById("frmVideoUpload");
	document.getElementById("hidVideoUpload").value = "Upload";
	if(validateFrmUpload()) { 
		jQuery('#video_path').uploadifyUpload();		
		//frm.submit(); 
		ShowLightBox('loader'); 
        return false;
	}
}

//ajax get video request informations
function funRequestInfo(val,divId){
	 //alert(val);
	 document.getElementById(divId).innerHTML="<img src='images/busyblu.gif'> Please wait..";
	 xmlHttp = GetXmlHttpObject();  //create object for http request
	 if (xmlHttp==null)  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  }	  
	 var url="get_request_info.php";
	 url=url+"?reg="+val;	 
	 xmlHttp.open("GET", url, true);
	 xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	 xmlHttp.send(null);		 
	 xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) { 		
			document.getElementById(divId).innerHTML=xmlHttp.responseText;
		}
	 }	 	
}

//ajax get video request informations
function funRequestTopic(val,divId){
	 //alert(val);
	 document.getElementById(divId).innerHTML="<img src='images/busyblu.gif'> Please wait..";
	 xmlHttp = GetXmlHttpObject();  //create object for http request
	 if (xmlHttp==null)  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  }	  
	 var url="get_request_topic.php";
	 url=url+"?pby="+val;	 
	 xmlHttp.open("GET", url, true);
	 xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	 xmlHttp.send(null);		 
	 xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) { 		
			document.getElementById(divId).innerHTML=xmlHttp.responseText;
		}
	 }	 	
}

function funToEdit(videoId) {
		window.location = "index.php?my&editvideo="+videoId;
}

function funDelVideo(videoId) {
	
	var del=confirm("Are you sure you want to mark this video for deletion?");
    if(del==true) {
		var frm = document.getElementById("frmMyVideo");
		document.getElementById("hidKeyword").value = "delete";
		document.getElementById("hidRecord").value = videoId;	
		frm.submit(); 
	}
}

function funEditVideo() {
	var frm = document.getElementById("frmEditVideo");
	document.getElementById("hidEditVideo").value = "Edit video";
	if(validateFrmUpload()) { 
		frm.submit(); 		
	}
}

function validateFrmUpload() {
	var home_town=document.getElementById("home_town");
	var region=document.getElementById("primary_region");
	var country=document.getElementById("ajax_country");
	var state=document.getElementById("state");
	var city=document.getElementById("city");
	var pro_cat=document.getElementById("category");	  
	var video_title=document.getElementById("video_title");
	var vsource=document.getElementById("video_path");	  
	var video_description=document.getElementById("video_description");
	//var reqOption=document.getElementById("request_option");
	
	var IsFileSelected=document.getElementById("hidFileSelected");
				
	submitOK="true";
	 /*if (home_town.value=='') {
		 alert("Enter Current Residence");
		 home_town.focus();
		 submitOK="false";
		 return false;
	 }	
	 if (country.value=='') {
		 alert("Select Country");
		 region.focus();
		 submitOK="false";
		 return false;
	 }
	 if (state.value=='') {
		 alert("Enter State");
		 region.focus();
		 submitOK="false";
		 return false;
	 }	
	 /*if (city.value=='') {
		 alert("Enter City");
		 region.focus();
		 submitOK="false";
		 return false;
	 }	
	 if (pro_cat.value=='') {
		 alert("Select Product category");
		 pro_cat.focus();
		 submitOK="false";
		 return false;
	 }	
	 
	 if (video_title.value=='') {
		 alert("Enter video title");
		 video_title.focus();
		 submitOK="false";
		 return false;
	 }	 
	 	 
	 if (video_description.value=='') {
		 alert("Enter video description");
		 video_description.focus();
		 submitOK="false";
		 return false;
	 }*/
     if (region.value=='') {
         alert("Please select the continent the video was taken in.");
         region.focus();
         submitOK="false";
         return false;
     }        
	 
	 if(IsFileSelected.value=="false") {
		 alert("Please select a video file to upload.");
		 vsource.focus();
		 submitOK="false";
		 return false;
	 }
		 
	 if (submitOK=="false")
	 {	 	
		return false;
	 }
	 else { return true; }
}

//ajax get latest category
function funLatestVideo(catId,divId,pageNumber, statusDivId){         
	 //alert(val);
	 document.getElementById(statusDivId).innerHTML="<tr><img src='images/busyblu.gif'> Please wait..</tr>";

	 xmlHttp = GetXmlHttpObject();  //create object for http request
	 if (xmlHttp==null)  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  }	  
	 var url="get_videos.php";
     codeVal = catId*1000000+pageNumber;    //for some reason, multiple query value is not working so having to use a code value
     url=url+"?codeval="+codeVal;

//     alert(url);
	 xmlHttp.open("GET", url, true);
	 xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	 xmlHttp.send(null);		 
	 xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) { 		
			document.getElementById(divId).innerHTML=xmlHttp.responseText;
            document.getElementById(statusDivId).innerHTML="";
		}
	 }	 	
}

//ajax get highest rated videos
function funHighratedVideo(val,divId){
	 //alert(val);
	 document.getElementById(divId).innerHTML="<img src='images/busyblu.gif'> Please wait..";
	 xmlHttp = GetXmlHttpObject();  //create object for http request
	 if (xmlHttp==null)  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  }	  
	 var url="get_highrated_videos.php";
	 url=url+"?cat_id="+val;	 
	 xmlHttp.open("GET", url, true);
	 xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	 xmlHttp.send(null);		 
	 xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4) { 		
			document.getElementById(divId).innerHTML=xmlHttp.responseText;
		}
	 }	 	
}

function funHighratedVideoV2(catId,divId,pageNumber, statusDivId){         
     //alert(val);
     document.getElementById(statusDivId).innerHTML="<tr><img src='images/busyblu.gif'> Please wait..</tr>";

     xmlHttp = GetXmlHttpObject();  //create object for http request
     if (xmlHttp==null)  {
          alert ("Your browser does not support AJAX!");
          return;
      }      
     var url="get_highrated_videos_v2.php";
//     url=url+"?cat_id="+val+"&pagenum="+pageNumber;     
     codeVal = catId*1000000+pageNumber;    //for some reason, multiple query value is not working so having to use a code value
     url=url+"?codeval="+codeVal;

     xmlHttp.open("GET", url, true);
     xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     xmlHttp.send(null);         
     xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState==4) {         
            document.getElementById(divId).innerHTML=xmlHttp.responseText;
            document.getElementById(statusDivId).innerHTML="";
        }
     }         
}

//ajax get most viewed videos
function funMostViewedVideos(val,divId){
     //alert(val);
     document.getElementById(divId).innerHTML="<img src='images/busyblu.gif'> Please wait..";
     xmlHttp = GetXmlHttpObject();  //create object for http request
     if (xmlHttp==null)  {
          alert ("Your browser does not support AJAX!");
          return;
      }      
     var url="get_mostviewed_videos.php";
     url=url+"?cat_id="+val;     
     xmlHttp.open("GET", url, true);
     xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     xmlHttp.send(null);         
     xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState==4) {         
            document.getElementById(divId).innerHTML=xmlHttp.responseText;
        }
     }         
}

function funMostViewedVideosV2(catId,divId,pageNumber, statusDivId){         
     //alert(val);
     document.getElementById(statusDivId).innerHTML="<tr><img src='images/busyblu.gif'> Please wait..</tr>";

     xmlHttp = GetXmlHttpObject();  //create object for http request
     if (xmlHttp==null)  {
          alert ("Your browser does not support AJAX!");
          return;
      }      
     var url="get_mostviewed_videos_v2.php";
//     url=url+"?cat_id="+val+"&pagenum="+pageNumber;     
     codeVal = catId*1000000+pageNumber;    //for some reason, multiple query value is not working so having to use a code value
     url=url+"?codeval="+codeVal;

     xmlHttp.open("GET", url, true);
     xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     xmlHttp.send(null);         
     xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState==4) {         
            document.getElementById(divId).innerHTML=xmlHttp.responseText;
            document.getElementById(statusDivId).innerHTML="";
        }
     }         
}

//travel job functions

//post travel job
function funPostJobs() {
	var frm = document.getElementById("frmPostJobs");
	document.getElementById("hidPostJobs").value = "Post jobs";	
	if(valPostJobs()) {
		frm.submit();	
		ShowLightBox('loader'); return false;		
	}
}

//edit travel job
function funEditJobs() {
	var frm = document.getElementById("frmEditJobs");
	document.getElementById("hidEditJobs").value = "edit jobs";	
	if(valPostJobs()) {
		frm.submit();	
		ShowLightBox('loader'); return false;		
	}
}

//go to edit job page
function goToEditMyJob(jobid) {
	//alert(jobid);
	window.location = "index.php?my&editjob="+jobid;
}

//delete my job
function funDelMyJob(jobid) {
	//alert(jobid);
	var del=confirm("Are you sure want to delete your travel job?");
    if(del==true) {
		frm = document.getElementById('frmMyJobList');
		document.getElementById('hidDelJob').value=jobid;
		frm.submit();
	}
}

//validate post travel job
function valPostJobs() {
	var title=document.getElementById("job_title");
	var description=document.getElementById("job_description");
	var designation=document.getElementById("job_designation");	  
	var location=document.getElementById("job_location");
	var cname=document.getElementById("company_name");	  
	var cemail=document.getElementById("company_email");
	var cphone=document.getElementById("company_phone");
	   
	 submitOK="true";
	 if (title.value=='') {
		 alert("Enter job title");
		 title.focus();
		 submitOK="false";
		 return false;
	 }	
	 if (designation.value=='') {
		 alert("Enter job designation");
		 designation.focus();
		 submitOK="false";
		 return false;
	 }		 
	 if (location.value=='') {
		 alert("Enter job location");
		 location.focus();
		 submitOK="false";
		 return false;
	 }	
	 
	 if (description.value=='') {
		 alert("Enter job description");
		 description.focus();
		 submitOK="false";
		 return false;
	 }	 
	 
	  if (cname.value=='') {
		 alert("Enter company name");
		 cname.focus();
		 submitOK="false";
		 return false;
	 }
	 
	 if (cemail.value=='') {
		 alert("Enter company email address");
		 cemail.focus();
		 submitOK="false";
		 return false;
	 }
	 
	 if(cemail.value != '')  {
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(cemail.value)) {
			  var err=0;
		}
		else {
			alert("Invalid E-mail Address! Please re-enter.");				
			cemail.value="";
			cemail.focus();
			submitOK="false"; return false;	
		}
	 } 
	 
	 if (submitOK=="false")
	 {
	 	return false;
	 }
	 else { return true; }
}


//ajax comments section
function getPostComment(val,divId){
	 if(funValidateComment()) {	
	 
		 document.getElementById(divId).innerHTML="<img src='images/busyblu.gif'> Please wait..";
		 xmlHttp = GetXmlHttpObject();  //create object for http request
		 if (xmlHttp==null)  {
			  alert ("Your browser does not support AJAX!");
			  return;
		  }	  

         var ned = nicEditors.findEditor('txtComment');
         var txtcmt=escape(ned.getContent());
         
          
		 var url="get_post_comment.php"
		 //url=url+"?comment="+val['txtComment'].value+"&user="+val['hidUser'].value+"&video="+val['hidVideo'].value;
         url=url+"?comment="+txtcmt+"&user="+val['hidUser'].value+"&video="+val['hidVideo'].value;
         
         //alert(url);
		
		 xmlHttp.open("GET", url, true);
		 xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		 xmlHttp.send(null);		 
		 xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState==4) { 			
			document.getElementById('txtComment').value="";
            ned.setContent("");
			document.getElementById(divId).innerHTML=xmlHttp.responseText;
			}
		 }
	} 	 	
}

function funValidateComment() {
	//var txtcmt=document.getElementById("txtComment");
    var ned = nicEditors.findEditor('txtComment');
    var txtcmt=ned.getContent();
	//if(txtcmt.value=="") 
    if (txtcmt=="")
    {
		alert("Enter your comment");
		txtcmt.focus();
		return false;
	}
	return true;
}


//edit profile
function funEditMyProfile() {
	var frm = document.getElementById("frmEdit");
	document.getElementById("hidEditPro").value = "edit profile";	
	frm.submit();	
	ShowLightBox('loader'); return false;			
}

function getRequestComment(val,divId){
    var userId = val['hidUser'].value;
    var subject = val['txt_title'].value;
    var ned = nicEditors.findEditor('txt_message');
    var txtmsg=escape(ned.getContent());

    if(userId=="") 
    {
        alert("Please sign-in to post a request.");
        return;
    }
    if(subject=="") {
        alert("Please enter a video request subject.");
        return;
    }
    if(txtmsg=="") {
        alert("Please enter a video request message.");
        return;
    }

     document.getElementById(divId).innerHTML="<img src='images/busyblu.gif'> Please wait..";
     xmlHttp = GetXmlHttpObject();  //create object for http request
     if (xmlHttp==null)  {
          alert ("Your browser does not support AJAX!");
          return;
      }      

     var url="get_request_comment.php"
     url=url+"?message="+txtmsg+"&subject="+subject+"&user="+userId;
    

     xmlHttp.open("GET", url, true);
     xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     xmlHttp.send(null);         
     xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState==4) {             
        document.getElementById('txt_message').value="";
        document.getElementById('txt_title').value="";
        ned.setContent("");
        document.getElementById(divId).innerHTML=xmlHttp.responseText;
        }
     }
}

function getTextReply(val,divId)
{
    document.getElementById(divId).innerHTML="<img src='images/busyblu.gif'> Please wait..";     
    xmlHttp = GetXmlHttpObject();  //create object for http request
    if (xmlHttp==null)  {
         alert ("Your browser does not support AJAX!");
         return;
     }      

     var ned = nicEditors.findEditor('txtReply');
     var txtrpl=escape(ned.getContent());
     
      
     var url="get_post_textreply.php"
     //url=url+"?comment="+val['txtComment'].value+"&user="+val['hidUser'].value+"&video="+val['hidVideo'].value;
     url=url+"?txtreply="+txtrpl+"&user="+val['hidUser'].value+"&requestid="+val['hidRequest'].value;
     
     //alert(url);
    
     xmlHttp.open("GET", url, true);
     xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     xmlHttp.send(null);         
     xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState==4) {             
            document.getElementById('txtReply').value="";
            ned.setContent("");
            document.getElementById(divId).innerHTML=xmlHttp.responseText;
            window.location='index.php?vreplysuc';
        }
     }
}

