// JavaScript Document
/**********************************************************************************************************************************
 Author: Gautam G Gosavi
 Organization : Indelible Technologies
 Description : This contain javascript for contest file
 
 Modified By :                   Date                     Purpose
 
 
**********************************************************************************************************************************/


/*********************************************************************************************************************************
 Name : 
 Purpose : This below code uses ajax logic
*********************************************************************************************************************************/

var XMLresponse;
var http;
var doc;
var ajaxErr;

  
	


function createRequestObject() {
 // This function is used to create HTTP request object  
    var ObjReq;
    var browser = navigator.appName;
	
    if(browser == "Microsoft Internet Explorer"){
        ObjReq = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ObjReq = new XMLHttpRequest();
    }
    return ObjReq;
}

/*********************************************************************************************************************************
 Name : viewcurrentcontest
 Purpose : Will always load the current contest
*********************************************************************************************************************************/

function viewcurrentcontest()
{

 //location.reload("contest.php");
	self.location="contest.php";
	
}

/*********************************************************************************************************************************
 Name : viewcontest
 Purpose : Will always load the  contest based on combo selection
*********************************************************************************************************************************/

function viewcontest()
{
  var x=document.frmcontest.cmbselect.value;

  if(x!=0)
  { 
     self.location="contest.php?x="+x+"";
  }
  else{alert("Please select proper contest");}
	
}

/*********************************************************************************************************************************
 Name : searchbaby
 Purpose : This will call babyresults.php for 
*********************************************************************************************************************************/
function searchbaby()
{
	
	var strbabyname=document.frmcontest.txtbabyname.value;

  if(strbabyname!=""  )
	{  
	  if(!checkforregularstring(strbabyname)){  alert('No numeric and special chars are allowed.');}else{
		//location.reload("babyresults.php?name="+strbabyname+"");
		  self.location="babyresults.php?name="+strbabyname+"";
		}
	}else
	{
	  alert("Please enter baby name");	
	}

	
	
	
}

/*********************************************************************************************************************************
 Name : votebaby
 Purpose : This will call voting logic
*********************************************************************************************************************************/

function votebaby()
{
	var voteid=document.getElementById("txtvoteid");
	
	if(voteid.value==0)
	{
	  alert("Please select the contestant");	
	  
	}else
	{
		sndReq(voteid.value);
		
		
		//loadresultpage();
	}
	
	
}

/*********************************************************************************************************************************
 Name : checkforregularstring
 Purpose : Will check for numeric and special chars and will disallow them.
*********************************************************************************************************************************/
function checkforregularstring(string)
{

  if (!string) return true;
  var Chars = "0123456789-@!~#$%^&*()"; //declaring all the chars defined which will be checked

	for (var i = 0; i < string.length; i++)
	{ 
	
	  if (Chars.indexOf(string.charAt(i)) >0)
	    return false;
	}
	return true;

	
}


function sndReq(voteid) {
   // This function sends HHTP request
    http = createRequestObject();
	
    http.open('get', 'balcontest.php?action=1&x='+voteid+'');
    http.onreadystatechange = handleResponse;
    http.send(null);
	
}
function handleResponse() {
   //This functions handle HTTP response
   
   if(http.readyState == 4){
        
         XMLresponse=http.responseText;
		
		
		
		 if(XMLresponse==11)
		 {
			
			 alert("Thanks for voting.");
			 window.open('voteresults.php')
			  
		 }
		 if(XMLresponse==33)
		 {
			  
			  alert("Error in Operation.Please try again");
		 }
		 if(XMLresponse==22)
		 {
			  
			  alert("Cannot vote you have already voted.");
			  window.open('voteresults.php');
		 }
			
    }
   
}

function loadresultpage()
{
   
  
   //window.location.reload("yahoo.com");
   //document.location.href="voteresults.php";
	
}

function submitenterkey(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
        ((evt.which) ? evt.which : 0));
    if (charCode ==13 ) {
       
	    var strbabyname=document.frmcontest.txtbabyname.value;

			if(strbabyname!="")
			{  
			  if(!checkforregularstring(strbabyname)){  alert('No numeric and special chars are allowed.');
			     return false;}else{
			  
				document.frmcontest.method="get";
				document.frmcontest.action="babyresults.php";
				document.frmcontest.submit();
			    //location.reload("babyresults.php?name="+strbabyname+"");
				
			   }
			 
			 
			}
			else
			{
			  alert("Please enter baby name");
			  return false;
			}
			
			
			
			
	    }
		
    
}
