var xmlhttp;

function ajaxGeneric(strPageURL, strElementID, strQueryString, strRunNext) {
    var xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) { 
     return;
    }

		qstr="?sid="+Math.random();
		qstr=qstr+strQueryString;
    xmlHttp.open("GET", strPageURL+qstr, false);
    xmlHttp.send(null);
		if (strElementID != '') {
			updatepage(strElementID, xmlHttp.responseText);
		}
		
		if (strRunNext=='hidemessage') {
			//Fraser - Used to slide/hide success message on searches.
			hidemessage();
		}
		
} 

function ajaxGenericPost(strPageURL, strElementID, strQueryString, strRunNext) {
    var xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) { 
     return;
    }

		qstr="sid="+Math.random();
		qstr=qstr+strQueryString;
    xmlHttp.open("POST", strPageURL, false);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", qstr.length);
		xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(qstr);
		if (strElementID != '') {
			updatepage(strElementID, xmlHttp.responseText);
		}
		
		if (strRunNext=='hidemessage') {
			//Fraser - Used to slide/hide success message on searches.
			hidemessage();
		}
		
} 


function ajaxGenericNoWait(strPageURL, strElementID, strQueryString) {
    var xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) { 
     return;
    }

		qstr="?sid="+Math.random();
		qstr=qstr+strQueryString;
    xmlHttp.open("GET", strPageURL+qstr, true);
    xmlHttp.send(null);
		if (strElementID != '') {
			updatepage(strElementID, xmlHttp.responseText);
		}
		
} 

function ajaxGenericRemoteUpdate(strPageURL, strElementID, strQueryString) {
    var xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
     return;
    }

		qstr="?sid="+Math.random();
		qstr=qstr+strQueryString;
    xmlHttp.open("GET", strPageURL+qstr, false);
    xmlHttp.send(null);
//		if (strElementID != '') {
//			alert(xmlHttp.responseText);
//		}
		
} 

function ajaxGenericCheckResponse(strPageURL, strQueryString, strCheckVar) {
    var xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) { 
     return;
    }

		qstr="?sid="+Math.random();
		qstr=qstr+strQueryString;
    xmlHttp.open("GET", strPageURL+qstr, false);
	  xmlHttp.send(null);
		if (xmlHttp.responseText == strCheckVar) {
			if (document.getElementById('checkresponseoutput')) {
				document.getElementById('checkresponseoutput').innerHTML=xmlHttp.responseText;
			}
			return true;
		} else {
			if (document.getElementById('checkresponseoutput')) {
				document.getElementById('checkresponseoutput').innerHTML=xmlHttp.responseText;
			}
			return false;
		}
} 

function ajaxGenericCheckResponseTest(strPageURL, strQueryString, strCheckVar) {
    var xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) { 
     return;
    }

		qstr="?sid="+Math.random();
		qstr=qstr+strQueryString;
    xmlHttp.open("GET", strPageURL+qstr, false);
	  xmlHttp.send(null);
		alert(xmlHttp.responseText);
		if (xmlHttp.responseText == strCheckVar) {
			if (document.getElementById('checkresponseoutput')) {
				document.getElementById('checkresponseoutput').innerHTML=xmlHttp.responseText;
			}
			return true;
		} else {
			if (document.getElementById('checkresponseoutput')) {
				document.getElementById('checkresponseoutput').innerHTML=xmlHttp.responseText;
			}
			return false;
		}
} 



function updatepage(strElementID, str){
    document.getElementById(strElementID).innerHTML = str;
}

function GetXmlHttpObject() {
	if (window.XMLHttpRequest){
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject){
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}


function compareFieldHiLow(strMinFieldID, strMaxFieldID, strErrorID, strErrorDesID, strFieldType) {
	strMinField = document.getElementById(strMinFieldID);
	strMaxField = document.getElementById(strMaxFieldID);
	strErrorField = document.getElementById(strErrorID);
	strErrorDesField = document.getElementById(strErrorDesID);
	bolNoError = true;
	
	if (strFieldType == 'dd') {
		if (parseInt(strMinField[strMinField.selectedIndex].value) <= parseInt(strMaxField[strMaxField.selectedIndex].value)) {
			bolNoError = true;
		} else {
			bolNoError = false;
		}
	} else if (strFieldType == 'text') {
		if (isNaN(strMinField.value) == false && isNaN(strMaxField.value) == false && strMinField.value <= strMaxField.value) {
			bolNoError = true;
		} else {
			bolNoError = false;
		}
	}

	if (bolNoError == false) {
		if (strErrorField != null) {
			strErrorField.innerHTML='';
			strErrorField.style.display = 'none';
		}
		if (strErrorDesField != null) {
			strErrorDesField.innerHTML='Maximum must be greater than or equal to the minimum';
			strErrorDesField.style.display = '';
		}
	} else {
		if (strErrorField != null) {
			strErrorField.innerHTML='';
			strErrorField.style.display = 'none';
		}
		if (strErrorDesField != null) {
			strErrorDesField.innerHTML='';
			strErrorDesField.style.display = 'none';
		}
	}
	return bolNoError;
}


function comparePasswordValues(objPassField1, objPassField2, intMinChar, intMaxChar, strErrorID, strErrorDesID) {
	bolNoError = true;
	strErrorField = document.getElementById(strErrorID);
	strErrorDesField = document.getElementById(strErrorDesID);
	
	if (strErrorDesField != null) {
		strErrorDesField.innerHTML='';
		strErrorDesField.style.display = 'none';
	}

	if (objPassField1.value != objPassField2.value && bolNoError === true) {
		bolNoError = false;
		if (strErrorDesField != null) {
			strErrorDesField.innerHTML='Your passwords do not match';
			strErrorDesField.style.display = '';
		}

	} else {

		if (!isNaN(intMinChar) && bolNoError === true) {
			if (objPassField1.value.length < intMinChar) {
				bolNoError = false;
			}
		}
	
		if (!isNaN(intMaxChar) && bolNoError === true) {
			if (objPassField1.value.length > intMaxChar) {
				bolNoError = false;
			}
		}

		if (strErrorDesField != null && bolNoError === false) {
			if (isNaN(intMinChar)==false && isNaN(intMaxChar)==false) {
				strErrorDesField.innerHTML='Your password must contain ' + intMinChar + ' to ' + intMaxChar + ' characters';
				strErrorDesField.style.display = '';
			} else if (isNaN(intMinChar)==false && isNaN(intMaxChar)==true) {
				strErrorDesField.innerHTML='Your password must contain at least ' + intMinChar + ' characters';
				strErrorDesField.style.display = '';
			} else if (isNaN(intMaxChar)==false && isNaN(intMinChar)==true) {
				strErrorDesField.innerHTML='Your password must not contain more than ' + intMaxChar + ' characters';
				strErrorDesField.style.display = '';
			}
		}
		
	}
	

	return bolNoError;
}


function checkRequiredField(strFieldID, strErrorID, strErrorDesID, strFieldType) {
	strField = document.getElementById(strFieldID);
	strErrorField = document.getElementById(strErrorID);
	strErrorDesField = document.getElementById(strErrorDesID);
	bolNoError = true;
	
	if (strField != null) {
		if (strField.style.display=='none') {
			if (strErrorField != null)
				strErrorField.style.display = 'none';
			if (strErrorDesField != null)
				strErrorDesField.style.display = 'none';
		} else {
			if (strFieldType == 'dd') {
				if (strField[strField.selectedIndex].value != '') {
					bolNoError = true;
				} else {
					bolNoError = false;
				}
			} else if (strFieldType == 'text') {
				if (strField.value != '') {
					bolNoError = true;
				} else {
					bolNoError = false;
				}
			} else if (strFieldType == 'ddl') {
				var opt = strField.options;
				var x, len = opt.length;
				for (x=0; x<len; ++x) {
						if(opt[x].selected) break;
				}
				if(x<len) {
					bolNoError = true;
				} else {
					bolNoError = false;
				}
			}
			
			if (bolNoError == false) {
				if (strErrorField != null) {
					strErrorField.innerHTML='';
					strErrorField.style.display='';
				}
				if (strErrorDesField != null) {
					strErrorDesField.innerHTML='This is a required field';
					strErrorDesField.style.display='';
				}
			} else {
				if (strErrorField != null) {
					strErrorField.innerHTML='';
					strErrorField.style.display='none';
				}
				if (strErrorDesField != null) {
					strErrorDesField.innerHTML='';
					strErrorDesField.style.display='none';
				}
			}
		}
	}
	return bolNoError;
}


function checkNumericField(strFieldID, strErrorID, strErrorDesID, strFieldType) {
	strField = document.getElementById(strFieldID);
	strErrorField = document.getElementById(strErrorID);
	strErrorDesField = document.getElementById(strErrorDesID);
	bolNoError = true;
	
	if (strField != null) {
		if (strField.style.display=='none') {
			if (strErrorField != null)
				strErrorField.style.display = 'none';
			if (strErrorDesField != null)
				strErrorDesField.style.display = 'none';
		} else {
			if (strFieldType == 'dd') {
				if (isNaN(strField[strField.selectedIndex].value)==false && strField[strField.selectedIndex].value != '') {
					bolNoError = true;
				} else {
					bolNoError = false;
				}
			} else if (strFieldType == 'text') {
				if (isNaN(strField.value)==false && strFieldType != '') {
					bolNoError = true;
				} else {
					bolNoError = false;
				}
			}
			
			if (bolNoError == false) {
				if (strErrorField != null) {
					strErrorField.innerHTML='';
					strErrorField.style.display='';
				}
				if (strErrorDesField != null) {
					strErrorDesField.innerHTML='Please enter a numeric value';
					strErrorDesField.style.display='';
				}
			} else {
				if (strErrorField != null) {
					strErrorField.innerHTML='';
					strErrorField.style.display='none';
				}
				if (strErrorDesField != null) {
					strErrorDesField.innerHTML='';
					strErrorDesField.style.display='none';
				}
			}
		}
	}
	return bolNoError;
}


function checkValidandUniqueEmail(objEmailAddress, strEmailCheckPage, strCheckResponse, strErrorID, strErrorDesID) {
	var vartempvalidEmail = false;
	bolNoError = true;
	strErrorField = document.getElementById(strErrorID);
	strErrorDesField = document.getElementById(strErrorDesID);

	strEmailAddress = objEmailAddress.value;

	bolNoError = ajaxGenericCheckResponse(strEmailCheckPage,'&email_address='+strEmailAddress,strCheckResponse);
	
	if (bolNoError == false) {
		if (strErrorField) {
			strErrorField.innerHTML='';
			strErrorField.style.display='';
		}
		if (strErrorDesField) {
			strErrorDesField.innerHTML='This email address already exists or is invalid.';
			strErrorDesField.style.display='';
		}
	} else {
		if (strErrorField) {
			strErrorField.innerHTML='';
			strErrorField.style.display='none';
		}
		if (strErrorDesField) {
			strErrorDesField.innerHTML='';
			strErrorDesField.style.display='none';
		}
	}
	return bolNoError;
}

function checkValidandUniqueUsername(objEmailAddress, strEmailCheckPage, strCheckResponse, strErrorID, strErrorDesID) {
	var vartempvalidEmail = false;
	bolNoError = true;
	strErrorField = document.getElementById(strErrorID);
	strErrorDesField = document.getElementById(strErrorDesID);

	strEmailAddress = objEmailAddress.value;

	bolNoError = ajaxGenericCheckResponse(strEmailCheckPage,'&email_address='+strEmailAddress,strCheckResponse);
	//alert('Result: '+bolNoError);
	
	if (bolNoError == false) {
		if (strErrorField) {
			strErrorField.innerHTML='';
			strErrorField.style.display='';
		}
		if (strErrorDesField) {
			strErrorDesField.innerHTML='This username already exists or is invalid.';
			strErrorDesField.style.display='';
		}
	} else {
		if (strErrorField) {
			strErrorField.innerHTML='';
			strErrorField.style.display='none';
		}
		if (strErrorDesField) {
			strErrorDesField.innerHTML='';
			strErrorDesField.style.display='none';
		}
	}
	return bolNoError;
}

function ScrolltoTopAnchor() {
   	window.scroll(0,findYPosition(document.getElementById('top'))); // horizontal and vertical scroll targets
}

function findYPosition(obj) {
	var curtop = 0;
	if(obj.offsetParent)
			while(1)
			{
				curtop += obj.offsetTop;
				if(!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
	else if(obj.y)
			curtop += obj.y;
	return curtop;
}

function resize(object,height,speed) {
		if (object) {
			var myinterval = window.setInterval(function () {
					var ht = parseInt(object.style.height,10);
					if (ht > height) {
							object.style.height = (ht - (ht/100)*speed) + 'px';
					} else {
							window.clearInterval(myinterval);
					}
			},10);
		}
}


