//	------------------------------	//
// BROWSER DETECTION FOR FADE		//
//	------------------------------	//
var bID;
var browser = navigator.userAgent;
if(browser.indexOf("MSIE") != -1){
	bID = "IE";
	var ieOpacityHolder = 0;
} else{
	bID = "Gen";
	var mozOpacityHolder = 0;
}
//	------------------------------	//
// !BROWSER DETECT					//
//	------------------------------	//

//	------------------------------	//
// AJAX REQUEST OBJECT				//
//	------------------------------	//
var aRo = null;

function initARO(){
	try{
		aRo = new XMLHttpRequest();
	} catch(e){
		var flag = false;
		var domObj = ["MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
		for(var i = 0; i < domObj.length; i++){
			if(flag == false){
				try{
					if(aRo = new ActiveXObject(domObj[i])){
						flag = true;
					}
				} catch(e){
					continue;
				}
			} else break;
		}
	}
}
//	------------------------------	//
// !AJAX REQUEST OBJECT				//
//	------------------------------	//

//	------------------------------	//
// DREAMWEAVER CODE					//
//	------------------------------	//

function MM_swapImgRestore(){
	var i, x, a = document.MM_sr;
	for(i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++)
		x.src = x.oSrc;
}

function MM_preloadImages(){
	var d = document;
	if(d.images){
		if(!d.MM_p)
			d.MM_p = new Array();
		var i , j = d.MM_p.length, a=MM_preloadImages.arguments;
		for(i = 0; i < a.length; i++)
			if(a[i].indexOf("#") != 0){
				d.MM_p[j] = new Image;
				d.MM_p[j++].src = a[i];
			}
	}
}

function MM_findObj(n, d){ //v4.01
	var p, i, x;
	if(!d)
		d = document;
	if((p = n.indexOf("?")) > 0 && parent.frames.length){
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0,p);
	}
	if(!(x = d[n]) && d.all)
		x = d.all[n];
	for(i = 0; !x && i < d.forms.length; i++)
		x = d.forms[i][n];
	for(i = 0; !x && d.layers && i < d.layers.length; i++)
		x = MM_findObj(n, d.layers[i].document);
	if(!x && d.getElementById)
		x = d.getElementById(n);
	return x;
}

function MM_swapImage() { //v3.0
	var i, j = 0, x, a = MM_swapImage.arguments;
	document.MM_sr = new Array;
	for(i = 0; i < (a.length - 2); i += 3)
		if((x = MM_findObj(a[i])) != null){
			document.MM_sr[j++] = x;
			if(!x.oSrc)
				x.oSrc = x.src;
				x.src = a[i + 2];
		}
}

//	------------------------------	//
// !DREAMWEAVER CODE				//
//	------------------------------	//

//	------------------------------	//
// 	FORM VALIDATION FUNCTIONS		//
//	------------------------------	//
var contactSubmitCount = 0;
var puppySubmitCount = 0;

function trim(str){
    return str.replace(/^\s+|\s+$/g,'');
}

function isEmail(str){
    var regex = /^[-_.a-z0-9]+@(([-a-z0-9]+\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;
    return regex.test(str);
}

function isNumeric(str){
	if(isNaN(str)) return false;
	else return true;
}

function isChecked(str){
	var fieldGrp;
	var flag = new Boolean();
	flag = false;
	if(document.all) fieldGrp = document.all(str);
	else fieldGrp = document.getElementsByName(str);
	for(var e = 0; e < fieldGrp.length; e++){
		if(fieldGrp[e].checked == true) flag = true;
		else continue;
	}
	return flag;
}

function isSelected(str){
	var flag = new Boolean();
	flag = false;
	for(var f = 1; f < str.length; f++){
		if(str.options[f].selected) flag = true;
		else continue;
	}
	return flag;
}

function checkMarkup(str){
	var muFlag = str.search(/<[a-zA-Z0-9]+((>|\/>)|(\s>|\s\/>))/g);
	if(muFlag != -1){
		alert("You have harmful tags in your input - please remove them.");
		return false;
	} else{
		return true;
	}
}

function replaceMarkup(target){
	var str = target.value;
	str = str.replace(/</g, "&lt;");
	str = str.replace(/>/g, "&gt;");
	str = str.replace(/"/g, "&quot;");
	str = str.replace(/'/g, "&#39;");
	str = str.replace(/&/g, "&amp;");
	target.value = str;
}

function validateContact(cobj, where){
	if(contactSubmitCount > 0){
		alert("Please allow a few moments for the form to submit");
		return false;
	} else{
		if(trim(cobj.first_name.value) == "" || cobj.first_name.value.length == 0 || !checkMarkup(cobj.first_name.value)){
			alert("Please tell us your first name.");
			cobj.first_name.focus();
			return false;
		}
		if((trim(cobj.email.value) == "" || cobj.email.value.length == 0) && (trim(cobj.contact_number.value) == "" || cobj.contact_number.value.length == 0)){
			alert("Please tell us either your email address or contact number so that we may be able to contact you.");
			cobj.email.focus();
			return false;
		}
		if(!isChecked("contact_method")){
			alert("Please tell us your preferred method of contact.");
			cobj.contact_method[0].focus();
			return false;
		}
		if(cobj.contact_method[0].checked != false && (trim(cobj.email.value) == "" || cobj.email.value.length == 0)){
			alert("Please tell us your email address");
			cobj.email.focus();
			return false;
		} else if(cobj.contact_method[1].checked != false && (trim(cobj.contact_number.value) == "" || cobj.contact_number.value.length == 0)){
			alert("Please tell us your contact number");
			cobj.contact_number.focus();
			return false;
		}
		if(cobj.email.value.length > 0){
			if(!isEmail(cobj.email.value)){
				alert("Please enter a valid email address.");
				cobj.email.focus();
				return false;
			}
		}
		if(cobj.contact_number.value.length > 0){
			if(!isNumeric(cobj.contact_number.value)){
				alert("Please enter a valid contact number (numeric values only please).");
				cobj.contact_number.focus();
				return false;
			}
		}
		if(trim(cobj.contact_query.value) == "" || cobj.contact_query.value.length == 0){
			alert("Please enter your query.");
			cobj.contact_query.focus();
			return false;
		}
		// Made it through the validations - get the contact method
		var contactGrp = document.getElementsByName("contact_method");
		for(var c = 0; c < contactGrp.length; c++){
			if(contactGrp[c].checked == true) var cMethod = contactGrp[c].value;
			else continue;
		}
		// Initiate ajax object
		initARO();
		if(aRo != null){
			if(where === "home")
				aRotgt = "services/contact.php";
			else
				aRotgt = "../services/contact.php";
			document.getElementById("contact_initial").style.display = "none";
			document.getElementById("contact_waiting").style.display = "block";
			var params = "querytype=" + cobj.query_type.value + "&firstname=" + cobj.first_name.value + "&surname=" + cobj.surname.value + "&contactmethod=" + cMethod + "&email=" + cobj.email.value + "&contactnumber=" + cobj.contact_number.value + "&query=" + cobj.contact_query.value;
			aRo.onreadystatechange = handleContactResponse;
			aRo.open("POST", aRotgt, true);
			aRo.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			aRo.setRequestHeader("Content-length", params.length);
			aRo.setRequestHeader("Connection", "close");
			aRo.send(params);
		} else{
			alert("Your browser does not support the functionality of this site, please use the contact details provided to best direct your query.\n\nWe do apologise for any inconvenience caused.");
		}
		contactSubmitCount++;
		return false;
	}
}

function handleContactResponse(){
	if(aRo.readyState == 4){
		if(aRo.status == 200 && aRo.responseText == "OK"){
			document.getElementById("contact_waiting").style.display = "none";
			document.getElementById("contact_success").style.display = "block";
			aRo = null;
		}
	}
	return true;
}

function validatePuppyReg(pobj){
	if(puppySubmitCount > 0){
		alert("Please allow a few moments for the form to submit");
		return false;
	} else{
		// OWNER VALIDATIONS
		if(trim(pobj.fname.value) == "" || pobj.fname.value.length == 0 || !checkMarkup(pobj.fname.value)){
			alert("Please tell us your first name.");
			pobj.fname.focus();
			return false;
		}
		if(trim(pobj.sname.value) == "" || pobj.sname.value.length == 0 || !checkMarkup(pobj.sname.value)){
			alert("Please tell us your surname.");
			pobj.sname.focus();
			return false;
		}
		
		
		
		if((trim(pobj.ph_address_1.value) == "" || pobj.ph_address_1.value.length == 0 || !checkMarkup(pobj.ph_address_1.value)) || (trim(pobj.ph_address_2.value) == "" || pobj.ph_address_2.value.length == 0 || !checkMarkup(pobj.ph_address_2.value))){
			alert("Please tell us your physical address.\n\nThe first two lines provided need to be filled in.");
			pobj.ph_address_1.focus();
			return false;
		}
		if(trim(pobj.ph_address_suburb.value) == "" || pobj.ph_address_suburb.value.length == 0 || !checkMarkup(pobj.ph_address_suburb.value)){
			alert("Please tell us your physical address suburb.");
			pobj.ph_address_suburb.focus();
			return false;
		}
		if(trim(pobj.ph_address_city.value) == "" || pobj.ph_address_city.value.length == 0 || !checkMarkup(pobj.ph_address_city.value)){
			alert("Please tell us your physical address city.");
			pobj.ph_address_city.focus();
			return false;
		}
		if(trim(pobj.ph_address_code.value) == "" || pobj.ph_address_code.value.length == 0 || !checkMarkup(pobj.ph_address_code.value) || !isNumeric(pobj.ph_address_code.value)){
			alert("Please tell us your physical address postal code.\n\nOnly numbers are accepted.");
			pobj.ph_address_code.focus();
			return false;
		}
		
		
		if(pobj.postal_add_check.checked == false){
			if((trim(pobj.p_address_1.value) == "" || pobj.p_address_1.value.length == 0 || !checkMarkup(pobj.p_address_1.value)) || (trim(pobj.p_address_2.value) == "" || pobj.p_address_2.value.length == 0 || !checkMarkup(pobj.p_address_2.value))){
				alert("Please tell us your postal address.\n\nThe first two lines provided need to be filled in.");
				pobj.p_address_1.focus();
				return false;
			}
			if(trim(pobj.p_address_city.value) == "" || pobj.p_address_city.value.length == 0 || !checkMarkup(pobj.p_address_city.value)){
				alert("Please tell us your postal address city.");
				pobj.p_address_city.focus();
				return false;
			}
			if(trim(pobj.p_address_code.value) == "" || pobj.p_address_code.value.length == 0 || !checkMarkup(pobj.p_address_code.value) || !isNumeric(pobj.p_address_code.value)){
				alert("Please tell us your postal address postal code.\n\nOnly numbers are accepted.");
				pobj.p_address_code.focus();
				return false;
			}
		}
		
		
		if(trim(pobj.c_number.value) == "" || pobj.c_number.value.length == 0 || !checkMarkup(pobj.c_number.value) || !isNumeric(pobj.c_number.value)){
			alert("Please tell us your contact number.\n\nOnly numbers are accepted.");
			pobj.c_number.focus();
			return false;
		}
		if(trim(pobj.email.value) == "" || pobj.email.value.length == 0 || !checkMarkup(pobj.email.value) || !isEmail(pobj.email.value)){
			alert("Please enter a valid email address.");
			pobj.email.focus();
			return false;
		}
		if(!isChecked("c_method")){
			alert("Please tell us your preferred method of contact.");
			pobj.c_method[0].focus();
			return false;
		}
		/*if(!isSelected(pobj.language)){
			alert("Please tell us your language.");
			pobj.language.focus();
			return false;
		}
		if(pobj.language.selectedIndex == (pobj.language.length - 1) && (trim(pobj.language_other.value) == "" || pobj.language_other.value.length == 0)){
			alert("Please tell us the other language you speak.");
			pobj.language_other.focus();
			return false;
		}
		if(pobj.language.selectedIndex == (pobj.language.length - 1) && (trim(pobj.language_other.value) != "" || pobj.language_other.value.length > 0) && !checkMarkup(pobj.language_other.value)){
			alert("Please tell us the other language you speak.");
			pobj.language_other.focus();
			return false;
		}*/
		
		// PUPPY VALIDATIONS
		if(trim(pobj.puppy_name.value) == "" || pobj.puppy_name.value.length == 0 || !checkMarkup(pobj.puppy_name.value)){
			alert("Please tell us your puppy's name.");
			pobj.puppy_name.focus();
			return false;
		}
		// re-work this validation to cater for month and years
		/*if(!isSelected(pobj.puppy_age)){
			alert("Please tell us your puppy's age.");
			pobj.puppy_age.focus();
			return false;
		}*/
		// !re-work
		if(!isSelected(pobj.puppy_breed)){
			alert("Please tell us your puppy's breed.");
			pobj.puppy_breed.focus();
			return false;
		}
		if(pobj.puppy_breed.selectedIndex == (pobj.puppy_breed.length - 1) && (trim(pobj.puppy_breed_other.value) == "" || pobj.puppy_breed_other.value.length == 0)){
			alert("Please tell us the other breed of your puppy.");
			pobj.puppy_breed_other.focus();
			return false;
		}
		if(pobj.puppy_breed.selectedIndex == (pobj.puppy_breed.length - 1) && (trim(pobj.puppy_breed_other.value) != "" || pobj.puppy_breed_other.value.length > 0) && !checkMarkup(pobj.puppy_breed_other.value)){
			alert("Please tell us the other breed of your puppy.");
			pobj.puppy_breed_other.focus();
			return false;
		}
		if(!isSelected(pobj.puppy_size)){
			alert("Please tell us your puppy's size.");
			pobj.puppy_size.focus();
			return false;
		}
		/*if(!isChecked("puppy_gender")){
			alert("Please tell us your puppy's gender.");
			pobj.puppy_gender[0].focus();
			return false;
		}*/
		if(!isSelected(pobj.puppy_why)){
			alert("Please tell us why you got your puppy.");
			pobj.puppy_why.focus();
			return false;
		}
		if(pobj.puppy_why.selectedIndex == (pobj.puppy_why.length - 1) && (trim(pobj.puppy_why_other.value) == "" || pobj.puppy_why_other.value.length == 0)){
			alert("Please tell us the other reason why you got your puppy.");
			pobj.puppy_why_other.focus();
			return false;
		}
		if(pobj.puppy_why.selectedIndex == (pobj.puppy_why.length - 1) && (trim(pobj.puppy_why_other.value) != "" || pobj.puppy_why_other.value.length > 0) && !checkMarkup(pobj.puppy_why_other.value)){
			alert("Please tell us the other reason why you got your puppy.");
			pobj.puppy_why_other.focus();
			return false;
		}
		if(!isSelected(pobj.puppy_where)){
			alert("Please tell us where you got your puppy.");
			pobj.puppy_where.focus();
			return false;
		}
		if(pobj.puppy_where.selectedIndex == (pobj.puppy_where.length - 1) && (trim(pobj.puppy_where_other.value) == "" || pobj.puppy_where_other.value.length == 0)){
			alert("Please tell us the other location you got your puppy from.");
			pobj.puppy_where_other.focus();
			return false;
		}
		if(pobj.puppy_where.selectedIndex == (pobj.puppy_where.length - 1) && (trim(pobj.puppy_where_other.value) != "" || pobj.puppy_where_other.value.length > 0) && !checkMarkup(pobj.puppy_where_other.value)){
			alert("Please tell us the other location you got your puppy from.");
			pobj.puppy_where_other.focus();
			return false;
		}
		if(!isSelected(pobj.puppy_program_hear)){
			alert("Please tell us where you heard about our Puppy Program.");
			pobj.puppy_program_hear.focus();
			return false;
		}
		if(pobj.puppy_program_hear.selectedIndex == (pobj.puppy_program_hear.length - 1) && (trim(pobj.puppy_program_hear_other.value) == "" || pobj.puppy_program_hear_other.value.length == 0)){
			alert("Please tell us where else you heard about our Puppy Program.");
			pobj.puppy_program_hear_other.focus();
			return false;
		}
		if(pobj.puppy_program_hear.selectedIndex == (pobj.puppy_program_hear.length - 1) && (trim(pobj.puppy_program_hear_other.value) != "" || pobj.puppy_program_hear_other.value.length > 0) && !checkMarkup(pobj.puppy_program_hear_other.value)){
			alert("Please tell us where else you heard about our Puppy Program.");
			pobj.puppy_program_hear_other.focus();
			return false;
		}
		return true;
	}
}
//	------------------------------	//
//	!FORM VALIDATION FUNCTIONS		//
//	------------------------------	//

//	------------------------------	//
//	FADE FUNCTIONALITY				//
//	------------------------------	//
var duration = 500;
var fInd;
var tID = null;

function fadeTarget(){
	if(bID == "IE"){
		if(fInd == "in"){
			if(ieOpacityHolder < 100){
				ieOpacityHolder += 10;
				tID.style.filter ="progid:DXImageTransform.Microsoft.Alpha(opacity=" + (ieOpacityHolder + 10) + ")";
				if(ieOpacityHolder < 100){
					setTimeout("fadeTarget()", (duration/10));
				} else{
					fInd = "";
					return true;
				}
			}
		} else{
			if(ieOpacityHolder > 0){
				ieOpacityHolder -= 10;
				tID.style.filter ="progid:DXImageTransform.Microsoft.Alpha(opacity=" + (ieOpacityHolder - 10) + ")";
				if(ieOpacityHolder > 0){
					setTimeout("fadeTarget()", (duration/10));
				} else{
					fInd = "";
					tID.style.display = "none";
					tID = null;
					return true;
				}
			}
		}
	} else{
		if(fInd == "in"){
			if(mozOpacityHolder < 1){
				mozOpacityHolder += 0.1;
				tID.style.opacity = mozOpacityHolder;
				if(mozOpacityHolder < 1){
					setTimeout("fadeTarget()", (duration/10));
				} else{
					fInd = "";
					return true;
				}
			}
		} else{
			if(mozOpacityHolder > 0){
				mozOpacityHolder -= 0.1;
				tID.style.opacity = mozOpacityHolder;
				if(mozOpacityHolder > 0){
					setTimeout("fadeTarget()", (duration/10));
				} else{
					fInd = "";
					tID.style.display = "none";
					tID = null;
					return true;
				}
			}
		}
	}
}

function fader(opacityFrom, opacityTo, targElem){
	/* First check if there is an existing fade div open - close and then open the target
	if(tID != null && tID.id != targElem){
		if(bID == "IE"){
			ieOpacityHolder = 0;
			tID.style.filter ="progid:DXImageTransform.Microsoft.Alpha(opacity=" + ieOpacityHolder + ")";
		} else{
			mozOpacityHolder = 0;
			tID.style.opacity = mozOpacityHolder;
		}
		tID.style.display = "none";
		tID = null;
	}*/
	// Continue bringing the fade target in
	tID = document.getElementById("" + targElem + "");
	if(opacityTo > opacityFrom && tID.style.display != "block"){
		tID.style.display = "block";
		fInd = "in";
	} else{
		fInd = "out";
	}
	fadeTarget();
}
//	------------------------------	//
//	!FADE FUNCTIONALITY				//
//	------------------------------	//

//	---------------------------------------------------------	//
//	PUPPY FAQS/CHOOSING YOUR PUPPY FUNCTIONALITY				//
//	---------------------------------------------------------	//
function showfaq(id){
	d = document.getElementById(id);
	d.style.display = (d.style.display == "none")?"block":"none";
}

function showallfaqs(){
	// alternate hideshow global var
	if(!window["faqdisplay"] || window["faqdisplay"] == "block"){
		faqdisplay = "none";
	} else{
		faqdisplay = "block";
	}
	// define the first faq div suffix
	i = 1;
	// loop through all the divs and show/hide them
	while(a = true){ //set it to loop indefinitely...
		if(document.getElementById("faq" + i)){ 
			// set the display property
			document.getElementById("faq" + i).style.display = "block";
			i++;
		} else{ // stop loop if the next div doesn't exist...
			return false;
		}
	}
}
//	---------------------------------------------------------	//
//	!PUPPY FAQS/CHOOSING YOUR PUPPY FUNCTIONALITY				//
//	---------------------------------------------------------	//

//	-----------------------------------------	//
//	PUPPY NUTRITION FUNCTIONALITY				//
//	-----------------------------------------	//
function showtab(id){
	if(window["active"]){
		document.getElementById(active).style.visibility = "visible";
		document.getElementById(active + "div").style.display = "none";
	} else{
		document.getElementById("stage1").style.visibility = "visible";
		document.getElementById("stage1div").style.display = "none";
	}
	document.getElementById(id).style.visibility = "hidden";
	document.getElementById(id + "div").style.display = "block";
	active = id;
}
//	-----------------------------------------	//
//	!PUPPY NUTRITION FUNCTIONALITY				//
//	-----------------------------------------	//