function toggle_category_alert(id){
	if (!eval('document.alertform.catsel'+id).checked){
		var currentval=document.alertform.category_alerts.value;
		var newval=currentval.replace(id+',','');
		document.alertform.category_alerts.value=newval;
		currentval=document.getElementById('curcatalrt').innerHTML;
		newval=currentval.replace(document.getElementById('cat'+id).innerHTML+', ','');
		document.getElementById('curcatalrt').innerHTML=newval;
		if (!document.getElementById('curcatalrt').innerHTML){
			document.getElementById('curcatalrt').innerHTML='<EM>No alerts have been selected.</EM>';
		}
	}
	else{
		if (document.getElementById('curcatalrt').innerHTML=='<EM>No alerts have been selected.</EM>'){
			document.getElementById('curcatalrt').innerHTML='';
		}
		document.getElementById('curcatalrt').innerHTML+=document.getElementById('cat'+id).innerHTML+', ';
		document.alertform.category_alerts.value+=id+',';
	}
}
function toggle_store_alert(id){
	if (!eval('document.alertform.storesel'+id).checked){
		var currentval=document.alertform.store_alerts.value;
		var newval=currentval.replace(id+',','');
		document.alertform.store_alerts.value=newval;
		currentval=document.getElementById('curstralrt').innerHTML;
		newval=currentval.replace(document.getElementById('store'+id).innerHTML+', ','');
		document.getElementById('curstralrt').innerHTML=newval;
		if (!document.getElementById('curstralrt').innerHTML){
			document.getElementById('curstralrt').innerHTML='<EM>No alerts have been selected.</EM>';
		}
	}
	else{
		if (document.getElementById('curstralrt').innerHTML=='<EM>No alerts have been selected.</EM>'){
			document.getElementById('curstralrt').innerHTML='';
		}
		document.getElementById('curstralrt').innerHTML+=document.getElementById('store'+id).innerHTML+', ';
		document.alertform.store_alerts.value+=id+',';
	}
}
function toggletip(id){
	// Get the text to use for the tip
	if (id=='byct'){
		var tiptext='Here are a few popular shopping departments. Click on a category to browse our best deals. Or click <a href="/all_categories">more categories</a> to find what you\'re looking for.<a href="Javascript: toggletip(\'byct\');">close tip</a>';
	}
	else if (id=='bt'){
		var tiptext='We\'ve listed a few of our most popular stores. Click on a store to see what coupons are available. Or click <a href="/all_merchants">all stores</a> to find your favorite store.<a href="Javascript: toggletip(\'bt\');">close tip</a>';
	}

	if (document.getElementById(id).className=='shti'){
		document.getElementById(id).className='dontshti';
	}
	else {
		document.getElementById(id).innerHTML=tiptext;
		document.getElementById(id).className='shti'
	}
}
function addComment(id){
  	url="/popup.php?c="+id+"&comment=1";
  
  	openCenteredOnOpenerWindow(url,'d',602,368,'',0,'resizable=no');
  
}
// Core utility function that actually creates the window and gives focus to it
function openWindow(url, name, properties, openerName) {

	// ie4.x pc can't give focus to windows containing documents from a different domain
	// in this case, initially load a local interstisial page to allow focussing before loading final url
	var agent = navigator.userAgent.toLowerCase();
	if (agent.indexOf("msie") != -1 && parseInt(navigator.appVersion) == 4 && agent.indexOf("msie 5") == -1 && agent.indexOf("msie5") == -1 && agent.indexOf("win") != -1 && url.indexOf('http://') == 0) {
		winReference = window.open('about:blank', name, properties);
		
		setTimeout('if (winReference && !winReference.closed) winReference.location.replace("' + url + '")', 300);
	}
	else {
		winReference = window.open(url, name, properties);
	}

	// ie doesn't like giving focus immediately (to new window in 4.5 on mac; to existing ones in 5 on pc)
	setTimeout('if (winReference && !winReference.closed) winReference.focus()', 200);
	
	if (openerName) self.name = openerName;
	return winReference;
}


// Open a window at a given position on the screen
function openPositionedWindow(url, name, width, height, x, y, status, scrollbars, moreProperties, openerName) {
	
	// ie 4.5 and 5.0 mac - windows are 2 pixels too short; if a statusbar is used, the window will be an additional 15 pixels short
	var agent = navigator.userAgent.toLowerCase();
	if (agent.indexOf("mac") != -1 && agent.indexOf("msie") != -1 && (agent.indexOf("msie 4") != -1 || agent.indexOf("msie 5.0") != -1) ) {
		height += (status) ? 17 : 2;
	}

	// Adjust width if scrollbars are used (pc places scrollbars inside the content area; mac outside) 
	width += (scrollbars != '' && scrollbars != null && agent.indexOf("mac") == -1) ? 16 : 0;

	var properties = 'width=' + width + ',height=' + height + ',screenX=' + x + ',screenY=' + y + ',left=' + x + ',top=' + y + ((status) ? ',status' : '') + ',scrollbars' + ((scrollbars) ? '' : '=no') + ((moreProperties) ? ',' + moreProperties : '');
	var reference = openWindow(url, name, properties, openerName);
	
	// resize window in ie if we can resize in ns; very messy
	// commented out because openPositionedWindow() doesn't set the resizable attribute
	// left in for reference
	/*if (resizable && agent.indexOf("msie") != -1) {
		if (agent.indexOf("mac") != -1) {
			height += (status) ? 15 : 2;
			if (parseFloat(navigator.appVersion) > 5) width -= 11;
		}
		else {
			height += (status) ? 49 : 31;
			width += 13;
		}
		setTimeout('if (reference != null && !reference.closed) reference.resizeTo(' + width + ',' + height + ');', 150);
	}*/

	return reference;
}

// Open a window at the center of the screen
function openCenteredWindow(url, name, width, height, status, scrollbars, moreProperties, openerName) {
	var x, y = 0;
	if (screen) {
      x = (screen.availWidth - width) / 2;
	   y = (screen.availHeight - height) / 2;
   }
	if (!status) status = '';
	if (!openerName) openerName = '';
	var reference = openPositionedWindow(url, name, width, height, x, y, status, scrollbars, moreProperties, openerName);
	return reference;
}	

// Open a window at the center of the parent window
function openCenteredOnOpenerWindow(url, name, width, height, status, scrollbars, moreProperties, openerName) {
	var centerX = 0;
   var centerY = 0;
   if (window.screenX != null && window.outerWidth) {
      centerX = window.screenX + (window.outerWidth / 2);
      centerY = window.screenY + (window.outerHeight / 2);
   }
   else if (window.screenLeft) {
      if (document.documentElement) {
         centerX = window.screenLeft + (document.documentElement.offsetWidth / 2);
         centerY = window.screenTop + (document.documentElement.offsetHeight / 2);
      }
      else if (document.body && document.body.offsetWidth) {
         centerX = window.screenLeft + (document.body.offsetWidth / 2);
         centerY = window.screenTop + (document.body.offsetHeight / 2);
      }
   }
   
   if (centerX == 0) {
      openCenteredWindow(url, name, width, height, status, scrollbars, moreProperties, openerName);
   }
   var x = parseInt(centerX - (width / 2));
   var y = parseInt(centerY - (height / 2));
	if (!status) status = '';
	if (!openerName) openerName = '';
	var reference = openPositionedWindow(url, name, width, height, x, y, status, scrollbars, moreProperties, openerName);
	return reference;
}	


function showHelp(){
window.open('http://www.Coupons.com/help_wizard.php','e','resizable=no,scrollable=no,height=500,width=500');
return 0;
}
function submitEmail(){
if(emailCheck(document.emailform.email.value)) document.emailform.submit();
}
function submitSearch(){
if(searchCheck(document.searchform.query.value)) document.searchform.submit();
}
function submitSearchbottom(){
if(searchCheck(document.searchformbottom.query.value)) document.searchformbottom.submit();
}

function searchCheck(query){
if (!query || query=='Search for Coupons' || query=='Enter Search') {
    // empty query
    alert("Please enter a search term (ie Dell Coupons).")
    return false
}
return true;
}

function clearText(){
if (document.searchform.query.value == document.searchform.defaultText.value){
document.searchform.query.value = "";}
}

function clearTextbottom(){
if (document.searchformbottom.query.value == document.searchformbottom.defaultText.value){
document.searchformbottom.query.value = "";}
}
function pausecomp(millis) 
{
date = new Date();
var curDate = null;

do { var curDate = new Date(); } 
while(curDate-date < millis);
} 
function sendFeedCoupon(merchant_id,url){
	document.location.href="/send.php?m="+merchant_id+"&url="+url;
	return false;
}
function addEmailAlert(merchant_id){
	if(!merchant_id){
		return false;
	}
	else{
	document.location.href="/mystchief/emailalerts.php?add_mid="+merchant_id;
	return false;
	}
}
function sendCoupon(coupon_id, merchant_id)
{
  if(coupon_id!='0' && coupon_id!=0){
  url="/popup.php?c="+coupon_id;
  win=window.open(url,'d','resizable=no,scrollable=no,height=368,width=602');
  document.location.href="/send.php?c="+coupon_id;
  }
  else if(merchant_id!='0' && merchant_id!=0){
  document.location.href="/send.php?m="+merchant_id;
  }
  return false;
}

function enlargeImage(imagefile, x, y)
{
  url="/images/products/"+imagefile;
  window.open(url,'d','resizable=yes,scrollable=no,height='+y+',width='+x);
  return false;
}

function clearEmail(){
if (document.emailform.email.value == document.emailform.emaildefault.value){
document.emailform.email.value = "";}}

function addBookmark(title,url) {
if (window.sidebar) {
window.sidebar.addPanel(title, url,"");
} else if( document.all ) {
window.external.AddFavorite( url, title);
} else if( window.opera && window.print ) {
return true;
}
}

function emailCheck (emailStr) {
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
	alert("Please enter and valid E-Mail address.")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

if (user.match(userPat)==null) {
    // user is not valid
    alert("Please enter and valid E-Mail address.")
    return false
}

var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Please enter and valid E-Mail address.")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("Please enter and valid E-Mail address.")
    return false
}

var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 ||
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   alert("Please enter and valid E-Mail address.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="Please enter and valid E-Mail address."
   alert(errStr)
   return false
}

// If we've gotten this far, everything's valid!
return true;
}
