var str = "left=0,screenX=0,top=0,screenY=0";

if (window.screen) {
  var ah = 450;
  var aw = 650;
  str += ",height=" + ah;
  str += ",innerHeight=" + ah;
  str += ",width=" + aw;
  str += ",innerWidth=" + aw;
  str += ",scrollbars=yes";
  str += ",status=yes";
  str += ",resizable";
} else {
  str += ",resizable"; // so the user can resize the window manually
}

function launch(newURL, newName, newFeatures, orgName) {
	var PopWin = open(newURL, newName, newFeatures);
	if (PopWin.opener == null) // if something went wrong
		PopWin.opener = window;
		PopWin.opener.name = orgName;
	return PopWin;
}

function launchPop(url,tiny) {
	var ah = 450;
	var aw = 650;
	if(tiny){
		ah = 1;
		aw = 1;
	}
	  str += ",height=" + ah;
	  str += ",innerHeight=" + ah;
	  str += ",width=" + aw;
	  str += ",innerWidth=" + aw;
	  str += ",scrollbars=yes";
	  str += ",status=yes";
	  str += ",resizable";
	if (!window.myPop || window.myPop.closed) {
		myPop = launch(url, "Popchild", str, "PopParent");
		myPop.focus();
	} else {
		myPop.location.href = url;
		myPop.focus();
	}
}

function Is() {
    var agent   = navigator.userAgent.toLowerCase();
    this.major  = parseInt(navigator.appVersion);
    this.minor  = parseFloat(navigator.appVersion);
    this.ns     = ((agent.indexOf('mozilla')   != -1) &&
                  (agent.indexOf('spoofer')    == -1) &&
                  (agent.indexOf('compatible') == -1) &&
                  (agent.indexOf('opera')      == -1) &&
                  (agent.indexOf('webtv')      == -1));
    this.ns2    = (this.ns && (this.major      ==  2));
    this.ns3    = (this.ns && (this.major      ==  3));
    this.ns4    = (this.ns && (this.major      ==  4));
    this.ns6    = (this.ns && (this.major      >=  5));
    this.ie     = (agent.indexOf("msie")       != -1);
    this.ie3    = (this.ie && (this.major      <   4));
    this.ie4    = (this.ie && (this.major      ==  4) &&
                  (agent.indexOf("msie 5.0")   == -1));
    this.ie5    = (this.ie && (this.major      ==  4) &&
                  (agent.indexOf("msie 5.0")   != -1));
    this.ieX    = (this.ie && !this.ie3 && !this.ie4);
}

var is = new Is();

function launchWebmail(url) {
	if(is.ns4||is.ns6) {
		available_width=screen.availWidth-100;
		available_height=screen.availHeight-100;
	} else if(is.ie4||is.ie5) {
		available_width=screen.availWidth-100;
		available_height=screen.availHeight-100;
	}
	  var ah = available_height;
	  var aw = available_width;
	  str += ",height=" + ah;
	  str += ",innerHeight=" + ah;
	  str += ",width=" + aw;
	  str += ",innerWidth=" + aw;
	  str += ",scrollbars=yes";
	  str += ",status=yes";
	  str += ",resizable";
	if (!window.webmail || window.webmail.closed) {
		var webmail = window.open (url, 'webmail', str);
	}
	webmail.focus();
}

function trapint(form_name,formField) {
	frm = document.forms[form_name];
	var field = frm.elements[formField];
	if(field.value.length == 0){
		if (event.keyCode==45 || (event.keyCode>46 && event.keyCode<58)) {
			return true;
		} else {
			event.returnValue = false;
			return false;
		}
	}
	else{
		if (event.keyCode>46 && event.keyCode<58) {
			return true;
		} else {
			event.returnValue = false;
			return false;
		}
	}
}
function trapfloat() {
	if (event.keyCode==13 || event.keyCode==8 || (event.keyCode>45 && event.keyCode<58)) {
		return true;
	} else {
		event.returnValue = false;
		return false;
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_displayStatusMsg(msgStr) { //v1.0
  status=msgStr;
  document.MM_returnValue = true;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function restore() {
  location.reload()
}

function ucw(str){
	str = str.replace('_',' ');
	strArray = str.split(' ');
	for(j=0; j<strArray.length; j++){
		strArray[j] = strArray[j].charAt(0).toUpperCase() + strArray[j].substring(1);
	}
	return strArray.join(' ');
}

function validate(form){

        jumpto = "";
        errors = "";
		requiredField = "";
		display = "";

		if(typeof(form.required) != 'undefined' && form.required.value != ""){
			requiredArray = form.required.value.split(",");
			for(i=0; i<requiredArray.length; i++){
				requiredField = requiredArray[i];
				display = ucw(requiredField);

				if(requiredField == 'email_address' || requiredField == 'email' || requiredField == 'username'){
					var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
					var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
					var reg1 = new RegExp(reg1str);
					var reg2 = new RegExp(reg2str);
					if (reg1.test(eval('form.'+requiredField+'.value')) || !reg2.test(eval('form.'+requiredField+'.value'))) {
						errors += 'Please use a valid '+display+':\n';
						if (!jumpto) {
							jumpto = eval('form.'+requiredField);
						}
					}
				}
				else{
					if (eval('form.'+requiredField+'.value') == "") {
						errors += 'Please enter a '+display+':\n';
						if (!jumpto) {
							jumpto = eval('form.'+requiredField);
						}
					}
				}
			}
		}

		if (errors) {
			string = "The following fields were incomplete or invalid. Please complete them to continue.\n\n"+errors;
			alert(string);
			jumpto.focus();
			return false;
		}
		else {
			return true;
		}
}

function confirmDelete(message){
	var agree=confirm('Are You Sure' + message);
	if (agree)
		return true ;
	else
		return false ;
}

function getChildElement(obj, childNum) {
  var child = 0;
  if (obj.hasChildNodes()) {
    for (var i=0; i < obj.childNodes.length; i++) {
      if (obj.childNodes[i].nodeType != 1) continue;
      if (child != childNum) {
         child++;
         continue;
      } else
         return obj.childNodes[i];
    }
  }
  return null;
}

function expandList(e) {
	var src = false;
	if (e.target) {
		src = e.target;
	} else if (e.srcElement) {
		src = e.srcElement;
	} else {
		src = e;
	}
	var child = null, parent;
	if (document.all) {
		parent = document.all[src.sourceIndex -1];
		child = document.all[src.sourceIndex + 1];
	} else {
		parent = src.parentNode;
		child = getChildElement(parent, 1);
	}
	if (null != child && "LI" == parent.tagName && "UL" == child.tagName) {
		parent.className = ("close" == parent.className ? "open" : "close");
		child.className = ('expanded' == child.className ? 'none' : 'expanded');
		var even = true;
		var nextSib = false;
		var childLi = child.firstChild;
		var evenc = "#f3f1f8";
		var oddc = "#e9e5f2";
		if (childLi) {
			childLi.style.backgroundColor = oddc;
			while (nextSib = childLi.nextSibling) {
				nextSib.style.backgroundColor = (even)?evenc:oddc;
				even = ! even;
				childLi = nextSib;
			}
		}
	}
}
function launchContextMenuNoEvent(id) {
	var m = window.open('/cmenu.html?'+id, 'cmenu', 'innerHeight=1,width=100,status=no,resizable');
	m.goFirstItem = false;
	return m;
}
function launchContextMenu(e) {
	var cmenu = false;
	var id = false;
	if (e.target) {
		id = e.target.id;
		e.preventDefault();
	} else if (e.srcElement) {
		id = e.srcElement.id;
		e.returnValue = false;
	}
	if (id.substr(0,11) == 'date_input_') {
		cmenu = show_calendar(id);
	} else {
		cmenu = launchContextMenuNoEvent(id);
	}
	cmenu.moveTo(e.screenX-10, e.screenY-40);
	cmenu.focus();
	return cmenu;
}
function launchFirstContextMenuItem(e) {
	var cmenu = launchContextMenu(e);
	cmenu.goFirstItem = true;
}
function show_calendar(id) {
	url = "/pickdate.html?id=" + id;
	return window.open(url, 'calendar', 'innerHeight=140,width=160,status=no');
}

function copyfields(form,count) {
var i;
var callerv_mail = new Array();
	for (i=0;i<count; i++)
	{
		if (form.copy.checked) {
		form.caller_vmail[i].value = form.customer_name[i].value;
		}
		else {
		form.caller_vmail[i].value = caller_vmail[i];
		   }
	}
}
function copyfield(form) {
var callerv_mail;
		if (form.copy.checked) {
			form.caller_vmail.value = form.fname.value;
		}
		else {
			form.caller_vmail.value = caller_vmail;
		   }
	}
function show(){
	document.getElementById('display_ext').style.display="block";
}
function show2(){
	document.getElementById('pass').style.display="block";
}

function copyGeneral(form,i) {
var field_blank='';
var check;
var field;
	switch(i)
	{
	case 3:
	  	check='ena';
	  	field='mobile_phone';
	  break    
	case 4:
	  	check='copy';
	  	field='vmail';
	  break;
	case 2:
		check='s';
	  	field='seconds';
	  break
	}
	if ((i==2) || (i==3)|| (i==4))
	{
		if (document.getElementById(check).checked) {
			document.getElementById(field).disabled =false;
			if(i==2)  document.getElementById(field).value = 10;
			if(i==3) document.getElementById(field).value = document.getElementById(field).value;
			if(i==4) {
			document.getElementById(field).value = document.getElementById('fname').value+document.getElementById('lname').value;			}
		}
		else {
			document.getElementById(field).value = field_blank;
			document.getElementById(field).disabled = true;
		}
	}
}

