
function check_length(my_form)
{
maxLen = 100; 
 if (my_form.ques.value.length > maxLen) {
 my_form.countdown.value = 0;
 my_form.ques.value = my_form.ques.value.substring(0, maxLen);
 var msg = "You have reached your maximum limit of characters allowed";
 alert(msg);
 return false;
 }
 else { 
 my_form.countdown.value = maxLen - my_form.ques.value.length; 
 }
}

function fn_ques(e,fieldid) {
var keycode;
	if(window.event) {
	keycode=window.event.keyCode;
	}
	else if(e) {
	keycode=e.which;
	}
		if((keycode==0) || (keycode==8) || (keycode==32) || (keycode==33) || (keycode==36) || (keycode==37) || (keycode==39) || (keycode==40) || (keycode==41) || (keycode==44) || (keycode>=48 && keycode<=57) || (keycode==63) || (keycode>=65 && keycode<=90) || (keycode>=97 && keycode<=122) || (keycode==127) || (keycode==45)) {
		return true;
		}	
		else {
		alert("Special characters are not allowed");
		return false;
		}
}

function fn_ans(e,fieldid){
var keycode;
if(window.event){
keycode=window.event.keyCode;
}
else if(e){
keycode=e.which;
}
if((keycode>=48 && keycode<=57) || (keycode>=65 && keycode<=90) || (keycode>=97 && keycode<=122) || (keycode==8) || (keycode==127) || (keycode==0) || (keycode==32) || (keycode==33) || (keycode==36) || (keycode==37) || (keycode==38) || (keycode==42) || (keycode==40) || (keycode==41) || (keycode==45) || (keycode==46) || (keycode==13)){
return true;
}
else{
alert("Special characters are not allowed");
return false;
}
}

function check_edate(dat)
{
 var   dt = new Date();
 var mon = dt.getMonth()+1;
 var dy = dt.getDate();
 var yar = dt.getFullYear();
 var sys_date = mon+"/"+dy+"/"+yar;
 var current_date = Date.parse(sys_date);

 var format = eval("/-/ig");
 var dfrom = document.post_ques.fdate.value.replace(format,"/");
 var temp = dfrom.split("/");
 var dfrom = temp[1]+"/"+temp[0]+"/"+temp[2];
 var fdate = Date.parse(dfrom);

 var format1 = eval("/-/ig");
 var dto = dat.replace(format1,"/");
 var temp1 = dto.split("/");
 var dto = temp1[1]+"/"+temp1[0]+"/"+temp1[2];
 var tdate = Date.parse(dto);

var edate = temp[1]+"/"+temp[0]+"/"+(parseInt(temp[2]) + parseInt(2));
edate = Date.parse(edate);

  if(tdate > edate) {
 alert('End date should not more than two years');
 document.post_ques.tdate.focus();
 return false;
 }
}

function quickpoll()
{
	if(document.post_ques.channel.value == "") 
	{
		alert("Please select the channel");
		return false;
	}
	if(document.post_ques.category.value == "")
	{
		alert("Please select the category");
		return false;
	}
	if(document.post_ques.ques.value == "")
	{
		alert("Please enter the poll question");
		return false;
	}
	if((document.getElementById("ans1").value == "") || (document.getElementById("ans2").value == ""))
	{
		alert("Minimum two answers required");	
		return  false;
	}
	if(document.post_ques.tdate.value == "")
	{
		alert("Please enter poll end date");
		return false;
	}
	 dt = new Date();
 var mon = dt.getMonth()+1;
 var dy = dt.getDate();
 var yar = dt.getFullYear();
 var sys_date = mon+"/"+dy+"/"+yar;
 var current_date = Date.parse(sys_date);

 var format = eval("/-/ig");
 var dfrom = document.post_ques.fdate.value.replace(format,"/");
 var temp = dfrom.split("/");
 var dfrom = temp[1]+"/"+temp[0]+"/"+temp[2];
 var fdate = Date.parse(dfrom);

 var format1 = eval("/-/ig");
 var dto = document.post_ques.tdate.value.replace(format1,"/");
 var temp1 = dto.split("/");
 var dto = temp1[1]+"/"+temp1[0]+"/"+temp1[2];
 var tdate = Date.parse(dto);

var edate = temp[1]+"/"+temp[0]+"/"+(parseInt(temp[2]) + parseInt(2));
edate = Date.parse(edate);
//alert(edate);

 if(fdate < current_date) {
 //alert('Start date should not less than current date');
 //document.post_ques.fdate.focus();
 //return false;
 }
 if(tdate < fdate) {
 alert('End date should not less than start date');	
 document.post_ques.tdate.focus();
 return false;
 }
 if(fdate == tdate) {
 alert('Start date and End date are same');
 document.post_ques.tdate.focus();
 return false;
 }
 if(tdate < current_date) {
 alert('End date should not less than current date');
 document.post_ques.tdate.focus();
 return false;
 }
  if(tdate > edate) {
 alert('End date should not more than two years');
 document.post_ques.tdate.focus();
 return false;
 }
	if(document.post_ques.terms.checked == false)
	{
		alert("Please read and accept our terms and conditions before posting the poll");
		return false;
	}
}

function readterms()
{
	if(document.getElementById("read").style.display == "none")
		{
			document.getElementById("read").style.display = "";
			$('#add_sub_forms').css({'left' : 150,'top' : 150, 'width': 735, 'height' : 360, 
        'border-width':5}); 
		}
	else
		{
			document.getElementById("read").style.display = "none";
			$('#add_sub_forms').css({'left' : 150,'top' : 150, 'width': 735, 'height' : 210, 
        'border-width':5});
		}
}

var arrInput = new Array(0);
var arrInputValue = new Array(0);

function addInput() {
  document.getElementById("remove").style.display = "";
	
  arrInput.push(arrInput.length);
  arrInputValue.push("");
  display();
}

function display() {
  document.getElementById('parah').innerHTML="";
  for (intI=0;intI<arrInput.length;intI++) {
	if(intI > 3) {
		alert("Maximum Six Answers Only Allowed");
		return false;
	}
	else {
	 document.getElementById('parah').innerHTML+=createInput(arrInput[intI], arrInputValue[intI]);
	 }
  }
}

function saveValue(intId,strValue) {
  arrInputValue[intId]=strValue;
}  

function createInput(id,value) {
  var snid = id+3;
  return "<tr><td width='100%'><b>Answer "+snid +":</b><br/><input type='text' id='ans"+id +"' name=ans[] onChange='javascript:saveValue("+ id +",this.value)' value='"+ value +"' size=15 maxlength='25' style='border: 1px solid grey;' onkeypress='return  fn_ans(event,this.id);'></td></tr>";
}

function deleteInput() {
  if (arrInput.length > 0) { 
     arrInput.pop(); 
     arrInputValue.pop();
  }
  
   if(arrInput.length == 0)
  {
	document.getElementById("remove").style.display = "none";
  }
  display(); 
}


// LIGHTBOX POP UP WINDOW OPEN
function quickpoll_forms(e,popid) {
    var rand = Math.random();   
    $.get('/modules/poll/quickpoll.php',{popid:popid,rand:rand},
    function (resp)
    {
        var windowWidth  = document.documentElement.clientWidth - 200;
        var windowHeight = document.documentElement.clientHeight;
        var popupHeight  = $("#add_sub_forms").height()+350;
        var popupWidth   = $("#add_sub_forms").width()+320;

        var mousex = e.clientX;////to get mouse position x axis
        var mousey = e.clientY;////to get mouse position y axis

        //mousex = mousex - 150 ;////to get the popup window in desired x position
        //mousey = mousey - 200;////to get the popup window in desired y position*/
        $('#add_sub_forms').css({'left' : 150,'top' : 150, 'width': 740, 'height' : 210, 
        'border-width':5});////set the css for popup using jquery
        $("#add_sub_forms").css({"opacity": "0.8"});
    
        //only need force for IE6
        //centering
        $("#backgroundPopup").css({"opacity": "0.7","height": windowHeight});  
        $("#backgroundPopup").fadeIn(30);
        $("#add_sub_forms").fadeIn(30);
        document.getElementById('add_sub_forms').innerHTML = resp;
        document.getElementById('add_sub_forms').style.visibility = "visible";
		
		document.getElementById('fea-poll').style.display = "none";
		document.getElementById('spo-poll').style.display = "none";
		
     });
}
// END LIGHTBOX POP UP WINDOW OPEN

// Close Lightbox Popup Window              ////////// function to close add popup /// add_sub_forms here
function close_subforms(addeddiv) {
    document.getElementById(addeddiv).innerHTML = "";
	$("#backgroundPopup").fadeOut(30);
    document.getElementById(addeddiv).style.visibility = "hidden";
	
	document.getElementById('fea-poll').style.display = "";
	document.getElementById('spo-poll').style.display = "";
}
// End Close Lightbox Popup Window

//TO SELECT CATEGORY FOR THE CHANNEL
var xmlhttp;
function fn_selectcatg(str)
  {
  //alert(str);
  xmlhttp=GetXmlHttpObject();
  if (xmlhttp==null)
    {
    alert ("Your browser does not support XML HTTP Request");
    return;
    }
  var url="/modules/poll/select_category.php";
  url=url+"?q="+str;
  url=url+"&sid="+Math.random();
  xmlhttp.onreadystatechange=stateChangedw;
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
  }

function stateChangedw()
  {
  if (xmlhttp.readyState==4)
    {
		document.getElementById("qqq").innerHTML="";
		document.getElementById("qqq").innerHTML=xmlhttp.responseText;
    }
  }

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
} 