/* disable prev days functions 
 * Calendar functions 
 * */

function days_between(date1, date2) {
	var difference=date1-date2
	formatdifference=Math.round(difference/1000/60/60/24)
	return formatdifference
}

function CompareTwoDates(date1, date2) {
	//debugger;
	var str1  = date1;
	var str2  = date2;
	var dt1   = parseInt(str1.substring(0,2),10);
	var mon1  = parseInt(str1.substring(3,5),10);
	var yr1   = parseInt(str1.substring(6,10),10);
	var dt2   = parseInt(str2.substring(0,2),10);
	var mon2  = parseInt(str2.substring(3,5),10);
	var yr2   = parseInt(str2.substring(6,10),10);
	var date1 = new Date(yr1, mon1-1, dt1);
	var date2 = new Date(yr2, mon2-1, dt2);
	if(date2 > date1) {
		return false;
	} else {
		return true;
	}
}

function prevdate(date) { // disable Days before today and Disable Days after allowed Period

	var MyDate=new Date();

	var allowedDaysToShowOnCalendar=120;

	/*Configurable grace Period*/
	var allowedGracePeriod=0;

	/*Date difference between calendar date, current date */
	MyDate.setHours(00,00,00,00);
	date.setHours(00,00,00,00);
	var datediff=days_between(date, MyDate);
	
	/*date difference should be allowed day to show on calendar*/
	if (datediff >=allowedGracePeriod) {
		//Date difference is with in allowed period - show this date on Calendar
		return false;
	} else {
		//Date difference is not with in allowed Period - Don't show it on Calendar
		return true;
	}
		
	 
}

//end calendar functions


function clear_textbox(ctrlName,chkText)
{
	//alert(ctrlName.style.color);
	if (ctrlName.value == chkText)
	ctrlName.value = "";
	ctrlName.style.color="#000000";
} 

function fill_textbox(ctrlName,chkText)
{
	if (ctrlName.value == "")
	ctrlName.value = chkText;
	
} 

function show_div(boxid){
	document.getElementById(boxid).style.display="block";
}


//Ajax Request Code
var http_request = false;
function makePOSTRequest(url, parameters,funct) {
   http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
         http_request.overrideMimeType('text/xml');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }

   http_request.onreadystatechange = funct;
   http_request.open('POST', url, true);
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", parameters.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.send(parameters);
}

//Check User availability
function alertContents() {
    if (http_request.readyState == 4) {
       if (http_request.status == 200) {
          result = http_request.responseText; 
		  exprNot = /\bnot available\b/;
		  if(! exprNot.test(result)){
			  document.getElementById('check').className = "greentext";
		  }else{
			  document.getElementById('check').className = "redtext";
		  }
          document.getElementById('check').innerHTML=result;
          document.getElementById('check').style.display = 'block'; 
       } else {
          alert('There was a problem with the request.');
       }
    }
 }

//Check User availability
function alertContents1() {
    if (http_request.readyState == 4) {
       if (http_request.status == 200) {
          result = http_request.responseText; 
		  exprNot = /\bnot available\b/;
		  if(! exprNot.test(result)){
			  document.getElementById('check1').className = "greentext";
		  }else{
			  document.getElementById('check1').className = "redtext";
		  }
          document.getElementById('check1').innerHTML=result;
          document.getElementById('check1').style.display = 'block'; 
       } else {
          alert('There was a problem with the request.');
       }
    }
 }

function alertContents2() {
    if (http_request.readyState == 4) {
       if (http_request.status == 200) {
          result = http_request.responseText; 
		  exprNot = /\bnot available\b/;
		  if(! exprNot.test(result)){
			  document.getElementById('check2').className = "greentext";
		  }else{
			  document.getElementById('check2').className = "redtext";
		  }
          document.getElementById('check2').innerHTML=result;
          document.getElementById('check2').style.display = 'block'; 
       } else {
          alert('There was a problem with the request.');
       }
    }
 }
//Check User availability
function sendAjaxMail() {
    if (http_request.readyState == 4) {
       if (http_request.status == 200) {
          result = http_request.responseText; 
		  
          //alert(result);
          document.getElementById('error').innerHTML=result;
          slideup('login_div');
          //document.getElementById('check1').style.display = 'block'; 
       } else {
          alert('There was a problem with the request.');
       }
    }
 }


//Reset password
function JSF_ResetPassword() {
    if (http_request.readyState == 4) {
       if (http_request.status == 200) {
          result = http_request.responseText;
          if(parseInt(result)){
        	  document.getElementById('msg').innerHTML="Password Changed Successfully";
          }
       } else {
          alert('There was a problem with the request.');
       }
    }
 }

//Change the captcha
function Changecaptcha() {
    if (http_request.readyState == 4) {
       if (http_request.status == 200) {
          result = http_request.responseText;  
          alert(result);
          //document.getElementById('msg').innerHTML=result;
          document.getElementById('divcpt').innerHTML= result;
       } else {
          alert('There was a problem with the request.');
       }
    }
 }


 function createCities(){
 if (http_request.readyState == 4) {
       if (http_request.status == 200) {
          result = http_request.responseText;  
          //alert(result);
          var obj = document.getElementById('city');
		  eval(result);	// Executing the response from Ajax as Javascript code	
       } else {
          alert('There was a problem with the request.');
       }
    }
	
}

function createLocations(){
 if (http_request.readyState == 4) {
       if (http_request.status == 200) {
          result = http_request.responseText;     
                  
          var obj = document.getElementById('location');
		  eval(result);	// Executing the response from Ajax as Javascript code	
       } else {
          alert('There was a problem with the request.');
       }
    }
	
}

function checkBoxCount(frm){
	frm = document.getElementById(frm);
	flg=0;
	for(i=0;i<frm.elements.length;i++){
		if(frm.elements[i].type == "checkbox"){
			if(frm.elements[i].checked==true){
				flg=flg+1;
			}
		}
	}
	if(flg<1){
		alert("Please Select a Record");
		return false;
	}
	return true;
}
	
function checkAll(frm,cbox){
	frm = document.getElementById(frm);
	//frm = document.getElementById(frm);
	for(i=0;i < frm.elements.length;i++){
		if(cbox.checked == true){
			if(frm.elements[i].type == "checkbox"){
				frm.elements[i].checked=true;
			}
		}	
		else if(cbox.checked == false){
			if(frm.elements[i].type == "checkbox"){
				frm.elements[i].checked=false;
			}
		}
	}
}

function deselectCheckall(frm, cboxall){
	for(i=0;i < frm.elements.length;i++){
  		if(frm.elements[i].type == "checkbox"){
			cboxall.checked=false;	
  		}
 	}
}

function checkAllradios(total,column,cbox){
//debugger;
	var totalcount = total;
	var col = column; 
	
	for(j=0; j<4; j++){
		if(j!=column){					
			document.getElementById('checkbox_'+j).checked = false;
		}
	}
	for(i=0;i < totalcount;i++){	    
		if(cbox.checked == true){		   
			document.getElementById("radio_"+i+"_"+col).checked =true;
		}	
		else if(cbox.checked == false){
			document.getElementById("radio_"+i+"_"+col).checked = false;
		}
	}
}


function changeAllStatus(frm,act){
	if(checkBoxCount(frm)){
		//alert("hi");
		frm = document.getElementById(frm);
		frm.status.value = act;
		//alert(document.frm);
		frm.submit();
	}
}


//Change the user status
function ChangeUserStatus() {
    if (http_request.readyState == 4) {
       if (http_request.status == 200) {
          result = http_request.responseText;  
          //alert(result);
          //document.getElementById('msg').innerHTML=result;
          document.getElementById('msg').style.display = 'block'; 
       } else {
          alert('There was a problem with the request.');
       }
    }
 }

function ChangeRecStatus(t,f,i,m) {    
  var m = (m == null) ? "Status" : m;
  var poststr = "t=" + encodeURI(t) +"&f="+ encodeURI(f)+"&i="+ encodeURI(i)+"&flag=t";
  //var url=site_url+'scripts/ChangeStatus.php';
  res = makePOSTRequest(site_url+'scripts/ChangeStatus.php', poststr,ChangeUserStatus);
  //+alert(t+','+f+','+i+','+m);
  if(document.getElementById(i).title=="Inactive") {
	  document.getElementById(i).title="Active";
	  document.getElementById(i).innerHTML="Active";
   	  document.getElementById('msg').innerHTML= m+" Activated successfully";
  } else {
	  document.getElementById(i).title="Inactive";
	  document.getElementById(i).innerHTML="Inactive";
	  document.getElementById('msg').innerHTML= m+" Deactivated successfully";
  }
}

function ChangeFrntStatus(t,f,i) {     
  var poststr = "t=" + encodeURI(t) +"&f="+ encodeURI(f)+"&i="+ encodeURI(i)+"&flag=t";
  res = makePOSTRequest('scripts/ChangeStatus.php', poststr,ChangeUserStatus);
  i = i+"f";
  if(document.getElementById(i).title=="Hide") {
	  document.getElementById(i).title="Show";
	  document.getElementById(i).innerHTML="Show";
  } else {
	  document.getElementById(i).title="Hide";
	  document.getElementById(i).innerHTML="Hide";
  }
   document.getElementById('msg').innerHTML="Show/Hide in Home Page changed successfully";
}

function ChangeNewsletterStatus(t,f,i) {     
  var poststr = "t=" + encodeURI(t) +"&f="+ encodeURI(f)+"&i="+ encodeURI(i)+"&flag=t";
  res = makePOSTRequest('scripts/ChangeStatus.php', poststr,ChangeUserStatus);
  i = i+"l";
  if(document.getElementById(i).title=="Subscribed") {
	  document.getElementById(i).title="UnSubscribed";
	  document.getElementById(i).innerHTML="UnSubscribed";
	  document.getElementById('msg').innerHTML="Newsletter UnSubscribed successfully";
  } else {
	  document.getElementById(i).title="Subscribed";
	  document.getElementById(i).innerHTML="Subscribed";
	  document.getElementById('msg').innerHTML="Newsletter Subscribed successfully";
  }
}

function ChangeETemplateStatus(i) {     
  var poststr = "i="+ encodeURI(i)+"&flag=et";
  res = makePOSTRequest('scripts/ChangeStatus.php', poststr,ChangeUserStatus);
  if(document.getElementById(i).title=="Inactive") {
	  document.getElementById(i).title="Active";
	  document.getElementById(i).innerHTML="Active";
      document.getElementById('msg').innerHTML="Email Template Activated successfully";
  } else {
	  document.getElementById(i).title="Inactive";
	  document.getElementById(i).innerHTML="Inactive";
      document.getElementById('msg').innerHTML="Email Template Deactivated successfully";
  }
}


function showHideButton(){
	val = document.getElementById('src_status');
	status = val.options[val.selectedIndex].value;
	if(status == 1){
		document.getElementById('btn_active').style.visibility = "hidden";
		document.getElementById('btn_active').style.display = "none";
		document.getElementById('btn_deactive').style.visibility = "visible";
	}else if(status == 0){
		document.getElementById('btn_active').style.visibility = "visible";
		document.getElementById('btn_deactive').style.visibility = "hidden";
		document.getElementById('btn_deactive').style.display = "none";
	}else{
		document.getElementById('btn_active').style.visibility = "hidden";
		document.getElementById('btn_active').style.display = "none";
		document.getElementById('btn_deactive').style.visibility = "hidden";
		document.getElementById('btn_deactive').style.display = "none";
	}
}

function checkSearchForm(){
	if(document.getElementById('byDate').checked && document.getElementById('byDate').value == 'byDate' ){
		startDate = document.getElementById('start_date').value;
		endDate = document.getElementById('end_date').value
		if(startDate == 'Start Date' || endDate == 'End Date'){
		  alert("Please select Start date and End date");
		  return false;
		}
		var dt1   = parseInt(startDate.substring(0,2),10);
		var mon1  = parseInt(startDate.substring(3,5),10);
		var yr1   = parseInt(startDate.substring(6,10),10);
		var dt2   = parseInt(endDate.substring(0,2),10);
		var mon2  = parseInt(endDate.substring(3,5),10);
		var yr2   = parseInt(endDate.substring(6,10),10);
		var date1 = new Date(yr1, mon1, dt1);
		var date2 = new Date(yr2, mon2, dt2);
		if(date2 < date1){
		  alert("Start date cannot be later than End date");
		  return false;
		}
	}
	return true;
}

function mypopup(file){
   mywindow = window.open (file,"mywindow","location=0,status=0,scrollbars=1,width=650,height=570,left=50,top=50");
} 

function globepopup(file){
	   mywindow = window.open (file,"mywindow","location=0,status=0,scrollbars=1,width=970, height=500,left=50,top=50");
} 
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
function VideoVisitStatus() {
    if (http_request.readyState == 4) {
       if (http_request.status == 200) {
          result = http_request.responseText;  
          // document.getElementById('msg').style.display = 'block'; 
       } else {
          alert('There was a problem with the request.');
       }
    }
 }
function sdet(el){
       //if(el!= null){
               var off_url = el.href;
               newwindow =window.open(off_url,'newwin','top=100,left=100,height=700,width=650,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes');
               newwindow.focus();
       // }
return false;
}
function textCounter(field,maxlimit) {
//alert(field.value.length);
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
}
function chkDivLogin() {
//alert(http_request.readyState);
    if (http_request.readyState == 4) {
       if (http_request.status == 200) {
          result = http_request.responseText; 
         
       } else {
          alert('There was a problem with the request.');
       }
    }
 }
 
function trim(str) {

	 if (typeof str != "string") return str;
	 str = str.replace(/^\s+|\s+$/g, "");
	 return str;
}