function emptyUserBox(o){
	if(o.value == 'User ID')
		o.value='';
}
function emptyPswBox(o){
	if(o.value == 'Login ID')
		o.value='';
}
$(function(){
	$("#txtpsw").bind("keypress", function(e) {
	  if (e.keyCode == 13){
		 headerLogin(); 
	  }	  
	});
});

function headerLogin(){
	var frm = document.getElementById('form-login');
	var rtn = validateEmail(frm.txtlogin,"please enter valid Login ID");
	if(rtn){
		rtn = validateString(frm.txtpsw,"Password");
		if(rtn){
			$.ajax({
				type:"POST",
				url:"include/chklogin.php" ,
				data:({
					  email : $("#txtlogin").val(),
					  psw : $("#txtpsw").val(),
					  action : 'login',
					  url: document.location.href
					  }),
				dataType:"html",
				cache:false,
				success:function(args){
					if(args==1)
						document.location.reload();
					else
						alert('Invalid Login ID/password.');
				}
			});
		} 
	}
}
// -----------------    Script for fetching Rss feeds   --------------------
/* <![CDATA[ */
function animate(id){
	$(id).html("<div class='loading'></div>")
}
function getLatestNews(id){  
	animate(id);
	$.ajax({
		type:"GET",
		url:"include/rss.php",
		data:({url:"http://www.fifa.com/newscentre/news/rss.xml",title:$(id).prev().html()}),
		cache:false,
		dataType:"html",
		success:function(feed){
			$(id).html(feed)
		}
	});
}
function getlatest_photo(id){
	animate(id);
	$.ajax({
		type:"GET",
		url:"include/rss.php",
		data:({url:"http://www.fifa.com/newscentre/photo/rss.xml",title:$(id).prev().html()}),
		cache:false,
		dataType:"html",
		success:function(feed){
			$(id).html(feed)
		}
	});
}
function getlatest_videos(id){
	animate(id);
	$.ajax({
		type:"GET",
		url:"include/rss.php",
		data:({url:"http://www.fifa.com/worldcup/video/rss.xml",title:$(id).prev().html()}),
		cache:false,
		dataType:"html",
		success:function(feed){
			$(id).html(feed)
		}
	});
}
function getlatest_fifaNews(id){
	animate(id);
	$.ajax({
		type:"GET",
		url:"include/rss.php",
		data:({url:"http://www.fifa.com/worldcup/news/interviews.xml",title:$(id).prev().html()}),
		cache:false,
		dataType:"html",
		success:function(feed){
			$(id).html(feed)
		}
	});
}

function getlatest_articles(id){
	animate(id);
	$.ajax({
		type:"GET",
		url:"include/rss.php",
		data:({article:"http://www.fifa.com/media/rss.xml"}),
		cache:false,
		dataType:"html",
		success:function(feed){ 
			$(id).html(feed)
		}
	});
}

/* ]]> */	
// -------------  extra function for validation of predictions
function check4Semifinalist(){
	if($("#SemiFTeam1").val() == $("#SemiFTeam2").val() || $("#SemiFTeam1").val() == $("#SemiFTeam3").val()|| $("#SemiFTeam1").val() == $("#SemiFTeam4").val()){
		alert('4 semifinalist must be unique');
		return false;
	}
	if($("#SemiFTeam2").val() == $("#SemiFTeam3").val() || $("#SemiFTeam2").val() == $("#SemiFTeam4").val()){
		alert('4 semifinalist must be unique');
		return false;
	}
	if($("#SemiFTeam3").val() == $("#SemiFTeam4").val() || $("#SemiFTeam2").val() == $("#SemiFTeam4").val()){
		alert('4 semifinalist must be unique');
		return false;
	}
		
	return true;
}
function check2finalist(){ 
	if($("#FinalistTeam1").val() != $("#SemiFTeam1").val() && $("#FinalistTeam1").val() != $("#SemiFTeam2").val()&& $("#FinalistTeam1").val() != $("#SemiFTeam3").val()&& $("#FinalistTeam1").val() != $("#SemiFTeam4").val()){
		alert('2 finalist must be with in 4 semifinalist');
		return false;
	} 
	if($("#FinalistTeam2").val() != $("#SemiFTeam1").val() && $("#FinalistTeam2").val() != $("#SemiFTeam2").val() && $("#FinalistTeam2").val() != $("#SemiFTeam3").val()&& $("#FinalistTeam2").val() != $("#SemiFTeam4").val()){
		alert('2 finalist must be with in 4 semifinalist');
		return false;
	} 
	
	if($("#FinalistTeam1").val() == $("#FinalistTeam2").val()){
		alert('2 semifinalist must be unique');
		return false;
	} 
	return true;
}
function finalist(){ 
	if($("#WinnerTeam").val() != $("#FinalistTeam1").val() && $("#WinnerTeam").val() != $("#FinalistTeam2").val()){
		alert('Winner team must be with in 2 finalist');
		return false;
	}  
	return true;
}


function Vote(){
		var purl = "include/pollvote.php?pid=" + $("#hndpid").val() + "&vote&ids=";
		var cnt = 0;
		$("input[name=poolfield[]]").each(function(){
			if($(this).attr("checked")){
				purl += $(this).val() + ",";
				cnt = 1;
			}
		});
		if(cnt == 0){
			alert("please select one option");
		}else{
			purl = purl.substr(purl, (purl.length - 1));
			$("#pooltbl").fadeOut(300,function(){
				// create cookie for voit .			
				//createCookie("poll",$("#hndpid").val(),30);
				$("#_Pool").html("<div align='center'>please wait while loading...</div>");
				$.ajax({
					url: purl,
					method:'get',
					type:'html',
					success:function(args){
						$("#_Pool").html(args);
						$("#pooltbl").fadeIn(300);
						$("#View_result").html("Back"); // change text of view result link
					}
				});
			});
		}
	}
	function viewResult(){
		var purl = "include/pollvote.php?result&pid=" + $("#hndpid").val() ;
		$("#pooltbl").fadeOut(300,function(){
			$("#_Pool").html("<div align='center'>please wait while loading...</div>");
			$.ajax({
					url: purl,
					method:'get',
					type:'html',
					success:function(args){
						$("#_Pool").html(args);
						$("#pooltbl").fadeIn(300);
					}
			});
		});
	}
	function viewFrom(){
	var purl = "include/pollvote.php?form&pid=" + $("#hndpid").val() ;
		$("#pooltbl").fadeOut(300,function(){
			$.ajax({
					url: purl,
					method:'get',
					type:'html',
					success:function(args){
						$("#_Pool").fadeIn(300);
						$("#_Pool").html(args);
					}
			});
		});
	} // change text of view result link
	/*function viewPoolResult(o){
		if(o.innerHTML == "View Results"){
			o.innerHTML = "Back";
			viewResult();
		}else{
			o.innerHTML = "View Results";
			viewFrom();
		}
	}*/
// ======================  Cookie  =====================
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name){
	createCookie(name,"",-1);
}

