var dcHasOpenPopup = 0;
// this is for opening pop-up windows
function dc_popup_win( url, name, widgets, openerUrl )
{
	var host = location.hostname;
	if (window == top) { window.top.name = "opener"; }
	var popupWin = window.open( url, name, widgets );
	if(popupWin) {dcHasOpenPopup = 1;}
	if ( popupWin && popupWin.opener ) {
		if ( openerUrl )
		{
			popupWin.opener.location = openerUrl;
		}
	}
	if ( popupWin) {
		popupWin.focus();
	}


}

function CheckForm()
{

    var count = 0;
    for (i = 0; i < document.SearchSavage.keywords.length; i++) {
        if (document.SearchSavage.keywords[i].checked == true)
            count++;
    }

    if (document.SearchSavage.query.value.length > 0) {
        count++;
    }

    if (count > 5 || count < 1) {
        alert('Please select at least 1, up to a maximum of 5 keywords and resubmit the search.');
        return false;
    } else {
        return true;
    }

}

function score ()
{
	var scoreDate = new Date();
	var scoreDay = scoreDate.getDate();
	var scoreMonth = scoreDate.getMonth() + 1;
	var scoreYear = scoreDate.getYear() - 2000;

	if (scoreDay < 10) {
		var scoreDay = '0' + scoreDay;
	}
	if (scoreMonth < 10) {
		var scoreMonth = '0' + scoreMonth;
	}
	if (scoreYear < 10) {
		var scoreYear = '0' + scoreYear;
	}

	var scoreToday  = scoreMonth + scoreDay + scoreYear;
	var scoreUrl = 'http://www.sportsnetwork.com/merge/tsnform.aspx?c=suntimes&page=mlb/scores/live/scoreboardCT.aspx?date=' + scoreToday;
	window.open(scoreUrl,'score');
}


function cookSelect() {
	var i=0;
	var isChicago = false;

	for (i=0; i < document.form1.Cook.length; i++) {
		if ((document.form1.Cook[i].selected) && (document.form1.Cook[i].value == 'Chicago')) {
			isChicago = true;
		}
	}

	if (isChicago) {
		document.form1.ChicagoCommunities.style.display='block';
	} else {
		document.form1.ChicagoCommunities.style.display='none';
	}
}

function menuSelect() {

	if (document.form1.County.value == 'Cook') {
		document.form1.Cook.style.display='block';
	} else {
		document.form1.Cook.style.display='none';
		document.form1.ChicagoCommunities.style.display='none';
	}
	if (document.form1.County.value == 'DuPage') {
		document.form1.DuPage.style.display='block';
	} else {
		document.form1.DuPage.style.display='none';
	}
	if (document.form1.County.value == 'Kane') {
		document.form1.Kane.style.display='block';
	} else {
		document.form1.Kane.style.display='none';
	}
	if (document.form1.County.value == 'Kendall') {
		document.form1.Kendall.style.display='block';
	} else {
		document.form1.Kendall.style.display='none';
	}
	if (document.form1.County.value == 'Lake') {
		document.form1.Lake.style.display='block';
	} else {
		document.form1.Lake.style.display='none';
	}
	if (document.form1.County.value == 'McHenry') {
		document.form1.McHenry.style.display='block';
	} else {
		document.form1.McHenry.style.display='none';
	}
	if (document.form1.County.value == 'Will') {
		document.form1.Will.style.display='block';
	} else {
		document.form1.Will.style.display='none';
	}
}

function loadPage() {
	document.form1.reset();

	document.form1.ChicagoCommunities.style.display='none';
	document.form1.DuPage.style.display='none';
	document.form1.Kane.style.display='none';
	document.form1.Kendall.style.display='none';
	document.form1.Lake.style.display='none';
	document.form1.McHenry.style.display='none';
	document.form1.Will.style.display='none';
	document.form1.Cook.style.display='none';

	var theLoc = location;
	theLoc = new String(theLoc);
	if (theLoc.indexOf('?zipcode=') > 0) {
		theLoc = theLoc.substring(theLoc.indexOf('?zipcode=')+9,theLoc.lastIndexOf('?zipcode=')+15);
		document.form1.zipcode.value = theLoc;
	}
}

//Email validation

				function mailtrim(mvalue)
				{
						var temp1 = mvalue;
						var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
						if (obj.test(temp1)) { temp1 = temp1.replace(obj, '$2'); }
						var obj = /  /g;
						while (temp1.match(obj)) { temp1 = temp1.replace(obj, " "); }
						return temp1;
						}
				
					function checkMail(inmail)
						{ 
						var x = inmail;
						var y = 0;
						var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
						if (x.indexOf(',') != -1)
						{  
						var seperatevalues = x.split(',');
						var xx=0; 
						for ( xx=0; xx<seperatevalues.length ; xx=xx+1) 
						{ 
						if (filter.test(seperatevalues[xx])) y = y + 1;
						} 
						  
						if (xx == y) return true;
						else return false;
						}
						else
						{
						if (filter.test(x)) return true;
						else return false;
						}
						}		 

					function validate()
						{
						
						 
						var trimtomail = mailtrim(document.mailform.toaddress.value);
						if (checkMail(trimtomail) == false)
						{ alert('Please enter a valid email address in - Recievers Email Address Field'); 
							document.mailform.toaddress.focus();
							return false;
						}
						// validate the from_address 
						var trimfrommail = mailtrim(document.mailform.fromaddress.value);
						if (checkMail(trimfrommail) == false)
						{ alert('Please enter a valid email address in - Your Email Address'); 
						document.mailform.fromaddress.focus();
						return false;
						}	 
						if(document.mailform.body.value=="") 
						{
						document.mailform.configid.value = 1847131
						return true;
						}
							 
						} 
			
	function getPollAction(flag,form,request)
	{
			
		 //var poll=form.qid1.value;
		 //var body=form.body.value;		
		
		/*if(flag=='q') 
		{
		  for (var i=0; i < form.ans1.length; i++)
		  {
			    if (form.ans1[i].checked)
			    {
			       var rad_val = form.ans1[i].value;			     
			       //dc_popup_win(request+"&select="+rad_val+"&no="+i+"&body="+body,'windows','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=500,height=450' );
			       dc_popup_win(request+"?no="+i,'windows','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=500,height=450' );
			     }			  
	   	   }	
	    }*/
	    
	    if(flag=='q')
		{
			var polltest=0;
			for (var i=0; i < form.ans1.length; i++)
			{
				if (form.ans1[i].checked)
				{
					var rad_val = form.ans1[i].value;
					//dc_popup_win(request+"&select="+rad_val+"&no="+i+"&body="+body,'windows','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=500,height=450' );
					dc_popup_win(request+"?no="+i,'windows','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=500,height=450' );
					break;
				}
				else
				{
				if(polltest==form.ans1.length-1) {
				//alert("Testingssss="+polltest+"========"+form.ans1.length);
				resval="res";
				dc_popup_win(request+"?no="+resval,'windows','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=500,height=450' );
				break;
				}
					// alert("Poll Test=="+polltest);
				polltest++;
				}
			}
		}	    
	    if(flag=='r') {	   
	        i="res";	        
	         var rad_val="res";
	         //dc_popup_win(request+"&select="+rad_val+"&no="+i+"&body="+body,'windows','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=500,height=450' );
	         dc_popup_win(request+"?no="+i,'windows','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=500,height=450' );
	   
	      }	  
      //dc_popup_win('"+section+"/"+slug+".poll?pr=view','"+slug+"','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=650,height=650')
				 
	}
	
	function get_radio_value()
	{
	for (var i=0; i < document.orderform.music.length; i++)
	   {
	   if (document.orderform.music[i].checked)
	      {
	      var rad_val = document.orderform.music[i].value;
	      }
	   }
	}


	//For email validation


					
	function mailtrim(mvalue)
	{
		var temp1 = mvalue;
		var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
		if (obj.test(temp1)) { temp1 = temp1.replace(obj, '$2'); }
		var obj = /  /g;
		while (temp1.match(obj)) { temp1 = temp1.replace(obj, " "); }
		return temp1;
	}
				
	function checkMail(inmail)
	{ 
		var x = inmail;
		var y = 0;
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (x.indexOf(',') != -1)
		{  
			var seperatevalues = x.split(',');
			var xx=0; 
			for ( xx=0; xx<seperatevalues.length ; xx=xx+1) 
			{ 
				if (filter.test(seperatevalues[xx])) y = y + 1;
			} 
						  
			if (xx == y) return true;
			else return false;
		}
		else
		{
			if (filter.test(x)) return true;
			else return false;
		}
	}		 

	function validate(formname)
	{
		var trimtomail = mailtrim(formname.a_recieveemail.value);
		var trimfrommail = mailtrim(formname.a_fromuser.value);
		var comment = formname.a_comment.value;
		
		if(trimtomail == "" && trimfrommail == "" && comment == ""){
		alert("All the text fields are mandatory.");
		 return false;
		}else{				
				if (checkMail(trimtomail) == false)
				{ alert('Please enter a valid email address in Colleagues Field');  
				formname.a_recieveemail.focus();
				return false;
				}
				// validate the from_address 
				if (checkMail(trimfrommail) == false)
				{ alert('Please enter a valid email address in Your\'s Email Field');  
				formname.a_fromuser.focus();
				return false;
				}	
				else {
				   var ma=trimfrommail.split(",");	   
				   if(ma[1]!=null) {		   
				      alert('Please enter only one email address in Your\'s Email Field');  
				      formname.a_fromuser.focus();
				      return false;
				   }
				} 
				if(formname.a_comment.value=="") 
				{
				alert('Please enter a message'); 
				  formname.a_comment.focus();
				  return false;
				}
				else if(formname.a_comment.value.length>400)
				{
				    alert('Your message must be under 400 characters');
				    formname.a_comment.focus();
				    return false;
				}
				else{
				formname.submit();
				}
		
	     }					 
	} 
					
	

