/* Shows the error dialog and sets the focus */

function WriteErr(errstr, focus) {
  if (errstr != '') {
    alert("Error: \n\n" + errstr);
  }

  if (focus != '') {
    document.forms['form'].elements[focus].focus();
  }
}

/* Submits the form */

function SubmitForm(button) {
  frm = document.forms['form'];
  if (button != '') {
    frm[button].value = '1';
  }
  frm.submit();
}

function PopUp(url, width, height) {
	winW = width;
	winH = height;
	winL = screen.width / 2 - winW / 2;
	winT = screen.height / 2 - winH / 2;
	window.open(url,"_blank","width="+width+",height="+height+",top="+winT+",left="+winL+",scrollbars=no,status=no,toolbar=no,menubar=no,location=no");
}

function CheckAvailability(id,state,location,ch) {
	var url='';
	url = '/CheckAvailability.asp?id_advertiser='+id+'&id_state='+state+'&id_location='+location+'&char='+ch;
	PopUp(url, 600, 500);
}

function ChangePage(page, item) {
	document.formular.Page.value=page;
	document.formular.submit();
}

function SendForm(item) {
	var state = document.formular.id_state.value;
	var region = document.formular.id_Locality.options[document.formular.id_Locality.selectedIndex].innerHTML;
	region = escape(region);
	
	if (item.id == 'id_state') {
		document.formular.Page.value=1;
		document.formular.id_Locality.value='*';
		document.formular.action += '?id_State=' + state;
	}else{
	    document.formular.action += '?id_State=' + state + '&Region='+ region;
	}
	document.formular.submit();
}

function Sort(number) {
	document.formular.SortColumn.value = number;
	document.formular.submit();
}

function SwitchImage(id, direction) {
	var img = document.getElementById(id);
	var newId = 0;
	
	if (direction > 0)
		newId = img.getAttribute('next');
	if (direction < 0)
		newId = img.getAttribute('previous');
 	if (newId > 0) {
		img.setAttribute('src', Photos[newId].src);
		img.setAttribute('next', Photos[newId].next);
		img.setAttribute('previous', Photos[newId].previous);
	}
}

function DetailImage(photo) {
	var img = document.getElementById(photo);
	var url = '/detailPhoto.asp?photo='+img.src;
	
	PopUp(url,400,400);
}

function SortPage() {
	var sortBy = document.formular.sortBy.value;
	if (sortBy == 0) {
		document.formular.sortBy.value = 1;
	}
	else {
		document.formular.sortBy.value = 0;
	}
	document.formular.submit();
}
function SubmitByEnter(e){
    var keycode = e.keyCode;
    var ENTERKEYCODE = 13; 
    if(keycode == ENTERKEYCODE) {
        var txtSearchVal = document.formular.txtSearch.value;
        location.href = "Search.asp?Search=" + txtSearchVal;
    }
    
}