function popWindow(windowURL, width, height, window_id)
{
        if( ! width  ) { width = 500 }
        if( ! height ) { height = 400 }
	if (! window_id ) { window_id = "popup" }
	var winl = (screen.width - width) / 2;
	var wint = (screen.height - height) / 2;

        var ops = 'toolbar=0, location=0,directories=0,status=0,menubar=0,resizable=1, scrollbars=1, width='+width+', height='+height+', top='+wint+',left='+winl;

	popWin = window.open( windowURL, window_id , ops );
	popWin.focus();
        return popWin;
}

function popWindowNoReturn( windowURL, width, height, window_id ) {
        win = popWindow( windowURL, width, height, window_id );
}

function option_selector( which ) {

 if( ! which ) { which = 'theForm' }
 var theForm = document[ which ];

 for(var i = 0; i< theForm.elements.length; i++) {
     if(theForm.elements[i].type.indexOf('select') > -1) {
	for( var j=0; j< theForm.elements[i].length; j++ ) {
	   if( theForm.elements[i].options[j].value == theForm.elements[i].id ) {
	       theForm.elements[i].options[j].selected = true;
           }
        }
     }
     if(theForm.elements[i].type.indexOf('checkbox') > -1 ) {
          if( theForm.elements[i].id == 'on' ) {
              theForm.elements[i].checked = true;
          }
     }
 }
}


function view_contact_choice(which) {
     
     var id;
     for(var i = 0; i<document.theForm.elements.length; i++) {
          if(document.theForm.elements[i].name == which) {
             var ind = document.theForm.elements[i].selectedIndex;
             id = document.theForm.elements[i].options[ ind ].value;
          }
     }
     popWindow( '/accounts/contacts/view_contact?contact_id=' + id );
}

function add_contact() {
     popWindow( '/accounts/contacts/add_contact' );
}

function edit_default_contact() {
     popWindow( '/accounts/contacts/edit_default_contact' );
}


function edit_contact_id(id) {
     popWindow( '/accounts/contacts/edit_contact?contact_id=' + id );
}

function view_contact_id(id) {
     popWindow( '/accounts/contacts/view_contact?contact_id=' + id );
}

function modify_contact_id(id){
	popWindow('/accounts/contacts/edit_contact?contact_id='+id);
}   
 
function change_domain_contact( id, which ) {
     popWindow( '/accounts/domains/modify_contact?domain_id='+id+'&contact_type='+which,
                500, 500 );
}

function edit_domain_contact_id( domain_id, contact_id, type ) {
     popWindow( '/accounts/domains/edit_contact?domain_id='+domain_id+'&contact_id='+contact_id+'&contact_type='+type,
                500, 500 );
}


function add_glue( id ) {
     popWindow( '/accounts/domains/add_ns_glue?domain_id='+id,500, 200 );
}

function edit_glue_choice( did ) {

     var nid;
     for(var i = 0; i<document.nsForm.elements.length; i++) {
          if(document.nsForm.elements[i].name == 'glue_id') {
             var ind = document.nsForm.elements[i].selectedIndex;
             nid = document.nsForm.elements[i].options[ ind ].value;
          }
     }

     popWindow( '/accounts/domains/edit_ns_glue?domain_id='+did+'&glue_id='+nid );
}

function delete_glue_choice( did ) {

     var nid;
     for(var i = 0; i<document.nsForm.elements.length; i++) {
          if(document.nsForm.elements[i].name == 'glue_id') {
             var ind = document.nsForm.elements[i].selectedIndex;
             nid = document.nsForm.elements[i].options[ ind ].value;
          }
     }

     popWindow( '/accounts/domains/delete_ns_glue?domain_id='+did+'&glue_id='+nid );
}


var fileProgress;
function postFile( upload_id ) {

        if( document.theForm.domain_list.value == '' ) {
		alert('Please enter a filename');
		return false;
	}
        if( document.theForm.list_name.value == '' ) {
		alert('Please enter a name for this list');
		return false;
	}


	fileProgress = popWindow("/accounts/bulk/upload_status/"+upload_id,460,200);
        document.theForm.submit();
}
function closeFileProgress()
{
	if(fileProgress) fileProgress.close();
}

function data_table_submit( sorter ) {

	document.theForm.sorter.value = sorter;
	document.theForm.submit();
}

function selectCard() {
  // find the option selected
  var selected = document.theForm.cc_choose.selectedIndex;
  var nug = document.theForm.cc_choose.options[selected].id;
  var nugarr = nug.split( ":" );
  var last_four = nugarr[0];
  var mo = nugarr[1];
  if( ! mo ) { mo = 0; }
  var yr = nugarr[2];
  if( yr ) { yr = yr - 2003; }
  else { yr = 0;  }
  var cc_cvv = nugarr[3];
  if( ! cc_cvv ) { cc_cvv = '' }
  var cc_type = nugarr[4];
  document.theForm.cc_year.options[yr].selected = true;
  document.theForm.cc_month.options[mo].selected = true;
  document.theForm.cc_cvv.value = cc_cvv;

  if( cc_type == "VISA" ) {
    document.theForm.cc_num.value = "************ " + last_four;
    document.theForm.cc_type.options[3].selected = true;
  }
  if( cc_type == "MC" ) {
    document.theForm.cc_num.value = "************ " + last_four;
    document.theForm.cc_type.options[2].selected = true;
  }
  if( cc_type == "AMEX" ) {
    document.theForm.cc_num.value = "*********** " + last_four;
    document.theForm.cc_type.options[1].selected = true;
  }
  if( ! cc_type ) {
    document.theForm.cc_num.value = "";
    document.theForm.cc_type.options[0].selected = true;
  }
}


//================================================================
// CSS dropdown menu hack using iframe to overlay select element in IE
var ua = navigator.userAgent.toLowerCase(); 
this.isOpera = (ua.indexOf('opera') != -1);		
this.isIE = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) ); 

function checkIt(string) {
	place = ua.indexOf(string) + 1;
        return place;
}

function useIFrame(state, layerID){
 	if(this.isIE && OS != "Mac"){
		if(state){
			showIFrame(layerID, "block");
		}else{
			showIFrame(layerID, "none");
		}
	}
}

function showIFrame(menu_id, action){
	var elm = document.getElementById(menu_id);
	var elmIframe = document.getElementById('dropList');
	
	//This will be preset for now
	var elmLi = 24.5;
	var elmTop = 129;
	var elmLeft = 19;
	var elmWidth = 14.6;
	var elmNum = elm.getElementsByTagName("LI");
	elmLeft = elmLeft + elm.offsetLeft;
	elmNum = elmNum.length;

	
	var elmHeight = elmNum * elmLi;
	if(elmIframe.style.display == "block")
		elmIframe.style.display = "none";
	else
		elmIframe.style.display = "block";
}

function dropdowns() {
		navRoot = document.getElementById("nav_menu");
		lis = navRoot.getElementsByTagName('li');
		for (i = 0; i < lis.length; i++) {
			lis[i].onmouseover = 	function() { 
				var before = this.className.match(' normal') ? ' normal' : 'normal';
				var after = (before == ' normal') ? ' over' : 'over';
				this.className = this.className.replace(before, after);
			}
			
			lis[i].onmouseout = 	function() { 
				var before = this.className.match(' normal') ? ' normal' : 'normal';
				var after = (before == ' normal') ? ' over' : 'over';
				this.className = this.className.replace(after, before);
			}
		}
}

function prepareDropdown() {
	if(checkIt('mac')){ 
		var OS = "Mac" 
	} else if(checkIt('linux')){ 
		var OS = "Linux" 
	}else if(checkIt('windows')){ 
		var OS = "Windows" 
	}
}
