// JavaScript Document
var b = new BrowserInfo();
function BrowserInfo()
{

  this.name = navigator.appName;
  this.codename = navigator.appCodeName;
  this.version = navigator.appVersion.substring(0,4);
  this.platform = navigator.platform;
  this.javaEnabled = navigator.javaEnabled();
  this.screenWidth = screen.width;
  this.screenHeight = screen.height;

}
function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
	
	//alert(eval('obj.'+theProp));
  }
}
// find object within document
function MM_findObj(n, d) { //v4.01
  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 && d.getElementById) x=d.getElementById(n); return x;
}
// validate by expression
function testPattern(pattern,inputString){
  // return true if test results is true
  if(pattern.test(inputString)){
    return true;
   }else{
    return false;
   }
}
function matchValue(val1,val2){
	if(val1 != '' && val2 != ''){
		if(val1 != val2){
			return true;
		}else{ 
			return false;
		}
	}else{
		return true;
	}
}
// validate email address
function validateEmailAddress(email){
  var pattern = /^[_a-z0-9-]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})/;
  // if valid return false
  if(testPattern(pattern,email)){
    return false;
  }else{
    return true;
  }
}
// validate phone number
function validatePhoneNumber(number){
  if(number.length >= 7){
    // string pattern test for numerals only
     var searchForNumbers = /\D+\_+\W+\s+\S+/;
    // validate that the number is all numerals
    return testPattern(searchForNumbers,number);
  }else{
    return true;
  }
}
// validate form
function checkForm(){
  // error manager
  var error = false;
  // inti
  var theform = '';
  var noElements = 0;
  // get form object
  if(arguments.length>0){
    theform = checkForm.arguments[0];
	noElements = (checkForm.arguments.length) - 1; // get no of elements from function
  }else{
    // find default form
    theform =  document.forms[0];
	// find # of elements
	noElements = theform.elements.length; // get no of elements from form
  }
  // loop true elements to validate
  forLoop:
  for(var i=0; i<noElements;i++){
    // get element object
	if(arguments.length>0){
		var field = MM_findObj(arguments[i+1]);
	}else{
    	var field = theform.elements[i];
	}
    // if the field is a required field
   if(field.type != 'submit' && field.type != 'hidden' && !field.disabled){
       if(field.className == 'required'){
        // check value
        if(error = checkValue(field.name,field.value)) break forLoop; // exit for loop
      }
    }
  }
  if(!error){
    theform.submit(); // i have to query this
  }else{
    return false;
  }
}
// check the values of the field name
function checkValue(fname,fvalue){
  if(fvalue == ''){
    sendAlert('Error: '+fname+'...Please Enter a Value for this Field!');
    return true;
  // validate phone number
  }else if(fname == 'yourphone'){
    if(validatePhoneNumber(fvalue)){
      sendAlert('Please Enter a (7) or (10) digit format for this Field!');
      return true;
    }else{
      return false;
    }
  // check for valid email
  }else if(fname == 'email'){
    if(validateEmailAddress(fvalue)){
      sendAlert('Please Enter a Valid Email Address!');
      return true;
    }else{
      return false;
    }
	// check for valid password
  }else if(fname == 'password'){
	var matchingField = MM_findObj('cpassword');
    if(matchValue(fvalue,matchingField.value)){
      sendAlert("Error Passwords don't match!");
      return true;
    }else{
      return false;
    }
  }else{
    return false;
  }
}
// alert user of error
function sendAlert(msg){
  window.alert(msg);
}
// universal confirmation pop up
// messge, function, paramter
function Confirm_Alert(){
	if (confirm(arguments[0])) {
    	if(arguments.length>0){
			eval(arguments[1]+'(arguments[2]);');
		}
	}else{
		//return false;
	}
}
function getUrl(url){
	document.location.href = url;
}
// generic pop up
var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height)
{
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = window.open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}
// hide show element
function toggleElement(){
	// set display based on broswer type
	if(b.name == "Netscape")
		stylePropValue = 'table-row';
	else stylePropValue = 'inline';
	MM_changeProp(arguments[0],'','style.display',stylePropValue);
	// hide other related elements
	for(i=1;i<arguments.length;i++){
		MM_changeProp(arguments[i],'','style.display','none');
	}
}
// pare url to get query string paramteres
function parsequery(){ 
	var parameters = new Array();
	var query = location.search.substring(1); 
	if(query == "") { 
		//alert('no url string');
		return null; 
	} 
	var pairs = query.split("\&"); 
	for(var i = 0; i < pairs.length; i++){ 
		var pos = pairs[i].indexOf('=');  
		if(pos == -1){ 
			continue; 
		} 
		var arg_name = pairs[i].substring(0,pos); 
		var arg_value = pairs[i].substring(pos+1);
		parameters[arg_name] = arg_value; 
	} 
	return parameters; 
}
function createLink(path,element){
		eval(element).value = path;
}
// pop up image
function popUpImage(imageURL,imageTitle){
	PositionX = 100;
	PositionY = 100;
	// Set these value approximately 20 pixels greater than the
	// size of the largest image to be used (needed for Netscape)
	defaultWidth  = 500;
	defaultHeight = 500;
	// Set autoclose true to have the window close automatically
	// Set autoclose false to allow multiple popup windows
	var AutoClose = true;
	if (parseInt(navigator.appVersion.charAt(0))>=4){
		var isNN=(navigator.appName=="Netscape")?1:0;
		var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;
	}
	var optNN='scrollbars=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
	var optIE='scrollbars=no,width=150,height=100,left='+PositionX+',top='+PositionY;
	if (isNN){imgWin=window.open('about:blank','',optNN);}
	if (isIE){imgWin=window.open('about:blank','',optIE);}
	with (imgWin.document){
		writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
		writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
		writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
		writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(100,100);');
		writeln('width=100-(document.body.clientWidth-document.images[0].width);');
		writeln('height=100-(document.body.clientHeight-document.images[0].height);');
		writeln('window.resizeTo(width,height);}');writeln('if (isNN){');       
		writeln('window.innerWidth=document.images["image"].width;');writeln('window.innerHeight=document.images["image"].height;}}');
		writeln('function doTitle(){document.title="'+imageTitle+'";}');writeln('</sc'+'ript>');
		if (!AutoClose) writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
		else writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
		writeln('<img name="image" src='+imageURL+' style="display:block"></body></html>');
		close();		
	}
}

function disableButton(status,buttonName){
	var theForm = MM_findObj('listForm');
	var tButton = MM_findObj(buttonName+'button');
	var buttonDisabled = tButton.disabled;
	if(status){
		if(!buttonDisabled){
			sendAlert('You cannot '+buttonName+' item!');
			tButton.disabled = true;
			tButton.src = '/cms/images/icon_'+buttonName+'off.gif';
		}
		return;
	}
	for (var i = 0; i < theForm.elements.length; i++) {
		 if (theForm.elements[i].type == "checkbox") {
			if((theForm.elements[i].title == "main") && (theForm.elements[i].checked) && buttonName == 'delete'){
				sendAlert('You cannot '+buttonName+' item!'); 
				return;
			}else if((theForm.elements[i].title == "#") && (theForm.elements[i].checked) && buttonName == 'publish'){
				sendAlert('You cannot '+buttonName+' item!'); 
				return;
			}
		}
	}
	
	tButton.disabled = false;
	tButton.src = '/cms/images/icon_'+buttonName+'.gif';
}
//->
