﻿// JScript File

//Function To Resest all opinion Form field values.
function ResetAll()
{
    document.getElementById('txtQuestionText').value ="";
    document.getElementById('txtChoice1').value ="";
    document.getElementById('txtChoice2').value =""; 
    document.getElementById('txtChoice3').value ="";    
    document.getElementById('txtChoice4').value ="";
    document.getElementById('txtChoice5').value =""; 
    document.getElementById('ddlNoofChoices').value ="5";
    var ErrorsDiv = document.getElementById("WarningMsgs_RegistrationErrors");         
    ErrorsDiv.style.display = "none";
     document.getElementById("WarningMsgs_divWarningInfo").style.display = "none";
   return(false); 
   
}

 // Function To Validate that at least one Choice is selected for Voting  
function VoteOpinionPoll()
{   
     
//   var rdo = document.getElementsByName("optPollChoice");
//   
//          
//    if(!(rdo[0].checked) && !(rdo[1].checked) && !(rdo[2].checked) && !(rdo[3].checked) && !(rdo[4].checked))
//    {
//        alert("Please Select the option");
//        return(false);
//    }
//    return(true);

var rdo = document.getElementsByName("optPollChoice");

for(i=0;i<rdo.length;i++)
{
    if(rdo[i].checked)
    {
       flag = true;
       return(flag);
       break;
    }
    else
    {
       flag = false;       
        
    }
  
}
   if(flag ==false)
   {
         //alert(" Please select an option to cast your vote.");
        document.getElementById("WarningMsgs_RegistrationErrors").innerHTML=" Please select an option to cast your vote.";
        document.getElementById("WarningMsgs_divWarningInfo").style.display = "block";
         return(flag);
   }

}
 
//This function validates all field values of the form AddOpinionPoll
function ValidateFields()
{
    
 			
    if (!ValidateQuestionText())
    {
	    return(false);
	}
			
	if(!ValidateChoice1())
	{
	    return(false);
	}
		
    if(!ValidateChoice2())
    {
	    return(false);
	}

    iNoofChoice = document.getElementById('ddlNoOfChoices').value;
      
    if(iNoofChoice > 2) 
    {
   	    if(!ValidateChoice3())
   	    {
		    return(false);
		}
	 }		
	 
	if(iNoofChoice > 3) 
    {
		if(!ValidateChoice4())
		{
			return(false);
	    }
	}	
   
    if(iNoofChoice > 4) 
    { 
        if(!ValidateChoice5())
        {
		    return(false);
		}
	}
}	


// Function To Validate Question Text
function ValidateQuestionText()
	{
		var Name ="Question Text";
		var Value = document.getElementById('txtQuestionText').value;
	    
		
		if(!( ValidateMandatory(Name,Value)&&  ValidateLength(Name,Value,1024) ) )
		{
			document.getElementById('txtQuestionText').setfocus;
			return(false);
			
		}
		return(true);	
	}


// Function To Validate Choice1 Value
function ValidateChoice1()
	{
		var Name ="Choice 1";
		var Value = document.getElementById('txtChoice1').value;
	
		
		if(!( ValidateMandatory(Name,Value)&&  ValidateLength(Name,Value,255) ) )
		{
			document.getElementById('txtChoice1').setfocus;
			return(false);
			
		}
		return(true);	
	}

// Function To Validate Choice2 Value
function ValidateChoice2()
	{
		var Name ="Choice 2";
		var Value = document.getElementById('txtChoice2').value;
	
		
		if(!( ValidateMandatory(Name,Value)&&  ValidateLength(Name,Value,255) ) )
		{
			document.getElementById('txtChoice2').setfocus;
			return(false);
			
		}
		return(true);	
	}
	
	// Function To Validate Choice3 Value
	function ValidateChoice3()
	{
		var Name ="Choice 3";
		var Value = document.getElementById('txtChoice3').value;
	
		
		if(!( ValidateMandatory(Name,Value)&&  ValidateLength(Name,Value,255) ) )
		{
			document.getElementById('txtChoice3').setfocus;
			return(false);
			
		}
		return(true);	
	}
	
    // Function To Validate Choice4 Value
	function ValidateChoice4()
	{
		var Name ="Choice 4";
		var Value = document.getElementById('txtChoice4').value;
	
		
		if(!( ValidateMandatory(Name,Value)&&  ValidateLength(Name,Value,255) ) )
		{
			document.getElementById('txtChoice4').setfocus;
			return(false);
			
		}
		return(true);	
	}
	
	 // Function To Validate Choice5 Value
	function ValidateChoice5()
	{
		var Name ="Choice 5";
		var Value = document.getElementById('txtChoice5').value;
	
		
		if(!( ValidateMandatory(Name,Value)&&  ValidateLength(Name,Value,255) ) )
		{
			document.getElementById('txtChoice5').setfocus;
			return(false);
			
		}
		return(true);	
	}


//Function To Validate Length of Field
function ValidateLength(vFldName, vFieldVal,vFieldSize)
{
     errors = "";
     errors +=vFldName + ' cannot contain more than '+ vFieldSize +'<br/>';  
     //errors +='</ul>';
	if(trim(vFieldVal).length > vFieldSize )
	{
		//alert(vFldName + ' cannot exceed than '+ vFieldSize );
	    var ErrorsDiv = document.getElementById("WarningMsgs_RegistrationErrors");         
        ErrorsDiv.style.display = "block";
         document.getElementById("WarningMsgs_divWarningInfo").style.display = "block";
        ErrorsDiv.innerHTML = "";
        ErrorsDiv.innerHTML = errors; 
		return(false);
	}
	else
	{
	    var ErrorsDiv = document.getElementById("WarningMsgs_RegistrationErrors");         
        ErrorsDiv.style.display = "none";
        document.getElementById("WarningMsgs_divWarningInfo").style.display = "none";
        
		return(true);
		
    }
	
}

//Function To Validate Mandatory Value
function ValidateMandatory(vFldName, vFieldVal)
{ 
     errors = "";
     errors +=vFldName + ' cannot be empty.'+'<br/>';  
     //errors +='</ul>';
	if(trim(vFieldVal) == '')
	{
	    //alert(vFldName + ' cannot be empty.');
	    var ErrorsDiv = document.getElementById("WarningMsgs_RegistrationErrors");
        ErrorsDiv.style.display = "block";
        document.getElementById("WarningMsgs_divWarningInfo").style.display = "block";
        ErrorsDiv.innerHTML = "";
        ErrorsDiv.innerHTML = errors; 

		return(false);
	}
	else
    {
        var ErrorsDiv = document.getElementById("WarningMsgs_RegistrationErrors");         
        ErrorsDiv.style.display = "none";
         document.getElementById("WarningMsgs_divWarningInfo").style.display = "none";
		return(true);
	}
}

//This Function display Choice Textboxes as per Selected Number of choice value.
function DisplayPollOpinionChoices()
{
     var iNoofChoice;
     
     if(document.getElementById('ddlNoofChoices')!=null)
     {
         iNoofChoice =document.getElementById('ddlNoofChoices').value;
         if(iNoofChoice == 2)
         {
          
            document.getElementById("divChoice3").style.visibility ="hidden";
            document.getElementById("lblChoice3").style.visibility ="hidden";
            document.getElementById("txtChoice3").style.visibility ="hidden";
            
            document.getElementById("divChoice4").style.visibility ="hidden";
            document.getElementById("lblChoice4").style.visibility ="hidden";
            document.getElementById("txtChoice4").style.visibility ="hidden";
            
            document.getElementById("divChoice5").style.visibility ="hidden";
            document.getElementById("lblChoice5").style.visibility ="hidden";
            document.getElementById("txtChoice5").style.visibility ="hidden";
            

        }
        
        if(iNoofChoice == 3)
        {
            document.getElementById("divChoice3").style.visibility ="visible";
            document.getElementById("lblChoice3").style.visibility ="visible";
            document.getElementById("txtChoice3").style.visibility ="visible";
            
            document.getElementById("divChoice4").style.visibility ="hidden";
            document.getElementById("lblChoice4").style.visibility ="hidden";
            document.getElementById('txtChoice4').style.visibility ="hidden";
            
            document.getElementById("divChoice5").style.visibility ="hidden";
            document.getElementById("lblChoice5").style.visibility ="hidden";
            document.getElementById("txtChoice5").style.visibility ="hidden";
            
        }
        
        if(iNoofChoice == 4)
        {
                      
            document.getElementById("divChoice3").style.visibility ="visible";
            document.getElementById("lblChoice3").style.visibility ="visible";
            document.getElementById("txtChoice3").style.visibility ="visible";
            
            document.getElementById("divChoice4").style.visibility ="visible";
            document.getElementById("lblChoice4").style.visibility ="visible";
            document.getElementById("txtChoice4").style.visibility ="visible";
            
            document.getElementById("divChoice5").style.visibility ="hidden";
            document.getElementById("lblChoice5").style.visibility ="hidden";
            document.getElementById("txtChoice5").style.visibility ="hidden";
        }
        
        if(iNoofChoice == 5)
        {
                   
            document.getElementById("divChoice3").style.visibility ="visible";
            document.getElementById("lblChoice3").style.visibility ="visible";
            document.getElementById("txtChoice3").style.visibility ="visible";
            
            document.getElementById("divChoice4").style.visibility ="visible";
            document.getElementById("lblChoice4").style.visibility ="visible";
            document.getElementById("txtChoice4").style.visibility ="visible";
            
            document.getElementById("divChoice5").style.visibility ="visible";
            document.getElementById("lblChoice5").style.visibility ="visible";
            document.getElementById("txtChoice5").style.visibility ="visible";
        }
    }
    //return(false);
    
}

function LoadOpinionPollResult()
{
   // var PollId =Window.document.getElementById("PollId1").name;
   

  // if (navigator.appName.indexOf("Microsoft Internet")==-1)
  // {
    
       // alert(document.pollID1);
     
//       window.document["pollID1"].SetVariable("LegendText",document.getElementById('hidLegendText').value);
//       window.document["PollID1"].SetVariable("LegendValues",document.getElementById('hidLegendValues').value);
//    
    //     document.getElementById("pollID1").SetVariable("LegendText",document.getElementById('hidLegendText').value);
    //     document.getElementById("pollID1").SetVariable("LegendValues",document.getElementById('hidLegendValues').value);
        

   // }
  //  else
   // {
      
        
        window.pollID1 = document.getElementById('pollID1');
     
        document.pollID1.SetVariable("LegendText",document.getElementById('hidLegendText').value);
        document.pollID1.SetVariable("LegendValues",document.getElementById('hidLegendValues').value);
   // }

}

function loadUPEventHandler()
{   
    window.pollID1 = document.getElementById('pollID1');

   var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(UPEndRequestHandler);
        
    if(document.getElementById('hidPollId').value != null)
    {
        if(document.getElementById('hidPollId').value !=0 )
        {
            LoadOpinionPollResult();
        }
    }
    //prm.add_beginRequest(BeginRequestHandler);
}

function UPEndRequestHandler(sender, args) 
{
//    if (args.get_error() == undefined)
//        alert("updated successfully!!");
//    else
//        alert("There was an error" + args.get_error().message);
   var err = args.get_error();
   if (err)
    {
      if (err.name == "Sys.WebForms.PageRequestManagerServerErrorException")
       {
             args.set_errorHandled(true);
        }
        if (err.name == "Sys.WebForms.PageRequestManagerParserErrorException") 
              {
                e.set_errorHandled(true);
              }
    }
    var UserMessage = null;
    var EditMessage = null;
    UserMessage = document.getElementById("hidUserMessage"); 
    
    //alert("EndRequest");
   // alert(UserMessage.value);
    if(UserMessage != null && UserMessage != "")
    {
       var UserMessageVal = UserMessage.value;
    }
    switch(UserMessageVal)
    {               
        case "CallFlashFile":
             LoadOpinionPollResult();    
             break;     
             
        case "ReloadFlashFile":
             LoadOpinionPollResult();   
             break; 
                           
       case "ErrorCallFlashFile":
            closepopup();             
             alert("Error in loading flash file"); 
              break;     
                            
        default:
            return;
         
    }
    
    UserMessage.value = "";
    
}
    
