var isDOM=document.getElementById?1:0;
var isIE=document.all?1:0;
var isNS4=navigator.appName=='Netscape'&&!isDOM?1:0;

function getRef(i, p) {
	p = !p ? document : p.navigator ? p.document:p;
	return isIE ? p.all[i]:isDOM ? (p.getElementById ? p : p.ownerDocument ).getElementById(i) : isNS4 ? p.layers[i] : null;
}

function goBack() {
		window.history.back();
}

var passminlen = 6;
var datesep = '-';
var timesep = ':';

function validate(field, type, name) {
	switch (type) {
		case 'string':
			if (field.value == '') {
				alert(name);
				field.focus();
				return false;
			}
			break;

		case 'email':
			if ((field.value.indexOf('@') == -1 
					|| field.value.indexOf('.') == -1 
					|| field.value.indexOf('.', field.value.indexOf('@')) <= field.value.indexOf('@')+1
					|| field.value.length < 6)
					&& field.value != 'admin' ) {
				
				alert(name);
				field.focus();
				return false;
			}
			break;

		case 'password':
			if (field.value == ''
					|| (field.value.length < passminlen && field.value != 'admin')) {
				alert(name);
				field.focus();
				return false;
			}
			break;

		case 'number':
			for (var x=0; x<field.value.length; x++) {
				var ch = field.value.charAt(x);

				if (( ch < '0' || ch > '9' ) && ch != '.') {
					alert(name);
					field.focus();
					return false; 
				}
			}
			break;

		case 'non0number':
			if (field.value == '' || Number(field.value) == 0) {
				alert(name);
				field.focus();
				return false;
			}

			for (var x=0; x<field.value.length; x++) {
				var ch = field.value.charAt(x);
				
				if (( ch < '0' || ch > '9' ) && ch != '.') {
					alert(name);
					field.focus();
					return false; 
				}
			}
			break;

		case 'date_mdy':
			var d = new Date(field.value.substr(6, 4)
						, Number(field.value.substr(0, 2))-1
						, field.value.substr(3, 2));
			
			var d2 = ((d.getMonth() < 9)?'0':'')+Number(d.getMonth()+1).toString()+datesep
						+((d.getDate() < 10)?'0':'')+Number(d.getDate()).toString()+datesep
						+Number(d.getFullYear()).toString();

			if ((field.value != d2 
					|| field.value.charAt(2) != datesep
					|| field.value.charAt(5) != datesep
					|| field.value.length != 10) && field.value != '') {
				alert(name);
				field.focus();
				return false; 
			}
			break;

		case 'date_dmy':
			var d = new Date(field.value.substr(6, 4)
												, Number(field.value.substr(3, 2))-1
												, field.value.substr(0, 2));
			
			var d2 = ((d.getDate() < 10)?'0':'')+Number(d.getDate()).toString()+datesep
								+((d.getMonth() < 9)?'0':'')+Number(d.getMonth()+1).toString()+datesep
								+Number(d.getFullYear()).toString();

			if ((field.value != d2 
					|| field.value.charAt(2) != datesep
					|| field.value.charAt(5) != datesep
					|| field.value.length != 10) && field.value != '') {
				alert(name);
				field.focus();
				return false; 
			}
			break;

		case 'time24':
			if (field.value.charAt(2) != timesep
					|| field.value.length != 5
					|| Number(field.value.substr(0, 2)) > 23
					|| Number(field.value.substr(3, 2)) > 59) {
				alert(name);
				field.focus();
				return false; 
			}
			break;

		case 'radio':
			if (field.value == null) {
				alert(name);
				return false; 
			}
			break;

		case 'url':
			if (field.value.substr(0, 7) != 'http://' 
					|| field.value.indexOf('.') == -1 
					|| field.value.indexOf(' ') != -1 
					|| field.value.length < 11) {
				
				alert(name);
				field.focus();
				return false;
			}
			break;

		case 'country':
			if (field.value.substr(0,2) == '--') {
					alert(name);
					field.focus();
					return false;
			}
			break;	

		case 'captcha':
			if (field.value == '' || field.value.length != 5) {
					alert(name);
					field.focus();
					return false;
			}
			break;	

		case 'phone':
			var checkOK = "0123456789+() -";
			var checkStr = field.value;
			var allValid = true;
			if(checkStr.length < 6){
				alert(name);
				field.focus();
				return false;	
			}

			for (i = 0;  i < checkStr.length;  i++)
			{
				ch = checkStr.charAt(i);
				for (j = 0;  j < checkOK.length;  j++)
					if (ch == checkOK.charAt(j))
						break;
				if (j == checkOK.length)
				{
					allValid = false;
					break;
				}
			}
			if (allValid==false) {
				alert(name);
				field.focus();
				return false;
			}
			break;
	}

	return true;
}



function valReserve() {
	f = window.document.mail;
	ret = validate(f.name, 'string', 'name')
						&& validate(f.email, 'email', 'e-mail address')
						&& validate(f.phone, 'phone', 'phone number')
						&&	validate(f.country, 'country', 'country')
						&& validate(f.comment, 'string', 'comment')
						&& validate(f.security_code, 'captcha', 'Please check the entered word')
						;
	return ret;
}
function valContact() {
	f = window.document.mail;
	ret = validate(f.name, 'string', 'name')
						&& validate(f.email, 'email', 'e-mail address')
						&& validate(f.phone, 'phone', 'phone number')
						&&	validate(f.country, 'country', 'country')
						&& validate(f.subject, 'string', 'subject')
						&& validate(f.comment, 'string', 'comment')
						&& validate(f.security_code, 'captcha', 'Please check the entered word')
						;
	return ret;
}

function valInviteFriend() {
	f = window.document.mail;
	ret = validate(f.sender_name, 'string', 'name')
				&& validate(f.sender_email, 'email', 'e-mail address')
				&& validate(f.friend_name, 'string', 'friend name')
				&& validate(f.friend_email, 'email', 'friend e-mail address')
				&& validate(f.comment, 'string', 'comment')
				&& validate(f.security_code, 'captcha', 'Please check the entered word')
				;
	return ret;
}


// XTRA FUNCTIONS

function placePaw(imageNum,baseurl) {
		imgPaw = getRef('paw'+imageNum);
		imgPaw.src = baseurl+'images/sm_paw.gif';	
}

function resetPaws(maxPaws,baseurl) {
	for (x=1; x<=maxPaws; x++) {
		imgPaw = getRef('paw'+x);
		imgPaw.src = baseurl+'images/spacer.gif';
	}
}

function ImageSrc(imgid, imgsrc, txtid, txtval){

	var imgobj = getRef(imgid);
	imgobj.src= imgsrc;
	
	var txtobj = getRef(txtid);
	txtobj.value = txtval;

}

	function showVideo(divid, divstat, dividnd, divstatnd, txtid, txtval, embedid, embedsrc) {
	
	var divobj = getRef(divid);
	divobj.style.display = divstat;
	
	var divobjnd = getRef(dividnd);
	divobjnd.style.display = divstatnd;

	var embedobj = getRef(embedid);
	embedobj.src = embedsrc;

	var txtobj = getRef(txtid);
	txtobj.value = txtval;
	
}

function showImage(divid, divstat, dividnd, divstatnd, imgid, imgsrc, txtid, txtval) {
	
	var divobj = getRef(divid);
	divobj.style.display = divstat;
	
	var divobjnd = getRef(dividnd);
	divobjnd.style.display = divstatnd;

	var imgobj = getRef(imgid);
	imgobj.src = imgsrc;
	
	var txtobj = getRef(txtid);
	txtobj.value = txtval;
	
	}

function bgInput(tdid, tdclass){
	
	var tdobj = getRef(tdid);
	tdobj.className= tdclass;	
	
}

function divTag(divid)
			{
				var objdiv = getRef(divid);
			 if (objdiv.style.display=="none"){ objdiv.style.display = "block"; return false;}					
					if (objdiv.style.display=="block"){ objdiv.style.display = "none"; return false;}
					
			}
function valCat() {
		f = window.document.editform;
		if (f.lx_type[0].checked || f.lx_type[1].checked){
			ret = validate(f.lx_name, 'string', 'cat name');
			return ret;
		}
		/*else if (f.lx_type[3].checked){
			ret = validate(f.lx_name, 'string', 'cat name')
			&& validate(f.lx_owner, 'string', 'owner name')
			;
			return ret;
		}*/
}

function valPhone(field){

 var checkOK = "0123456789+()";
  var checkStr = field.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid) {
    alert("Enter a valid phone number");
    field.focus();
    return (false);
  }
}

function valDateGreater(start, end, message) {
	var s = new Date(start.value.substr(6, 4)
				, Number(start.value.substr(0, 2))-1
				, start.value.substr(3, 2));

	var e = new Date(end.value.substr(6, 4)
				, Number(end.value.substr(0, 2))-1
				, end.value.substr(3, 2));

	if (e <= s) {
		alert(message);
		end.focus();
		return false;
	}
	return true;
}

function valDateGreaterDMY(start, end, message) {
	var s = new Date(start.value.substr(6, 4)
				, Number(start.value.substr(3, 2))-1
				, start.value.substr(0, 2));

	var e = new Date(end.value.substr(6, 4)
				, Number(end.value.substr(3, 2))-1
				, end.value.substr(0, 2));

	if (e <= s) {
		alert(message);
		end.focus();
		return false;
	}
	return true;
}

