var top_coord = 135;
var left_coord = 8;

var ns;
var ie;
if (navigator.appName == 'Netscape'){
	ns = true;
	doc = document.layers;
	style = '';
	px = '';
	
}
if (navigator.appName == 'Microsoft Internet Explorer'){
	ie = true;
	doc = document.all;
	style = '.style';
	px = 'px';

}



// set up variables based on browser

var open_img = 'imgbuy';
var open_src = 'images/buy_off.gif';
var lastmenu = 'buy';

if (document.layers) {

  var visible = 'show';
  var hidden = 'hide';

} else if (document.all) {

  var visible = 'visible';
  var hidden = 'hidden';
  
}// if block for ns and ie error trapping


function OpenUrl(psUrl)
{
window.open(psUrl)
}


function preload_imgs() {

img_array = new Array('../images/buysell_on.gif', '../images/mag_on.gif', '../images/news_on.gif', '../images/websites_on.gif', '../images/events_on.gif', '../images/training_on.gif', '../images/discussions_on.gif');

for (x = 0; x < img_array.length; x++) {

 imgload = new Image();
 imgload.src = img_array[x];

}//image load loop

}//preload function

function menuoff() {
	if(document.layers) {
	    current_menu = document.layers[lastmenu];
	   	current_menu.visibility = hidden;
		document.layers['menu'].document.images[open_img].src = open_src;
	}
	else {
	current_menu = document.all(lastmenu).style;
	current_menu.visibility = hidden;
	document.all(open_img).src = open_src;
	}
	
}

function menuflip(menuname) {


//define current menu into current_menu var

if(document.layers) {
    current_menu = document.layers[menuname];
    lastmenu = document.layers[lastmenu];

} else if(document.all) {

    current_menu = document.all(menuname).style;
    lastmenu = document.all(lastmenu).style;

}// if block for ns and ie error trapping



//turn old menu off

lastmenu.visibility = hidden;

// change menu state to visible or hidden

if(current_menu.visibility == visible) {

  current_menu.visibility = hidden;


} else {

  current_menu.visibility = visible;

}// if block for change in menu bar state


lastmenu = menuname;

}// function menuflip


function flip_menu(sMenu) {
	var sImg = 'img' + sMenu;
	var sImgOn = 'images/' + sMenu + '_on.gif';
	var sImgOff = 'images/' + sMenu + '_off.gif';
	
	if (document.layers) {
		document.layers['menu'].document.images[sImg].src = sImgOn;
		}
	else {
	    document.all(sImg).src = sImgOn;
	}
	 
	menuflip(sMenu); 
	
	if (sImg == open_img) {
		return;
	}
	else {
		if (document.layers) {
			document.layers['menu'].document.images[open_img].src = open_src;
			}
		else {
		    document.all(open_img).src = open_src;
		}	
		open_img = sImg;
		open_src = sImgOff;
	}
	
}


function img_off(img_now, img_src) {

if (img_now == open_img) return;
if (document.layers) {
	document.layers['menu'].document.images[open_img].src = open_src;
	}
else {
    eval('document.' + open_img + '.src = \'' + open_src + '\'');
}
    open_img = img_now;
    open_src = img_src;
	
}//img_off function



function swapBg(mName, color) {

if(document.layers) {

   //document.layers['buy'].document.elements[mName].bgcolor = color;

} else {

  document.all(mName).style.backgroundColor = color;

}// if block for ns and ie error trapping


}//end swapBg function

function swapImage(sImgName,sImgSrc) {
	if (document.all) {
		document.all(sImgName).src = sImgSrc;
	
	}


}


function moveHD(col,sendtocol) {
	var iValue;
	var iText;
	var iSendToLength;
	
	if (sendtocol.options.length > 0) {
		if (sendtocol.options[sendtocol.options.length-1].value==''){
			sendtocol.options[sendtocol.options.length-1] = null;
		}
	}
	for (i=0; i < col.options.length; i++){
		if (col.options[i].selected) {
			iValue = col.options[col.selectedIndex].value;
			iText = col.options[col.selectedIndex].text;
			iSendToLength = sendtocol.options.length;
			
			sendtocol.options.length = iSendToLength + 1;
			sendtocol.options[iSendToLength].value = iValue;
			sendtocol.options[iSendToLength].text = iText;
			col.options[i]=null;
			i=i-1;
		}
	}
	
}


function EditModule(x)
	{
	    
	childwin = window.open(x,"bar","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,menubar=no,width=800,height=600")
	    
	}
 
 
function preview(target,params) {
	target.location.href = "preview_topheadlines.asp?list=" + params;


}
function go() {
    window.parent.opener.location.href = "/index.asp?editaction=savetophd";
	window.parent.close();
  }
  
function orderModule(down, col) 
{
  sl = col.selectedIndex;
  if (sl != -1 && col.options[sl].value > "") {
    oText = col.options[sl].text;
    oValue = col.options[sl].value;
    if (col.options[sl].value > "" && sl > 0 && down == 0) {
      col.options[sl].text = col.options[sl-1].text;
      col.options[sl].value = col.options[sl-1].value;
      col.options[sl-1].text = oText;
      col.options[sl-1].value = oValue;
      col.selectedIndex--;
    } else if (col.options[sl+1].value > "" && sl < col.length-1 && down == 1) {
      col.options[sl].text = col.options[sl+1].text;
      col.options[sl].value = col.options[sl+1].value;
      col.options[sl+1].text = oText;
      col.options[sl+1].value = oValue;
      col.selectedIndex++;
    }
  } else {
    alert("Please select a module first");
  }
}
 
function doSub() 
{
  layout = "nawi";
  
  for (i=0; i < layout.length; i++) {
    if (layout.substr(i,1) == 'n' || layout.substr(i,1) == 'w') {
      col = layout.substr(i,2); 
      document.formmodules["lst_" + col].value = makeList(col);
    }
  }
 
  return true;
}

function makeMultiList(col) {
	var val = ""; 
	for (i=0; i<col.length; i++) {
		
		for (j=0; j<col[i].length; j++) {
			if (val > "") { val += ", "; }
		  
			if (col[i].options[j].value > "") {
				val = val.replace(col[i].options[j].value,"");
				val += col[i].options[j].value;
			}
		} 
	}
	return val;
}

function makeHDList(col) 
{
  val = "";
	
  for (j=0; j<col.length; j++) {
    
    if (val > "") { val += ", "; }
    if (col.options[j].value > "") {
		val = val.replace(col.options[j].value,"");
		val += col.options[j].value;
	}
  } 

  return val;
}

function makeListNew(col) 
{
  val = "";

  for (j=0; j<col.length; j++) {
    
    if (val > "") { val += ", "; }
    if (col.options[j].value > "") val += col.options[j].value;
  } 
  
  return val;
}

function makeList(col) 
{
  val = "";

  for (j=0; j<document.formmodules[col].length; j++) {
    
    if (val > "") { val += ","; }
    if (document.formmodules[col].options[j].value > "") val += document.formmodules[col].options[j].value;
  } 
  
  return val;
}

function makeHDListReverse(col) 
{
  val = "";

  for (j=col.length-1; j>=0; j--) {
    
    if (val > "") { val += ", "; }
    if (col.options[j].value > "") val += col.options[j].value;
  } 
  
  return val;
}

function emailCheck (emailStr) {

	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	
	if (matchArray==null) {
	  
		//alert("Email address seems incorrect (check @ and .'s)")
		alert("E-mail Address is missing or invalid. Please enter it or check what you have entered.")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]

	if (user.match(userPat)==null) {
	   
	    //alert("The username doesn't seem to be valid.")
	    alert("E-mail Address is missing or invalid. Please enter it or check what you have entered.")
	    return false
	}


	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
	    
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
		        //alert("Destination IP address is invalid!")
		        alert("E-mail Address is missing or invalid. Please enter it or check what you have entered.")
			return false
		    }
	    }
	    return true
	}


	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		//alert("The domain name doesn't seem to be valid.")
		alert("E-mail Address is missing or invalid. Please enter it or check what you have entered.")
	    return false
	}

	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
	    domArr[domArr.length-1].length>3) {
	   
	   //alert("The address must end in a three-letter domain, or two letter country.")
	   alert("E-mail Address is missing or invalid. Please enter it or check what you have entered.")
	   return false
	}


	if (len<2) {
	   var errStr="This address is missing a hostname!"
	   //alert(errStr)
	   alert("E-mail Address is missing or invalid. Please enter it or check what you have entered.")
	   return false
	}

	return true;
}


function validate(field, datatype) {
	if (datatype == "int") {
		var valid = "0123456789"
	}
	else if (datatype == "char") {
		var valid =	"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
	}
	var ok = "yes";
	var temp;
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
	    if (datatype == "int"){
			alert("Invalid entry!  Only numbers are accepted!");
		}
		if (datatype == "char"){
			alert("Invalid entry!  Only characters are accepted!");
		}
		field.focus();
		field.select();
	}
}

function zipcodeCheck(field) 
{
	var valid = "0123456789-";
	var hyphencount = 0;

	if (field.length!=5 && field.length!=10) 
	{
		//alert("Please enter your 5 digit or 5 digit+4 zip code.");
		alert("Zip Code is missing or invalid. Please enter it or check what you have entered.")
		return false;
	}
	
	for (var i=0; i < field.length; i++) 
	{
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") 
		{
			//alert("Invalid characters in your zip code.  Please try again.");
			alert("Zip Code is missing or invalid. Please enter it or check what you have entered.")
			return false;
		}
	
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) 
		{
			//alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'. Please try again.");
			alert("Zip Code is missing or invalid. Please enter it or check what you have entered.")
			return false;
		}
	}
	return true;
}

function phoneCheck(phone)
{
	var phonexp = /^\d{10}$|^\d[\-\s]?\d{3}[\-\s]?[A-Za-z0-9]{3}[\-\s]?[A-Za-z0-9]{4}$|^\(?\d{3}\)?[\-\s]?[A-Za-z0-9]{3}[\-\s]?[A-Za-z0-9]{4}$|^\d{10}\s?[Xx]?\s?\d{2,4}$|^\d[\-\s]?\d{3}[\-\s]?[A-Za-z0-9]{3}[\-\s]?[A-Za-z0-9]{4}\s?[Xx]?\s?\d{2,4}$|^\(?\d{3}\)?[\-\s]?[A-Za-z0-9]{3}[\-\s]?[A-Za-z0-9]{4}\s?[Xx]?\s?\d{2,4}$/
	// \s?[Xx]?\s?\d{2,4} - format validation for extension number
	var pass = phonexp.test(phone)
	
	if (!pass)
	{
		alert("Phone or Fax Number is missing or invalid. Please enter it or check what you have entered.")
		return false
	}
	return true
}

function urlCheck(url)
{
	if (url == "http://")
	{
		alert("Web Site Address is missing or invalid. Please enter it or check what you have entered.")
		return false
	}
	return true
}

function validateRequired(form)
{
	var pass = true;
	var element = null;
	for (var i=0; i < form.length; i++)
	{
		var element = form.elements[i]
		if (element.name.substring(0,8)=="required") 
		{
			if (((element.type=="text" || element.type=="textarea") && element.value=='')
				|| (element.type.toString().charAt(0)=="s" && element.selectedIndex==0))
			{
				pass=false;
				break;
			}
		}
	}
	
	if (!pass)
	{
		if (navigator.appName == 'Netscape')
			alert(element.name.replace(/required_/i,"")+" is missing or invalid. Please enter it or check what you have entered.");
		else if (navigator.appName == 'Microsoft Internet Explorer')		
			alert(element.title+" is missing or invalid. Please enter it or check what you have entered.");
		element.focus();
		return false;
	}
	return true;
}

function validateCountryRequired(form, countryCode)
{
	if (countryCode == '1' || countryCode == '39')
	{
		var pass = true;
		var element = null;
		for (var i=0; i < form.length; i++)
		{
			var element = form.elements[i]
			if (element.name.substring(0,16)=="country_required") 
			{
				if (((element.type=="text" || element.type=="textarea") && element.value=='')
					|| (element.type.toString().charAt(0)=="s" && element.selectedIndex==0))
				{
					pass=false;
					break;
				}
			}
		}
			
		if (!pass)
		{
			if (navigator.appName == 'Netscape')
				alert(element.name.replace(/country_required_/i,"")+" is missing or invalid. Please enter it or check what you have entered.");
			else if (navigator.appName == 'Microsoft Internet Explorer')		
				alert(element.title+" is missing or invalid. Please enter it or check what you have entered.");
			element.focus();
			return false;
		}
		return true;
	}
	return true;
}

function fixLayout()
{
	if (document.all && document.images['space'])
		document.images['space'].height = document.body.scrollHeight
}

function getWordCount(expression)
{
    var words
               
    words = new Array(",", "~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "=", "{", "}", "[", "]", "|", "\\", ";", ":", "\"", "\'", "<", ">", ".", "/", "?")
        
    for(var i = 0; i < words.length-1; i++)
    {
        var re = /\s?words[i]\s?/
        expression = expression.replace(re, " ")
    }
    
    return expression.split(" ").length;
}
