function checkValues() {
  with ( document.input ) {
		if (!checkPhone(home_country, home_areacode, home_phone) ||
				!checkPhone(business_country, business_areacode, business_phone) ||
				!checkPhone(cell_country, cell_areacode, cell_phone) ||
				!checkPhone(other_country, other_area, other_phone))
			return false;
		for (var i=2; i<=7; i++) {
			var country = eval("other_country_" + i);
			var area = eval("other_area_" + i);
			var phone = eval("other_phone_" + i);
			if (!checkPhone(country, area, phone))
				return false;
		}
  }
  return true;
}

function checkIntPhone(phone, international, mailboxID) {
	var p = phone.replace(/(ext\.?|\*| X )/gi, " x ");
	p = p.replace(/[^0-9x\+]/g, "");

	if (international) {
		/* (p.length < 10 && !(p.length == 9 && p.substr(0, 3) == '253')) */
		if (p.length < 7) {
			alert("Invalid phone number");
			return false;
		}
	}
	else {
		var isInternational;
		isInternational = false;
		
		/*
		if (phone.charAt(0) == '+' && p.charAt(0) != '1') {
			isInternational = true;
		}
		*/

		var d;
		d = new Date();
		var res;
		var ajax = new RCJSAjax("/setup/check_int_number.asp?anticache=" + d.getTime() + "&number=" + encodeURIComponent(p) + (typeof(mailboxID) != "undefined" && mailboxID != "" ? "&mailboxID=" + mailboxID : ""),
			{
				onsuccess : function (result) {
					res = RCJSON_parse(result);
				}
			},
			false);
		ajax.HTTPRequest();
		isInternational = res;
		
		if (isInternational) {
			var msg = "International calling is currently disabled.\n"; // Please enter only US or Canadian phone numbers.
			if (typeof(bAdmin) != 'undefined') {
				if (bAdmin)
					msg += "To enable International calling, please contact " + displayName + 
							" Customer Service" + (typeof(csNumber) != 'undefined' ? " at " + csNumber : "") + ".";
				else
					msg += "To enable International calling, please contact your " + displayName + " account administrator.";
			}
			alert(msg);
			return false;
		}
	
		/*	
		if (p.length < 10 && !(p.length == 9 && p.substr(0, 3) == '253')) {
			alert("Invalid phone number");
			return false;
		}
		*/
		
		/*
		if (!p.match(/^(1\d{10}|[2-9]\d{9})(((\*|x){1}\d{1,6})|$)$/)) {
			alert("Invalid phone number");
			return false;
		}
		*/
	}
	return true;
}

function checkIntPhoneEx(phone_obj, international, mailboxID, lengthCheck) {
	if (phone_obj.value.match(/^\s*\S+@[^\s@]+\.[^\s@]+\s*$/))
		return true;
	
	if (lengthCheck == '1') {
		var p = phone_obj.value.replace(/(ext\.?|\*| X )/gi, " x ");
		p = p.replace(/[^0-9]/g, "");
		if (p.length > 0 && p.length < 5) {
			phone_obj.focus();
			alert("Invalid phone number");
			return false;
		}
	}
	
	if (phone_obj.value != "" && !checkIntPhone(phone_obj.value, international, mailboxID)) {
		phone_obj.focus();
		return false;
	}

	if (typeof(arPhones) != 'undefined' && phone_obj.value != "") {
		var p = parsePhoneNumber(phone_obj.value.replace(/[^\d]/g, ""), "yes");
		for (var i=0; i<arPhones.length; i++) {
			if (p == arPhones[i]) {
				alert(GetLang("Page.SendFax.PhoneNumberCannotBeTheSame"));
				phone_obj.focus();
				return false;
			}
		}
	}
	return true;
}

function editPhoneNumber(callback, phone, international) {
	var w = window.open("/setup/edit_phone.asp?phone=" + encodeURIComponent(phone) + "&callback=" + callback + "&international=" + international, 'EditPhone', 'width=410,height=250,left=175,top=250,scrollbars=no');
	w.focus();
}

function editPhoneNumberGen(callback, phone, international, BrandID) {
	var w = window.open("/setup/edit_phone_1.asp?phone=" + encodeURIComponent(phone) + "&callback=" + callback + "&international=" + international + (BrandID != "" ? "&s=" + BrandID : ""), 'EditPhone', 'width=410,height=250,left=175,top=250,scrollbars=no');
	w.focus();
}

function editPhoneNumberEx(callback, phone, callback2, label, international) {
	var w = window.open("/setup/edit_phone.asp?phone=" + encodeURIComponent(phone) + "&callback=" + callback + "&label=" + escape(label) + "&callback2=" + callback2 + "&international=" + international, 'EditPhone', 'width=410,height=300,left=175,top=250,scrollbars=no');
	w.focus();
}

function setValue(param, value) {
	eval (param + ".value = '" + value + "'");
}

function checkPhone(country, area, phone) {
	if (area.value != "" || phone.value != "") {
		if (country.value == "" || country.value == "1") {
			if (!area.value.match(/^\d{3}$/)) {
				alert("Invalid area code");
				area.focus();
				return false;
			}
			var p = phone.value.replace(/[^\d]/g, "");
			if (!p.match(/^\d{7}$/)) {
				alert("Invalid phone number");
				phone.focus();
				return false;
			}
		}
		else {
			if (!area.value.match(/^\d+$/)) {
				alert("Invalid area code");
				area.focus();
				return false;
			}
			var p = phone.value.replace(/[^\d]/g, "");
			if (!p.match(/^\d+$/)) {
				alert("Invalid phone number");
				phone.focus();
				return false;
			}
		}
	}
	else if (country.value != "") {
		alert("Please specify country code, area code and phone number");
		area.focus();
		return false;
	}
	return true;
}

function checkForwardPhones(strMailboxID, arFwdPhones, arNames, maxAllowed) {
	var url = document.location.protocol + "//" + document.location.host + "/setup/check_forward_phone.asp";
	var req;
	
	url += "?mbid=" + strMailboxID + "&max_allowed=" + maxAllowed;
	for (var i=0; i<arFwdPhones.length; i++)
		if (arFwdPhones[i].value)
			url += "&" + arFwdPhones[i].name + "=" + escape(arFwdPhones[i].value);
	
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.open("GET", url, false);
		req.send(null);
	} else {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		req.open("GET", url, false);
		req.send();	
	}
	
	if (req.status != 200)
		return true;
		
	if (!req.responseXML)
		return true;
		
	var xml = req.responseXML;
	var fieldName = xml.documentElement.getElementsByTagName("field_name")[0].text;
	if (fieldName != "") {
		for (var i=0; i<arFwdPhones.length; i++) {
			if (arFwdPhones[i].name == fieldName) {
				alert("Your " + arNames[i].value + " number is already being used in the system. Please enter another number.");
				arFwdPhones[i].focus();
				return false;
			}
		}
	}
	
	return true;
}

function add(sType, nType) {
	with ( document.input ) {
		var area, phone
		switch (sType) {
			case "Home":		area = home_areacode; phone = home_phone; break;
			case "Work":		area = business_areacode; phone = business_phone; break;
			case "Mobile":	area = cell_areacode; phone = cell_phone; break;
			case "Phone 1":	area = other_area; phone = other_phone; break;
			case "Phone 2":	area = other_area_2; phone = other_phone_2; break;
			case "Phone 3":	area = other_area_3; phone = other_phone_3; break;
			case "Phone 4":	area = other_area_4; phone = other_phone_4; break;
			case "Phone 5":	area = other_area_5; phone = other_phone_5; break;
			case "Phone 6":	area = other_area_6; phone = other_phone_6; break;
			case "Phone 7":	area = other_area_7; phone = other_phone_7; break;
		}
		if (!area.value && !phone.value || !checkPhone(area, phone))
			return;
		for (var i=0; i<orders.length; i++) {
			if (orders.options[i].text == sType)
				return;
		}
		orders.length++;
		orders.options[orders.length-1].text = sType;
		orders.options[orders.length-1].value = nType;
	}
}

function add_selected() {
	exchange_selected(document.input.phones, document.input.orders);
}

function remove_type(sType) {
	var i;
	var values = new Array();
	var texts = new Array();
	var o = document.input.orders;
	for (i=0; i<o.length; i++) {
		if (o.options[i].text != sType) {
			values[values.length++] = o.options[i].value;
			texts[texts.length++] = o.options[i].text;
		}
	}
	o.length = values.length;
	for (i=0; i<o.length; i++) {
		o.options[i].value = values[i];
		o.options[i].text = texts[i];
	}
}

function remove() {
	var i;
	var values = new Array();
	var texts = new Array();
	var o = document.input.orders;
	if (o.selectedIndex == -1) {
		alert("Please select a number");
		o.focus();
		return;
	}
	for (i=0; i<o.length; i++) {
		if (i != o.selectedIndex) {
			values[values.length++] = o.options[i].value;
			texts[texts.length++] = o.options[i].text;
		}
	}
	o.length = values.length;
	for (i=0; i<o.length; i++) {
		o.options[i].value = values[i];
		o.options[i].text = texts[i];
	}
}

function remove_selected() {
	exchange_selected(document.input.orders, document.input.phones);
}

function exchange_selected(from, to) {
	var i;
	var values = new Array();
	var texts = new Array();
	var value, text;

	if (from.selectedIndex == -1) {
		alert("Please select a number");
		from.focus();
		return;
	}

	for (i=0; i<from.length; i++) {
		if (i != from.selectedIndex) {
			values[values.length++] = from.options[i].value;
			texts[texts.length++] = from.options[i].text;
		}
		else {
			value = from.options[i].value;
			text = from.options[i].text;
		}
	}
	from.length = values.length;
	for (i=0; i<from.length; i++) {
		from.options[i].value = values[i];
		from.options[i].text = texts[i];
	}
	
	to.length++;
	to.options[to.length-1].text = text;
	to.options[to.length-1].value = value;
}

function moveUp() {
  with ( document.input ) {
    var idx = orders.selectedIndex;
		if ( idx == -1 ) {
			alert("Please select a number");
			orders.focus();
			return;
		}
    if ( idx > 0 ) {
      var text = orders.options[idx].text;
      var value = orders.options[idx].value;
      orders.options[idx].text = orders.options[idx-1].text;
      orders.options[idx].value = orders.options[idx-1].value;
      orders.options[idx-1].text = text;
      orders.options[idx-1].value = value;
      orders.selectedIndex = idx-1 ;
    }
  }
}

function moveDown() {
  with ( document.input ) {
    var idx = orders.selectedIndex;
		if ( idx == -1 ) {
			alert("Please select a number");
			orders.focus();
			return;
		}		
    if ( idx >= 0 && idx < orders.length-1 ) {
      var text = orders.options[idx].text;
      var value = orders.options[idx].value;
      orders.options[idx].text = orders.options[idx+1].text;
      orders.options[idx].value = orders.options[idx+1].value;
      orders.options[idx+1].text = text;
      orders.options[idx+1].value = value;
      orders.selectedIndex = idx+1;
    }
  }
}

function sendData() {
  if (checkValues()) {
		with (document.input) {
			if (typeof(orders) != 'undefined') {
				for (var i=1; i<=orders.length; i++) {
					switch (parseInt(orders.options[i-1].value)) {
						case 9: home_order.value = i; break;
						case 2: bus_order.value = i; break;
						case 3: cell_order.value = i; break;
						case 17: other_order.value = i; break;
						case 18: other_order_2.value = i; break;
						case 19: other_order_3.value = i; break;
						case 20: other_order_4.value = i; break;
						case 21: other_order_5.value = i; break;
						case 22: other_order_6.value = i; break;
						case 23: other_order_7.value = i; break;
					}
				}
			}
   		submit();
		}
	}
}

