// contest.js

// newFunction 
function doContestAction(contestId,isInternational,isNeedRegister) 
{
	var title = "" ;
	openContestWindow() ;
	
	if( document.getElementById("aContestTitle" + contestId) )
	{
		title = document.getElementById("aContestTitle" + contestId ).innerText ;
	}
	if( document.getElementById("contestId")  )
	{
		document.getElementById("contestId").value = contestId ; 
	}
	if( document.getElementById("contestTitle")  )
	{
		document.getElementById("contestTitle").value = title ; 
	}	
	if( document.getElementById("international")  )
	{
		document.getElementById("international").value = isInternational ; 
	}
	if( document.getElementById("needRegistration")  )
	{
		document.getElementById("needRegistration").value = isNeedRegister ; 
	}	
	if( document.getElementById("contestParticipateForm")  )
	{
		
		if( isNeedRegister == 'false' )
			document.getElementById("contestParticipateForm").action.value = "contestParticipatePage"  ;
		else
			document.getElementById("contestParticipateForm").action.value ="getRegistrationPage" ;
			
			
		document.getElementById("contestParticipateForm").submit();  
	}
			
	// 
}

function openContestWindow()
{
	var winContest = window.open("","contestWindow","height=450,width=620,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes");	
	winContest.focus();
}
function validateUser()
{
	var emailVal = "" ;
	if( document.getElementById("email") )
	{
		emailVal = Trim(document.getElementById("email").value) ;
		
		if( emailVal == "" || !isEmail(emailVal) )
		{
			alert("الرجاء ادخال البريد الكتروني بشكل صحيح");
			document.getElementById("email").focus();
			return false ;
		}
	}
	if( document.getElementById("fullName") )
	{
		var fullNameVal = Trim(document.getElementById("fullName").value) ;		
		if( fullNameVal == "" )
		{
			alert("الرجاء ادخال الإسم");
			document.getElementById("fullName").focus();
			return false ;
		}
	}	
	if( document.getElementById("telephoneNo") )
	{
		var telephoneNoVal = Trim(document.getElementById("telephoneNo").value) ;		
		if( telephoneNoVal == "" )
		{
			alert("الرجاء ادخال رقم الهاتف");
			document.getElementById("telephoneNo").focus();
			return false ;
		}
		if( document.getElementById("countryId") && document.getElementById("countryId").value == "634" )
		{
			if( telephoneNoVal.length != 7 )
			{
				alert(" رقم الهاتف يتكون من 7 أرقام");
				document.getElementById("telephoneNo").focus();
				return false ;				
			}
			if( telephoneNoVal.substring(0,1) != "5" )
			{
				if( telephoneNoVal.substring(0,1) != "6" )
				{
					if( telephoneNoVal.substring(0,1) != "4" )
					{				
						alert('يجب أن يبدأ رقم الهاتف برقم 5 أو 6 أو 4 ');
						document.getElementById("telephoneNo").focus();
						return false ;				
					}	
				}	
			}			
		}
	}	
	url = "/IntServices/Actions?action=checkUserParticipation&contestId=" + document.getElementById("contestId").value  ;
	url += "&email=" + emailVal ;
	
	var ajax = new AJAXInteraction(url, validateCallback,"checkUserParticipation",-1); 
    ajax.doGet(); 	
    return false ;

}
function validateCallback(response,actionType,contestId)
{
	
	var error = response.match(/<error>(.*?)<\/error>/);
	var isParticipated = response.match(/<isParticipated>(.*?)<\/isParticipated>/);
	
	if( error )
	{
		alert(error[1]) ;
		return false ;
	}
	if( isParticipated )
	{
		alert("لقد شاركت في هذه المسابقة");
		return false;;	
	}
	if( document.getElementById("contestParticipateForm") )
	{
		document.getElementById("contestParticipateForm").submit();
	}	
}	
function validateContestAnswer(questionId)
{
	//alert(questionId); 
	var arrAnswers =  document.getElementsByName("q"+questionId) ;
	var isChecked = false ;
	var answerIdVal = "" ;
	if( arrAnswers && arrAnswers.length > 0 )
	{
		for(var i = 0 ; i < arrAnswers.length ; i++ )
		{
			if( arrAnswers[i].checked )
			{
				answerIdVal = arrAnswers[i].value  ;

				isChecked = true ;
				break ;
			}	
				
		}	
	}	
	
	if(!isChecked)
	{
		alert("الرجاء الإجابة على السؤال");
		return false; 
	}
	if( document.getElementById("questionId") )			
		document.getElementById("questionId").value = questionId ;
	if( document.getElementById("answerId") )			
		document.getElementById("answerId").value = answerIdVal ;

	if( document.getElementById("contestParticipateForm") )			
		document.getElementById("contestParticipateForm").submit();
		

}
function cancelContest(questionId)
{
	if( document.getElementById("questionForm") )
		document.getElementById("questionForm").reset();
}
