/*
=============================================
 Development By: Infinity Arts Interactive, LLC - www.InfinityArts.com, © Copyright 2009 - All rights reserved.
 Description: Public JS Functions
 Date: 09/25/2009
=============================================
*/

	var cmpntPath = '/components/';

// ============================================================ FUNCTIONS: FORM PROCESSING ============================================================

	function valSMForm(oForm) {
		var flds = oForm.frmFlds.value;
		
		for (var i=1; i<=flds; i++) {
			var typeId = (oForm['typ'+i])? parseInt(oForm['typ'+i].value):0;
			
			if (oForm['timeHH'+i]) {
				oForm['fld'+i].value = oForm['timeHH'+i].value+':'+oForm['timeMM'+i].value+' '+oForm['timeAP'+i].options[oForm['timeAP'+i].selectedIndex].value;
			}
			
			if (oForm['req'+i]) {
				if (oForm['req'+i].value == 'Y') {
					switch(typeId) {
						case 1:
							if (!valSMForm_radio(oForm, i)) { formError('Please check an option for "'+valSMForm_labelTxt(i)+'" to continue.', oForm['rd'+i][0]); return false; }
							break;
						case 2:
							if (!oForm['cb'+i].checked) { formError('Please check "'+valSMForm_labelTxt(i)+'" to continue.', oForm['cb'+i]); return false; }
							break;
						case 3: case 4:
							if (oForm['fld'+i].value == '') { formError('Please enter a value for "'+valSMForm_labelTxt(i)+'" to continue.', oForm['fld'+i]); return false; }
							break;
						case 5: case 7: case 8: case 9: case 10:
							if (oForm['fld'+i].options[oForm['fld'+i].selectedIndex].value == '') { formError('Please select an option for "'+valSMForm_labelTxt(i)+'" to continue.', oForm['fld'+i]); return false; }
							break;
						case 6:
							if (!valSMForm_option(oForm, i)) { formError('Please select an option for "'+valSMForm_labelTxt(i)+'" to continue.', oForm['fld'+i][0]); return false; }
							break;
						case 13:
							if (!isDate(oForm['fld'+i].value,'')) { formError('Please enter a valid date (MM/DD/YYYY) for "'+valSMForm_labelTxt(i)+'" to continue.', oForm['fld'+i]); return false; }
							break;
						 case 19:
						 	if (oForm['fld'+i].value == '' || oForm['timeHH'+i].value == '' || oForm['timeMM'+i].value == '') { formError('Please enter a value for "'+valSMForm_labelTxt(i)+'" to continue.', oForm['timeHH'+i]); return false; }
							break;
					}
				}
			}
			
			switch(typeId) {
				case 1:
					valSMForm_specialFieldData(oForm, 'rd', i); break;
				case 2:
					valSMForm_specialFieldData(oForm, 'cb', i); break;
			}
		}
		
		
		if (oForm.frmCaptcha.value == 'Y') valRecaptcha(oForm);
		else valSMForm_send(oForm);
		
		return false;
	}
	
	function valSMForm_labelTxt(oPos) {
		if (document.all) return document.getElementById('txt'+oPos).innerText;
		else return document.getElementById('txt'+oPos).textContent;
	}
	
	function valSMForm_option(oForm, oPos) {
		for (var i=0; i<oForm['fld'+oPos].options.length; i++) {
			if (oForm['fld'+oPos].options[i].selected) return true;
		}
		return false;
	}
	
	function valSMForm_radio(oForm, oPos) {
		var i = 0;
		
		while (oForm['rd'+oPos][i]) {
			if (oForm['rd'+oPos][i].checked) return true;
			
			i++;
		}
		
		return false;
	}
	
	function valSMForm_specialFieldData(oForm, oFld, oPos) {
		for (var j=0; j<oForm[oFld+oPos].length; j++) { 
			if (oForm[oFld+oPos][j].checked) { oForm['fld'+oPos].value = oForm[oFld+oPos][j].value; break; }
		}
	}
	
	function valSMForm_send(oForm) {
		oForm.actiontype.value = 'submit';
		loadAJAX('DIVsmForm',cmpntPath+'sm-forms.asp',getFormVars(oForm));
	}

// ============================================================ FUNCTIONS: FORMS & DATE ============================================================
	
	function formError(oMsg, oField) {
		alert(oMsg);
		oField.focus();
	}
	
	function isDate(dateStr, field) {
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
		var matchArray = dateStr.match(datePat);
		
		if (matchArray == null) {
			//alert("Please enter a valid "+field+" Date as mm/dd/yyyy.");
			return false;
		}
		
		month = matchArray[1];
		day = matchArray[3];
		year = matchArray[5];
		
		if (month < 1 || month > 12) {
			//alert("The "+field+" Date month, must be between 1 and 12.");
			return false;
		}
		if (day < 1 || day > 31) {
			//alert("The "+field+" Date day, must be between 1 and 31.");
			return false;
		}
		if ((month==4 || month==6 || month==9 || month==11) && day==31) {
			//alert("The "+field+" Date month, "+month+" doesn`t have 31 days!")
			return false;
		}
		if (month == 2) {
			var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
			if (day > 29 || (day==29 && !isleap)) {
				//alert("The "+field+" Date February "+year+", doesn`t have "+day+" days!");
				return false;
			}
		}
		
		return true;
	}
	
	function dateAdd(intval, numb, oDate){
		switch(intval){
			case "M":
				base.setMonth(base.getMonth() + numb);
				break;
			case "YYYY":
				base.setFullYear(base.getFullYear() + numb);
				break;
			case "D":
				base.setDate(base.getDate() + numb);
				break;
			case "H":
				base.setHours(base.getHours() + numb);
				break;
			case "N":
				base.setMinutes(base.getMinutes() + numb);
				break;
			case "S":
				base.setSeconds(base.getSeconds() + numb);
				break;
			default:
		}
		return base
	}
	
// ============================================================ FUNCTIONS: CALENDAR CONTROLS ============================================================
	
	function getCalendar(oSrc, oFrmField) {
		var pos = get_XYcoordinates(oSrc);
		//var url = (location.href.search('https') == -1)? 'http://':'https://';
		var url = cmpntPath+"dlg-calendar.asp?the_field="+oFrmField.name+"&the_date="+oFrmField.value+"&start_date="+oFrmField.value;
		
		if (!document.getElementById('DIVdialogCalendarBG')) document.body.innerHTML = document.body.innerHTML + '<div id="DIVdialogCalendarBG" style="position:absolute; width:100%; Height:'+document.body.scrollHeight+'; left:0px; top:0px; z-index:1;" onclick="getCalendarExit(\'\',\'\');"></div>';
		if (!document.getElementById('DIVdialogCalendar')) document.body.innerHTML = document.body.innerHTML + '<div id="DIVdialogCalendar" style="border:2px #333333 solid; position:absolute; left:'+pos.x+'px; top:'+pos.y+'px; z-index:2;"></div>';
		
		switch(oSrc) {
			default:
				setCalendarPos(pos.x - 198, pos.y + 22);
				break;
		}
		document.getElementById('DIVdialogCalendar').innerHTML = '<iframe unselectable="on" id="IFRAMEdialogCalendar" name="IFRAMEdialogCalendar" src="'+url+'" width="210" height="195" marginheight="0" marginwidth="0" frameBorder="0" noresize scrolling="no"></iframe>';
		document.getElementById('DIVdialogCalendarBG').style.display = "block";
		document.getElementById('DIVdialogCalendar').style.display = "block";
	}

	function getCalendarExit(oFrmField, oDate) {
		document.getElementById('DIVdialogCalendarBG').style.display = "none";
		document.getElementById('DIVdialogCalendar').style.display = "none";
		
		if (document.getElementById(oFrmField)) document.getElementById(oFrmField).value = oDate;
	}
	
	function setCalendarPos(oX, oY) {
		document.getElementById('DIVdialogCalendar').style.left = oX+'px';
		document.getElementById('DIVdialogCalendar').style.top = oY+'px';
	}

// ============================================================ FUNCTIONS: COORIDNATES ============================================================	
	
	function get_XYcoordinates(element) {
		var useWindow = false;
		var coordinates = new Object();
		var x = 0;
		var y = 0;
		var use_gebi = false;
		var use_css = false;
		var use_layers = false;
		
		if (document.getElementById) use_gebi = true;
		else if (document.all) use_css = true;
		else if (document.layers) use_layers = true;
		
		if (use_gebi && document.all) {
			x = get_Xposition(document.all[element]);
			y = get_Yposition(document.all[element]);
		} else if (use_gebi) {
			x = get_Xposition(document.getElementById(element));
			y = get_Yposition(document.getElementById(element));
		} else if (use_css) {
			x = get_Xposition(document.all[element]);
			y = get_Yposition(document.all[element]);
		} else if (use_layers) {
			var found = 0;
			for (var i=0; i<document.anchors.length; i++) {
				if (document.anchors[i].name==element) {
					found = 1; 
					break;
				}
			}
	
			if (found == 0) {
				coordinates.x = 0;
				coordinates.y = 0;
				return coordinates;
			}
			
			x = document.anchors[i].x;
			y = document.anchors[i].y;
		} else {
			coordinates.x = 0;
			coordinates.y = 0;
			return coordinates;
		}
		
		coordinates.x = x;
		coordinates.y = y;
		return coordinates;
	}
	
	function get_Xposition (element) {
		var ol = element.offsetLeft;
		while ((element = element.offsetParent) != null) {
			ol += element.offsetLeft;
		}
		return ol;
	}
	
	function get_Yposition (element) {
		var ot = element.offsetTop;
		while((element = element.offsetParent) != null) {
			ot += element.offsetTop;
		}
		return ot;
	}
	
	function get_XYscrollPos() {
		var scrollX, scrollY;
      
		if (document.all) {
			if (!document.documentElement.scrollLeft) scrollX = document.body.scrollLeft;
			else scrollX = document.documentElement.scrollLeft;
		  
			if (!document.documentElement.scrollTop) scrollY = document.body.scrollTop;
			else scrollY = document.documentElement.scrollTop;
		} else {
			scrollX = window.pageXOffset;
			scrollY = window.pageYOffset;
		}
		
		var scroll = new Object();
		scroll.x = scrollX;
		scroll.y = scrollY;
		
		return scroll;
	}

// ============================================================ FUNCTIONS: AJAX CONTROLS ============================================================

	function getFormVars(oForm)  {
		var sendVars = '';
		for(i=0; i<oForm.elements.length; i++)
			sendVars += oForm.elements[i].name+'='+escape(oForm.elements[i].value)+'&';
		
		return sendVars;
	}
	
	function loadAJAX(oEl, oCom, oParams) {
		if (document.getElementById(oEl))
			document.getElementById(oEl).innerHTML = '<img src="'+cmpntPath+'assets/images/icon_loader.gif" align="middle" style="margin-right:5px; width:24px; height:24px;" /> <strong>Processing...</strong>';
		
		var http = false;			
				
		if(window.ActiveXObject) http = new ActiveXObject("Microsoft.XMLHTTP");
		else if (window.XMLHttpRequest) http = new XMLHttpRequest();
		
		if (!http) { alert('Sorry, your Web Browser is not configured to work with this feature.'); return; }
		
		http.open("POST", oCom, true);
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		
		http.onreadystatechange = function() {
			if(http.readyState == 4) document.getElementById(oEl).innerHTML = http.responseText;
		}
		
		http.send(oParams);
	}
	
// ============================================================ FUNCTIONS: ReCaptcha ============================================================
	
	function valRecaptcha(oForm) {
		if (oForm.recaptcha_response_field.value == '') { formError('Please enter a ReCaptcha code continue.', oForm.recaptcha_response_field); return false; }
		
		var params = ''
			+'actiontype=captcha'
			+'&frmIdno='+oForm.frmIdno.value
			+'&remoteip='+oForm.remoteip.value
			+'&challenge='+oForm.recaptcha_challenge_field.value
			+'&response='+oForm.recaptcha_response_field.value;
	
		var http = false;
				
		if(window.ActiveXObject) http = new ActiveXObject("Microsoft.XMLHTTP");
		else if (window.XMLHttpRequest) http = new XMLHttpRequest();
		
		if (!http) { alert('Sorry, your Web Browser is not configured to work with this feature.'); return; }
		
		http.open("POST", cmpntPath+'/sm-forms.asp', true);
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		
		http.onreadystatechange = function() {
			if(http.readyState == 4) {
				if(http.responseText == 'false') { formError('The ReCaptcha Code entered was incorrect!\nPlease verify the ReCaptcha code continue.', oForm.recaptcha_response_field); Recaptcha.reload(); return false; }
				else valSMForm_send(oForm);
			}
		}
		
		http.send(params);
	}