function checkForm(evnt) {
    if (typeof(required) != "undefined") {
	var me = (evnt.srcElement) ? evnt.srcElement : this;
	var stop = false;

	for (a = 0; a < required.length; a++) {
		var reqd = required[a].split(":");
		if (me.elements[reqd[0]]) {
			var val = me.elements[reqd[0]];
			if (reqd[1] == "date") {
				var pattern = /^\d\d?[\-\/]\d\d?[\-\/]\d\d\d\d$/;
				if (!val.value || (val.value && !val.value.match(pattern))) stop = val;
			} else if (reqd[1] == "email") {
				var pattern = /^[\w\d]+[\w\d\.\-_]*\@[\w\d]+[\w\d\.\-_]*\.\w+$/;
				if (!val.value.match(pattern)) stop = val;
			} else if (reqd[1] == "number") {
				val.value = val.value.replace(/[^\d\.]/g, "");
				if (!val.value) stop = val;
			} else if (reqd[1] == "phone") {
				stop = checkPhone(val)
			} else {
				stop = checkBlank(val);
			}
			if (stop) break;
		}
	}

	if (stop) {
		alert(reqd[2]);
		stop.focus();
		if (evnt.preventDefault != undefined) evnt.preventDefault();
		return false;
	}
    }
    return true;
}

function checkBlank(field) {
	// Text Field Validation
	if (field.type == "text" || field.type == "textarea" || field.type == "password" || field.type == "hidden") {
		if (field.value == "") return field;

	// File Uploads
	} else if (field.type == "file") {
		if (!field.value) return field;
	
	
	// Single Checkboxes
	} else if (field.checked != undefined) {
		if (!field.checked) return field;

	// Select Boxes
	} else if (field.selectedIndex != undefined) {
		if (field.selectedIndex == -1 || field[field.selectedIndex].value == "") return field;

	// Radio Buttons and Multiple Checkboxes
	} else if (field.length) {
		var checked = false;
		for (var i=0; i < field.length; i++) {
			if (field[i].checked) checked = true;
		}
		if (!checked) return field[0];
	}
	return false;
}

function checkPhone (you) {
	var cc = {
		"Afghanistan": 93, "Albania": 355, "Algeria": 213, "American Samoa": 1, "Andorra": 376, "Angola": 244, "Anguilla": 1, "Antigua and Barbuda": 1, "Argentina": 54, "Armenia": 374, "Aruba": 297, "Ascension Island": 247, "Australia": 61, "Austria": 43, "Azerbaijan": 994,
		"Bahamas": 1, "Bahrain": 973, "Bangladesh": 880, "Barbados": 1, "Belarus": 375, "Belgium": 32, "Belize": 501, "Benin": 229, "Bermuda": 1, "Bhutan": 975, "Bolivia": 591, "Bosnia and Herzegovina": 387, "Botswana": 267, "Bouvet Island": 0, "Brazil": 55, "British Indian Ocean Territory": 0, "Brunei Darussalam": 673, "Bulgaria": 359, "Burkina Faso": 226, "Burundi": 257,
		"Cambodia": 855, "Cameroon": 237, "Canada": 1, "Cape Verde": 238, "Cayman Islands": 1, "Central African Republic": 236, "Chad": 235, "Channel Islands": 0, "Chile": 56, "China": 86, "Christmas Island": 61, "Cocos (Keeling) Islands": 61, "Colombia": 57, "Comoros": 269, "Congo": 242, "Cook Islands": 682, "Costa Rica": 506, "Croatia": 385, "Cuba": 53, "Curacao": 599, "Cyprus": 357, "Czech Republic": 420,
		"Denmark": 45, "Djibouti": 253, "Dominica": 1, "Dominican Republic": 1,
		"East Timor": 0, "Ecuador": 593, "Egypt": 20, "El Salvador": 503, "Equatorial Guinea": 240, "Eritrea": 291, "Estonia": 372, "Ethiopia": 251,
		"Falkland Islands (Malvinas)": 500, "Faroe Islands": 298, "Fiji": 679, "Finland": 358, "France": 33, "French Guiana": 594, "French Polynesia": 689, "French Southern Territories": 0,
		"Gabon": 241, "Gambia": 220, "Georgia": 995, "Germany": 49, "Ghana": 233, "Gibraltar": 350, "Greece": 30, "Greenland": 299, "Grenada": 1, "Guadeloupe": 590, "Guam": 1, "Guatemala": 502, "Guinea": 224, "Guinea-Bissau": 245, "Guyana": 592,
		"Haiti": 509, "Heard and McDonald Islands": 0, "Holy See / Vatican City": 39, "Honduras": 504, "Hong Kong": 852, "Hungary": 36,
		"Iceland": 354, "India": 91, "Indonesia": 62, "Iran": 98, "Iraq": 964, "Ireland": 353, "Israel": 972, "Italy": 39, "Ivory Coast": 225,
		"Jamaica": 1, "Japan": 81, "Jordan": 962,
		"Kazakhstan": 7, "Kenya": 254, "Kiribati": 686, "Korea": 850, "Korea, Democratic People's Rep": 82, "Kuwait": 965, "Kyrgyzstan": 996,
		"Laos": 856, "Latvia": 371, "Lebanon": 961, "Lesotho": 266, "Liberia": 231, "Libyan Arab Jamahiriya": 218, "Liechtenstein": 423, "Lithuania": 370, "Luxembourg": 352,
		"Macau": 853, "Macedonia": 389, "Madagascar": 261, "Malawi": 265, "Malaysia": 60, "Maldives": 960, "Mali": 223, "Malta": 356, "Marshall Islands": 692, "Martinique": 596, "Mauritania": 222, "Mauritius": 230, "Mayotte": 269, "Mexico": 52, "Micronesia": 691, "Moldova, Republic of": 373, "Monaco": 377, "Mongolia": 976, "Montserrat": 1, "Morocco": 212, "Mozambique": 258, "Myanmar": 95,
		"Namibia": 264, "Nauru": 674, "Nepal": 977, "Netherlands": 31, "Netherlands Antilles": 599, "New Caledonia": 687, "New Zealand": 64, "Nicaragua": 505, "Niger": 227, "Nigeria": 234, "Niue": 683, "Norfolk Island": 672, "Northern Mariana Islands": 1, "Norway": 47,
		"Oman": 968,
		"Pakistan": 92, "Palau": 680, "Palestinian Authority": 970, "Panama": 507, "Papua New Guinea": 675, "Paraguay": 595, "Peru": 51, "Philippines": 63, "Pitcairn Islands": 0, "Poland": 48, "Portugal": 351, "Puerto Rico": 1,
		"Qatar": 974,
		"Reunion": 262, "Romania": 40, "Russian Federation": 7, "Rwanda": 250,
		"Samoa": 0, "San Marino": 378, "Sao Tome and Principe": 239, "Saudi Arabia": 966, "Senegal": 221, "Serbia and Montenegro": 381, "Seychelles": 248, "Sierra Leone": 232, "Singapore": 65, "Slovakia": 421, "Slovenia": 386, "Solomon Islands": 677, "Somalia": 252, "South Africa": 27, "South Georgia/South Sandwich": 0, "Spain": 34, "Sri Lanka": 94, "St. Helena": 290, "St. Kitts and Nevis": 1, "St. Lucia": 1, "St. Pierre and Miquelon": 508, "St. Vincent and Grenadines": 1, "Sudan": 249, "Suriname": 597, "Svalbard and Jan Mayen Islands": 0, "Swaziland": 268, "Sweden": 46, "Switzerland": 41, "Syrian Arab Republic": 963,
		"Taiwan": 886, "Tajikistan": 992, "Tanzania, United Republic of": 255, "Thailand": 66, "Togo": 228, "Tokelau": 960, "Tonga": 676, "Trinidad and Tobago": 1, "Tunisia": 216, "Turkey": 90, "Turkmenistan": 993, "Turks and Caicos Islands": 1, "Tuvalu": 688,
		"US Minor Outlying Islands": 1, "USA": 1, "Uganda": 256, "Ukraine": 380, "United Arab Emirates": 971, "United Kingdom": 44, "Uruguay": 598, "Uzbekistan": 998,
		"Vanuatu": 678, "Venezuela": 58, "Vietnam": 84, "Virgin Islands (British)": 1, "Virgin Islands (U.S.)": 1,
		"Wallis and Futuna Islands": 681, "Western Sahara": 0,
		"Yemen": 967,
		"Zambia": 260, "Zimbabwe":263 
	};
	you.value = you.value.replace(/\D/g, "");
	you.value = you.value.replace(/^00+/, "");
	var bob = you.value;
	if (typeof(you.form.elements["country"]) != "undefined") country = you.form.elements["country"].value;
	if (cc[country]) {
		var where = bob.indexOf(cc[country]);
		you.value = "+" + ((you.value.indexOf(cc[country]) == 0) ? you.value : cc[country] + you.value);
		var marg = you.value;
		if (cc[country] == 1) {
			if (you.value.length > 12) you.value = you.value.substring(0,12) + " x" + you.value.substring(12);
			if (!you.value.match(/^\+1[2-9]\d{2}[2-9]\d{6}/)) return you;
		} else {
			if (!you.value.match(/^\+\d{3,}/)) return you;
		}
	} else {
		return checkBlank(you);
	}
}

//-------------------------------
// File Upload Processing
//-------------------------------
var _shoretelFileUpload = new Object({
	uploads:new Array(),
	submit:function (evnt) {
		var me = (evnt.srcElement) ? evnt.srcElement : this;

		if (me.elements["newattach"].value) {
			var g = _shoretelFileUpload;
			var id = Math.round(Math.random() * new Date().getTime());

			g.uploads[id] = new g.shoretelUploader(id);
			g.uploads[id].submit(me);
		}

		if (evnt.preventDefault != undefined) evnt.preventDefault();
		return false;
	},
	shoretelUploader:function (id) {
		var a = this, c, fName, fSize, fLastTime = new Date(), fLastBytes = 0, fBarContainer, fBar, g = _shoretelFileUpload, wait, x = getXHR();

		a.submit = function(frm) {
			var a = this;
			var nAv = frm.elements["newattach"].value;
			if (nAv.indexOf('\\') != -1) nAv = nAv.split('\\').pop();

			var p = frm.parentNode;
			c = document.createElement("div");
			c.className = "uploadContainer";
			p.insertBefore(c,frm);

			try {
				var i = document.createElement("<iframe name='fpi" + id + "'/>");
			} catch (e) {
				var i = document.createElement("iframe");
				i.name = "fpi" + id;
			}
			i.style.display = "none";
			startListening(i, "load", whenFinished);
			c.appendChild(i);

			fName = document.createElement("p");
			fName.style.cssFloat = "left";
			fName.style.styleFloat = "left";
			fName.appendChild(document.createTextNode(nAv));
			c.appendChild(fName);

			fBarContainer = document.createElement("div");
			fBarContainer.className = "uploadBarContainer";
			c.appendChild(fBarContainer);

			fBar = document.createElement("div");
			fBar.className = "uploadBar";
			fBarContainer.appendChild(fBar);

			fSize = document.createElement("p");
			fSize.style.cssFloat = "right";
			fSize.style.styleFloat = "right";
			fSize.innerHTML = "Initializing";
			c.appendChild(fSize);

			c.appendChild(document.createElement("hr"));

			frm.target = "fpi" + id;
			frm.elements["PHP_SESSION_UPLOAD_PROGRESS"].value = id;
			frm.submit();

			wait = setTimeout(monitorRequest, 1000);

			frm.reset();
			frm.target = "";
			frm.elements["PHP_SESSION_UPLOAD_PROGRESS"].value = "";
		};
		function monitorRequest () {
			x.open("GET","add_attachment.php?key=" + id, true);
			x.onreadystatechange = monitorResult;
			x.send("");
		}
		function monitorResult () {
			if (x.readyState == 4) {
				if (x.responseText) {
					try {
						var status = eval(x.responseText);

						fBar.style.width = ((status.bytes_processed / status.content_length) * 100) + "%";
						fBar.innerHTML = Math.round((status.bytes_processed / status.content_length) * 100) + "%&nbsp;&nbsp;";

						if (status.done) {
							fSize.innerHTML = "Saving";
							c.removeChild(fBarContainer);
						} else {
							var now = new Date();
							var perSec = (status.bytes_processed - fLastBytes) / (Math.round((now - fLastTime) / 1000));
							fSize.innerHTML = g.roundOut(status.bytes_processed) + " of " + g.roundOut(status.content_length) + " (" + g.roundOut(perSec) + "/sec)";
							fLastTime = now;
							fLastBytes = status.bytes_processed;
							wait = setTimeout(monitorRequest, 1000);
						}
					} catch (e) {
						fSize.innerHTML = "Failed";
						c.removeChild(fBarContainer);
						alert(x.responseText);
					}
				}
			} else {
				return false;
			}
		}
		function whenFinished () {
			fSize.innerHTML = "Done";
		}
	},
	roundOut:function (bytes) {
		if (bytes > 1073741824) {
			return (Math.round((bytes / (1024 * 1024 * 1024)) * 100) / 100) + " GB";
		} else if (bytes > 1048576) {
			return (Math.round((bytes / (1024 * 1024)) * 100) / 100) + " MB";
		} else if (bytes > 1024) {
			return (Math.round((bytes / 1024) * 100) / 100) + " KB";
		} else {
			return bytes;
		}
	}
});

