var zz;

function submitFormFromName(formName){
	return submitAjaxForm(document.getElementById(formName));
}

function submitAjaxForm(theForm){
		var objVal = "";
		var subArr = "";
		var str = "";
		var qArr = new Array();
		var checkedArray = new Array();
		
		for(var i = 0;i < theForm.elements.length;i++){
			str = theForm.elements[i].name;
			if(theForm.elements[i].type == "radio" || theForm.elements[i].type == "checkbox"){
				if(!checkedArray[str]){
					checkedArray[str] = 1;
					var elementlength = theForm.elements[str].length;
					for(var j = 0;j < elementlength;j++){
						if(theForm[str][j].checked)
							qArr[i] = str + "|" + theForm[str][j].value;
					}
				}
			}
			else{
				if(theForm.elements[i].type != "submit" && theForm.elements[i].type != "reset" && str != "formNum"){
					objVal = theForm[str].value;
					if(str)
						qArr[i] = str + "|" + objVal;
				}
			}
		}
		var qStr = qArr.join("~~");
		qStr += "&formNum=" + theForm['formNum'].value;
		updateDataSent(theForm.id + "_div", "sendMail", qStr);
}

function print_r(theObj){
  if(theObj.constructor == Array || theObj.constructor == Object){
    document.write("<ul>")
    for(var p in theObj){
      if(theObj[p].constructor == Array|| theObj[p].constructor == Object){
		document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
        document.write("<ul>")
        print_r(theObj[p]);
        document.write("</ul>")
      } else {
		document.write("<li>["+p+"] => "+theObj[p]+"</li>");
      }
    }
    document.write("</ul>")
  }
}

function validateForm(theForm){
		var mess = "";
		var checkedArray = new Array();
		for(var i = 0;i < theForm.elements.length;i++){
			var el = theForm.elements[i];
			var str = el.name;
			if(el.type == "radio" || el.type == "checkbox"){
				if(!checkedArray[str]){
					found = 0;
					checkedArray[str] = 1;
					var elementlength = theForm[str].length;
					for(var j = 0;j < elementlength;j++){
						if(theForm[str][j].checked){
							found = 1;
						}
					}
					if(!found){
						mess += "\t - " + str.replace(/_/g, " ") + "\n";
						showError(el);
					}
				}
			}
			else{
				if(el.className.indexOf("required") == 0){
					objVal = el.value;
					if(!objVal){
						mess += "\t - " + str.replace(/_/g, " ") + "\n";
						showError(el);
					}
				}
			}
		}
	if(mess){
		alert("The following fields are required:\n\n" + mess);
		return false;
	}
	return true;
}

function showError(element){
 				if (!element.name) element.name = zz++; // we'll need an id here.	
				element.id = element.name + "s";	
				// Add error flag to the field
				element.className += " " + "errFld";
				// Prepare error message
				var msgNode = document.createTextNode("This field is required");
				// Find error message placeholder.
				var fe = document.getElementById(element.id +  "err");
				if(!fe) { // create placeholder.
					fe = document.createElement("div"); 
					fe.setAttribute('id', element.id +  "err");	
					// attach the error message after the field label if possible
					var fl = document.getElementById(element.id +  "err");
					if(fl)
						fl.parentNode.insertBefore(fe,fl.nextSibling);
					else
						// otherwise, attach it after the field tag.
						element.parentNode.insertBefore(fe,element.nextSibling);
				}
				fe.innerHTML = "";	
				// Finish the error message.
				fe.appendChild(msgNode);  	
				fe.className += " " + "validate";
}

function openWin(page, width, height){
	if (!window.focus) return true;
	var win = window.open(page, 'pop', 'width=' + width + ',height=' + height + ',scrollbars=yes');
	win.focus();
	return false;
}

function generalAjaxCall(container, file, qStr){
	updateDataSent(container, file, qStr);
	return false;
}

function openNewsletterWin(arg, width, height, item){
	if (!window.focus) return true;
	if(isNaN(arg))
		arg = document.getElementById(arg).options[document.getElementById(arg).selectedIndex].value;

	if(arg){
		var str = "";
		if(item) str = "&item_id=" + item;
		var win = window.open("/app/newsletter.php?id=" + arg + str, 'pop', 'width=' + width + ',height=' + height + ',scrollbars=yes');
		win.focus();
	}
	return false;
}
