$(document).ready(
						function(){
							$(".check_clear_all").click(function()
							{
									var checked_status = this.checked;
									$("input[@name='" + $(this).attr('rel') + "']").each(function()
									{this.checked = checked_status;});
							});	
							
							$(".open_close").click(function()
							{
								
									if(this.checked) 
										$('#'+ $(this).attr('rel')).show(); 
									else 
										$('#'+ $(this).attr('rel')).hide();
							});	
										
							$('.merchantlist').hover(
								function() {
									who = this.name;
									$(this).css('background','#AAAA99');
									$('#' + who).css('background','#99AAAA');
										},
								function() {
									who = this.name;
									$(this).css('background','#ffffff');
									$('#' + who).css('background','#ffffff');
									}
							);

							$('.jsalist').hover(
								function() {
									who = this.name;
									$(this).css('background','#AAAA99');
									$('#' + who).css('background','#99AAAA');
										},
								function() {
									who = this.name;
									$(this).css('background','#ffffff');
									$('#' + who).css('background','#ffffff');
									}
							);							

							$('#page').click(function(event) {
								if (event.target.id != 'search' && event.target.id != 'sbi' && event.target.id != 'sbb' && event.target.id != 'searchlink') {
									if ($('#search').css('display') != "none") {
										$('#search').hide();
										$('#search').width('0px');
										$('#search').height('0px');
									}
								}
							});
							$('#search').hide();
							$('#searchlink').click(
								function() {
									if ($('#search').css('display') != "none") {
										$('#search').hide();
										$('#search').width('0px');
										$('#search').height('0px');
									}
									else {
										$('#search').show();
										$('#search').animate({height: '20px', width: '180px'},'fast');
										$('#search #sbi').focus();
									}
								});


							$('.linkbox').hover(
																			function() {
																				$(this).css('background','#E7FFCF');
         												},
																			function() {
																				$(this).css('background','#EFFFDF');
																			}
																			);
							$('.active-input').hover(
																			function() {
																				$(this).css('background','blue');
																				$(this).css('color','white');
         												},
																			function() {
																				$(this).css('background','white');
																				$(this).css('color','black');
																			}
																			);
							$('.disabled-input').hover(
																			function() {
																				$(this).css('background','blue');
																				$(this).css('color','white');
         												},
																			function() {
																				$(this).css('background','white');
																				$(this).css('color','gray');
																			}
																			);

							//DII ? $('.err').Highlight(1000, 'red');
							$('.cnf').Highlight(1000, 'green');
							$('.highlighted').css('background','gold').Highlight(1500, 'white');

							$('.highlighted').hover(
																			function() {
																				$(this).show();
																				$(this).css('background','#FFF8AF');
																				$(this).css('opacity',1);
         												},

																			function() {
																				$(this).css('background','gold');
																			}
																			);

							$('.highlighted').click(
																			function() {
																				$(this).show();
																				$(this).css('background','#FFF8AF');
																				$(this).css('opacity',1);
         												}
																			);

							$('#address').click( function() { showAddressForm(); } );
//DII  									$('#contact').click( function() { showContactForm(); } );
							$('#about').click( function() { showAboutForm(); } );
//  									$('#coupon0').click( function() { showCouponForm(0); } );
//  									$('#coupon1').click( function() { showCouponForm(1); } );
//  									$('#coupon2').click( function() { showCouponForm(2); } );

							$('#logo').click( function() { showLogoForm(); } );
							$('#image').click( function() { showImageForm(); } );
							$('#video').click( function() { showVideoForm(); } );
							$('#paytypes').click( function() { showPaytypesForm(); } );
							$('#category').click( function() { showCategoryForm(); } );

//DII  									$('#slug').click( function() { showSlugForm(); } );
//DII
							$('.newforms').hover(
										function() {$(this).show(); $(this).css('background','#FFFFEF');$(this).css('opacity',1);},
										function() {$(this).css('background','white');}      );
																			
							$('#hours').click( function() { showHoursForm(); } );

							$('.field-block').hover(
																			function() {
																				$(this).show();
																				$(this).css('background','#FFF8AF');
																				$(this).css('opacity',1);
         												},
																			function() {
																				$(this).css('background','white');
																			}
																			);
							$("select#cat").change(function(){
									vars = 'cat=' + $('#cat').attr('value');
									vars += '&action=get_subcats';
									$.get("/ajax.php?"+vars,
												function(data) {
                   								 $("select#subc").html(data);
									});
							});
						}
						);
						
//checks to see if a particular value out of multiple checkboxes with same id is selected
function isChecked(theid,myval) {
       var mymatch=false;
       alert ("myval");
       $("#you:checked").each(function () {
	       alert ("myval2");
	    	if($(this).val() == "myval") { mymatch=true;}
	    });
		return mymatch;
	}

// Send reminder
function sendReminder(mid) {
		vars = 'mid='+mid;
		vars += '&remtemplate=' + escape($('#remtemplate-'+mid).attr('value'));
		$.get("/ajax.php?action=send_reminder&" + vars,
		  function(data) {
			  $("#reminder-feedback-"+mid).show().html(data).Highlight(1500, 'green');
		});
}

function recipientsShow() {
		$("#recipients-list").show();
		$("#recipients-toggle").html('<a href="javascript:recipientsHide();" >Hide Recipients</a>');
}

function recipientsHide() {
		$("#recipients-list").hide();
		$("#recipients-toggle").html('<a href="javascript:recipientsShow();" >Show Recipients</a>');
}

function refreshPage() {
	setTimeout( "window.location.href = unescape(window.location.pathname);", 1 * 1000 );
}



function submitLogoForm() {
		vars = 'mid=' + $('#mid').attr('value');
		vars += '&logo=' + $('#logo').attr('value');
		$.get("ajax.php?action=update_logo&" + vars,
					function(data) {
				      $("#logo").attr('class', 'field-block');
				      $("#logo-form").hide();
				      $("#logo-display").show().html(data);
							$('#logo-display').Highlight(1500, 'green');

		});

}

function showLogoForm() {
		document.getElementById('logo-display').style.display = 'none';
		document.getElementById('logo-form').style.display = 'block';
}

function hideLogoForm() {
		document.getElementById('logo-display').style.display = 'block';
		document.getElementById('logo-form').style.display = 'none';
}

function showImageForm(existing_img) {
		document.getElementById('image-display').style.display = 'none';
		document.getElementById('image-form').style.display = 'block';
}

function hideImageForm() {
		document.getElementById('image-display').style.display = 'block';
		document.getElementById('image-form').style.display = 'none';
}

function showVideoForm() {
		document.getElementById('video-display').style.display = 'none';
		document.getElementById('video-form').style.display = 'block';
}

function hideVideoForm() {
		document.getElementById('video-display').style.display = 'block';
		document.getElementById('video-form').style.display = 'none';
}

function initcheckvalid(a, b, c){
  sfirst= a;
  ssecond= b;
  sthird= c;
}

function checkvalid(selectlist){
  var sel = document.getElementById('merch'+selectlist);
	var choise= sel.options[sel.selectedIndex].value;

	if (choise == 0){
	    if (sfirst == selectlist) sfirst= 0;
	    if (ssecond == selectlist) ssecond= 0;
	    if (sthird == selectlist) sthird= 0;
	}

	if (choise == 1){
	    if(ssecond == selectlist) ssecond= 0;
	    if(sthird == selectlist) sthird= 0;
	    if (sfirst != 0){
		    alert('You can choose only one business for first place');
			sel.selectedIndex= 0;
		}
		else{
		    sfirst= selectlist;
		}
	}

	if (choise == 2){
	    if(sfirst == selectlist) sfirst= 0;
	    if(sthird == selectlist) sthird= 0;
	    if (ssecond != 0){
		    alert('You can choose only one business for second place');
			sel.selectedIndex= 0;
		}
		else{
		    ssecond= selectlist;
		}
	}

	if (choise == 3){
	    if(ssecond == selectlist) ssecond= 0;
	    if(sfirst == selectlist) sfirst= 0;
	    if (sthird != 0){
		    alert('You can choose only one business for third place');
			sel.selectedIndex= 0;
		}
		else{
		    sthird= selectlist;
		}
	}
}
function pageXY( el) {
    var XY={x:0, y:0};

    for( var node = el; node; node=node.offsetParent)
	{ 
		XY.x += node.offsetLeft;
		XY.y += node.offsetTop;
	}

    return XY;

}

function handleMouseMove(e) {
    if (!e) {
	e = window.event;
	mouseX = e.clientX + document.body.scrollLeft;
	mouseY = e.clientY + document.body.scrollTop;
    } else {
	mouseX = e.pageX;
	mouseY = e.pageY;
    }
    width = 112;
    height = 112;
    b1 = document.getElementById('button1');
    if (mouseX >= pageXY(b1).x && mouseX <= (pageXY(b1).x + width) && mouseY >= pageXY(b1).y && mouseY <= (pageXY(b1).y + height)) {
		document.getElementById('hover1').style.display = "block";
    } else {
		document.getElementById('hover1').style.display = "none";
    }
    b2 = document.getElementById('button2');
    if (mouseX >= pageXY(b2).x && mouseX <= (pageXY(b2).x + width) && mouseY >= pageXY(b2).y && mouseY <= (pageXY(b2).y + height)) {
		document.getElementById('hover2').style.display = "block";
    } else {
		document.getElementById('hover2').style.display = "none";
    }

    b3 = document.getElementById('button3');
    if (mouseX >= pageXY(b3).x && mouseX <= (pageXY(b3).x + width) && mouseY >= pageXY(b3).y && mouseY <= (pageXY(b3).y + height)) {
		document.getElementById('hover3').style.display = "block";
    } else {
		document.getElementById('hover3').style.display = "none";
    }

    b4 = document.getElementById('button4');
    if (mouseX >= pageXY(b4).x && mouseX <= (pageXY(b4).x + width) && mouseY >= pageXY(b4).y && mouseY <= (pageXY(b4).y + height)) {
		document.getElementById('hover4').style.display = "block";
    } else {
		document.getElementById('hover4').style.display = "none";
    }

}

function confirmDelete(gotopath) {
		var mid = $('#mid').attr('value');
    var c = confirm('Are you sure you want to DELETE this?');
    if (!c) return false;
    document.location= gotopath;

}

function getCoupon(phoneid, cid) {
    phone = document.getElementById(phoneid+'-'+cid).value;
    carrier = document.getElementById('C'+ phoneid +'-'+cid).value;
	if(phone != "xxx-xxx-xxxx")
		openWin('/process.php?action=get_coupon2&carrier='+carrier+'&cid='+cid+'&phone='+phone, 425, 510); //DII was 467
	else
		alert('Please, enter Your phone number.');

}

function signmeup(mid) {
//	openWin('/signmeup.php?mid='+formObj.mid.value+'&phone='+formObj.phone.value+'&subscribe_merchant='+formObj.subscribe_merchant.checked+'&jabbertise_loyal='+formObj.jabbertise_loyal.checked+'&coupon_reminder_service='+formObj.coupon_reminder_service.checked, 550, 800);
	openWin('/signmeup.php?mid='+mid, 550, 700);
}

function orgsignmeup(mid) {
	openWinScroll('/orgsignmeup.php?mid='+mid, 550, 700);
}

function couponFull(cid) {
    openWin('/coupon-confirm.php?cid='+cid, 425, 510); 
}

function favoriteAdd(mid) {
    openWinScroll('/favorites.php?add='+mid, 425, 510); 
}

function toggle(objId) {

	var showBlock = document.getElementById(objId).style.display;
	if (showBlock == 'none') {
		document.getElementById(objId).style.display = 'block';
	}
	else {
		document.getElementById(objId).style.display = 'none';
	}
}

function categoryMouseOver(catid) {
    el = "cat_" + catid;
    cat = document.getElementById(el);
    cat.style.backgroundColor = 'blue';
    cat.style.color = 'white';
}



function categoryMouseOut(catid) {
    el = "cat_" + catid;
    cat = document.getElementById(el);
    cat.style.backgroundColor = 'white';
    cat.style.color = 'black';
}

function subcatMouseOver(sid) {
    el = "subcat_" + sid;
    s = document.getElementById(el);
    s.style.backgroundColor = 'blue';
    s.style.color = 'white';
}

function subcatMouseOut(sid) {
    el = "subcat_" + sid;
    s = document.getElementById(el);
    s.style.backgroundColor = 'white';
    s.style.color = 'black';
}


// Open windows

var width = 550;
var height = 450;
var newWindow;

function openWinForced (fileName, width, height) {
    params = 'width='+width+', height='+height+', directories=no, location=no, menubar=no, scrollbars=auto, status=no, toolbar=no, resizable=yes';
		newWindow = window.open(fileName,"newwin2",params);
}

function openWin (fileName, width, height) {
	if ( !newWindow || newWindow.closed)
	{
    params = 'width='+width+', height='+height+', directories=no, location=no, menubar=no, scrollbars=auto, status=no, toolbar=no, resizable=yes';
		newWindow = window.open(fileName,"newwin",params);
	}
	else
	{
		// window is already open, bring it to front
		newWindow.resizeTo(width,height);
		newWindow.location = fileName;
		newWindow.focus();
	}
}

function openWinScroll (fileName, width, height) {
	if (!newWindow || newWindow.closed)
	{
    params = 'width='+width+', height='+height+', directories=no, location=no, menubar=no, scrollbars=yes, status=no, toolbar=no, resizable=yes';
		newWindow = window.open(fileName,"newwin",params);
	}
	else
	{
		// window is already open, bring it to front
		newWindow.resizeTo(width,height);
		newWindow.location = fileName;
		newWindow.focus();
	}
}


// AJAX object

function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;

}

// Instantiate the ajax object
var http = getHTTPObject();

//DII - to display only one subcategory (at most) - that's the one that is opened
function changeOpenCategory(catid) {
	if  (catid) {
		$('#subcats').hide();
		$('#categories').show();
		$.get('ajax.php', {'action':'catopen', 'catid':catid}, function(data) {
			$('#cat_list').html(data);
		});
	}
//DII todo	ajaxDeal2Cat(catid,0);
}

function seeCuponsByCategory(catid, catname,crtopencat) {
	if  (catid) {

		$('#subcats .title').html('<h2> &nbsp; '+catname+' <span class="normal" align="right">[ <a href="/browse.php" onClick="changeOpenCategory('+crtopencat+');return false;">Back</a> ]</span></h2>');

		$.get('ajax.php', {'action':'cuponsbycat', 'catid':catid}, function(data) { //DII was subcats
			$('#subcat_list').html(data);
			$('#categories').hide();
			$('#subcats').show();
		});
	}
	else {
		$('#subcats').hide();
		$('#categories').show();
	}
//DII todo	ajaxDeal2Cat(catid,0);
}

function seeCuponsBySubcategory(subcatid, subcatname,crtopencat) {
	if  (subcatid) {

		$('#subcats .title').html('<h2> &nbsp; '+subcatname+' <span class="normal">[ <a href="/browse.php" onClick="changeOpenCategory('+crtopencat+');return false;">Back</a> ]</span></h2>');

		$.get('ajax.php', {'action':'cuponsbysubcat', 'subcatid':subcatid}, function(data) {
			$('#subcat_list').html(data);
			$('#categories').hide();
			$('#subcats').show();
		});
	}
	else {
		$('#subcats').hide();
		$('#categories').show();
	}
//TODO	ajaxDeal2Cat(catid,0);
}

// Connect to ajax vast 2.0 processor

function ajaxChangeCategory() {
    catid = document.getElementById('category').value;
    http.open("GET", '/ajax.php?action=subcats&catid='+catid, true);
    http.onreadystatechange = handleRestaurantsHttpResponse;
    http.send(null);
		ajaxDeal2Cat(catid,0);
//		ajaxMerchant2Cat(catid);

}

function handleRestaurantsHttpResponse() {
    if (http.readyState == 4) {
	results = http.responseText;
	$('#subcats').text(results);
//	document.getElementById('subcats').innerHTML = results;
    }
}



// Connect to ajax vast 2.0 processor

function ajaxChangeSubcat(zip) {
    sid = document.getElementById('subcat').value;
    http.open("GET", '/ajax.php?action=merchants&sid='+sid, true);
    http.onreadystatechange = handleSubcatsHttpResponse;
    http.send(null);
	ajaxDeal2Cat(0,sid);
//	ajaxMerchant2SubCat(sid);
}

function handleSubcatsHttpResponse() {
    if (http.readyState == 4) {
	results = http.responseText;
	document.getElementById('merchants').innerHTML = results;
    }
}

// Change merchant
function changeMerchant(mid) {
    document.location = "/"+mid;
}

// Hide/Show Scheduled sms form
function scheduledSms(visib, disp) {
	myForm = document.getElementById('scheduledsms_form');
	myForm.style.visibility = visib;
	myForm.style.display = disp;
	myForm2 = document.getElementById("recurringsms_form");
	myForm2.style.visibility = 'hidden';
	myForm2.style.display = 'none';
}



// Reocurring sms form
function reocurringSms() {
	myForm = document.getElementById('scheduledsms_form');
	myForm.style.visibility = 'hidden';
	myForm.style.display = 'none';
	myForm2 = document.getElementById("recurringsms_form");
	myForm2.style.visibility = 'visible';
	myForm2.style.display = 'block';
}


// Get merchants per category
function ajaxDeal2Cat(cat_id,subcat_id) {

	var http2 = getHTTPObject();
	if (subcat_id > 0){
		http2.open("GET", '/ajax.php?action=deal2cat&subcatid='+subcat_id, true);
	} else {
		http2.open("GET", '/ajax.php?action=deal2cat&catid='+cat_id, true);
	}

	http2.onreadystatechange = function() {
		if (http2.readyState == 4) {
			results = http2.responseText;
			//alert(results);
			if (results != "") {
				document.getElementById('top_deals').innerHTML = results;
			}
	    }
	}
	http2.send(null);
}

// Get merchants per category
function ajaxMerchant2Cat(cat_id) {
	var http2 = getHTTPObject();
	http2.open("GET", '/ajax.php?action=merchant2cat&catid='+cat_id, true);
	http2.onreadystatechange = function() {
		if (http2.readyState == 4) {
			results = http2.responseText;
			//alert(results);
			if (results != "") {
				document.getElementById('topmerchants').innerHTML = results;
			}
 	   }
	}
	http2.send(null);
}

// Get merchants per subcat
function ajaxMerchant2SubCat(sid) {
	var http2 = getHTTPObject();
	http2.open("GET", '/ajax.php?action=merchant2subcat&sid='+sid, true);
	http2.onreadystatechange = function() {
		if (http2.readyState == 4) {
			results = http2.responseText;
			//alert(results);
			if (results != "") {
				document.getElementById('topmerchants').innerHTML = results;
			}
    }
	}
	http2.send(null);
}

function topMerchant2Cat(dropdown, value) {
	if (value == 1) {
		if (firstOpt != "") {
			document.getElementById(firstOpt).selectedIndex = 0;
		}
		firstOpt = dropdown;
	}
	if (value == 2) {
		if (secondOpt != "") {
			document.getElementById(secondOpt).selectedIndex = 0;
		}
		secondOpt = dropdown;
	}
	if (value == 3) {
		if (thirdOpt != "") {
			document.getElementById(thirdOpt).selectedIndex = 0;
		}
		thirdOpt = dropdown;
	}
}



function saveTopMerchant2Cat() {
	document.fTopMerch2Cat.top_first.value = firstOpt;
	document.fTopMerch2Cat.top_second.value = secondOpt;
	document.fTopMerch2Cat.top_third.value = thirdOpt;
}



function topMerchant2SubCat(dropdown, value) {
	if (value == 1) {
		if (firstOpt != "") {
			document.getElementById(firstOpt).selectedIndex = 0;
		}
		firstOpt = dropdown;
	}
	if (value == 2) {
		if (secondOpt != "") {
			document.getElementById(secondOpt).selectedIndex = 0;
		}
		secondOpt = dropdown;
	}
	if (value == 3) {
		if (thirdOpt != "") {
			document.getElementById(thirdOpt).selectedIndex = 0;
		}
		thirdOpt = dropdown;
	}
}

function saveTopMerchant2SubCat() {
	document.fTopMerch2SubCat.top_first.value = firstOpt;
	document.fTopMerch2SubCat.top_second.value = secondOpt;
	document.fTopMerch2SubCat.top_third.value = thirdOpt;

}

function htmlspecialchars(string, quote_style) {
    // http://kevin.vanzonneveld.net
    // +   original by: Mirek Slugen
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Nathan
    // +   bugfixed by: Arno    
    if(string == undefined)
    	string = "";
    string = string.toString();
    
    // Always encode
    string = string.replace(/&/g, '&amp;');
    string = string.replace(/</g, '&lt;');
    string = string.replace(/>/g, '&gt;');
    
    // Encode depending on quote_style
    if (quote_style == 'ENT_QUOTES') {
        string = string.replace(/"/g, '&quot;');
        string = string.replace(/'/g, '&#039;');
    } else if (quote_style != 'ENT_NOQUOTES') {
        // All other cases (ENT_COMPAT, default, but not ENT_NOQUOTES)
        string = string.replace(/"/g, '&quot;');
    }
    
    return string;
}

function numbersonly(myfield, e, slash)
{	
	var key;
	var keychar;
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	
	// control keys
	if ((key==null) || (key==0) || (key==8) || 
	    (key==9) || (key==13) || (key==27) )
	   return true;
	
	// numbers
	else if ((("0123456789-").indexOf(keychar) > -1))
	   return true;
	
	// decimal point jump
	else if (slash && (keychar == "/"))
	   return true;
	else
	   return false;
}


var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;

// open hidden layer
function mopen(id)
{	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';

}
// close showed layer
function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// close layer when click-out
document.onclick = mclose; 