// JavaScript Document

function flashingText(div, class1, class2)
{
	if(document.getElementById(div).className == class1)
	{
		document.getElementById(div).className=class2;
	}
	else
	{
		document.getElementById(div).className=class1;
	}
}

// Preload Over and Down state images
Image1= new Image(10,12);
Image1.src = "../images/buttonbg.gif";
Image2= new Image(10,12);
Image2.src = "../images/buttonbg.gif";

function press(ID)
{
	document.getElementById(ID).style.backgroundImage='url(../images/buttonbg.gif)';
	document.getElementById(ID).style.backgroundPosition='top';
}
function release(ID)
{
	document.getElementById(ID).style.backgroundPosition='bottom';
	document.getElementById(ID).style.backgroundImage='url(../images/buttonbg.gif)';
}
function over(ID)
{
	document.getElementById(ID).style.backgroundPosition='bottom';
	document.getElementById(ID).style.backgroundImage='url(../images/buttonbg.gif)';
}
	function linkify(URL,target, config)
{
	if(target != '')
	{
		// Open in a new window
		window.open(URL, target, config);
	}
	else
	{
		location.href=URL;
	}
}

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;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.id; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function doSubmit(formName, page)
{
	switch(page)
	{
		case 'index':
			// Ask Page
			MM_validateForm('formquestion','','R','formname','','R','formemail1','','RisEmail'); 
			break;
		case 'taf':
			// Tell-A-Friend
			MM_validateForm('Your First Name','','R','Your Email','','RisEmail','Friends Name','','R','Friends Email','','RisEmail');
			break;
	}
	
	if(document.MM_returnValue != true)
	{
		return document.MM_returnValue;
	}
	else
	{
		switch(page)
		{
			case 'index':
				// Ask Page
				document.forms[formName].submit();
				break;
			case 'taf':
				// Tell-A-Friend
				document.forms[formName].elements['ajax'].value = 'true';
				submitForm(document.forms[formName]);
				break;
		}
	}
}


/* Collapsable/Expandable Div Display Box */
function showDiv(ID)
{
	// If there is no div, don't throw a JavaScript error
	if(!document.getElementById(ID)){ return false; }
	
	// Evaluate the current visibility status of the display box.
	if(document.getElementById(ID).style.display == 'none')
	{
		// The display box is not visible, so we want to display the box
		document.getElementById(ID).style.display='block';
	}
	else
	{
		// The display box is visible, so we want to hide the display box
		document.getElementById(ID).style.display='none';
	}
}

function up_or_down(ID)
{
	if(up == true)
	{
		document.getElementById(ID).src = 'images/Collapse-Up.jpg';
		up = false;
	}
	else
	{		
		document.getElementById(ID).src = 'images/Collapse-Down.jpg';	
		up = true;
	}
}

function hideMessage()
{
	// Hide system messages on Admin page.
	document.getElementById('message').style.display='none';
	clearInterval(intervalID);
}

function addSecurityCode(formName, fieldName, ID)
{
	securityCode = document.forms[formName].elements[fieldName];

	// Does the field have data in it?
	if(securityCode.value.length != 0)
	{
		secruityLock = "?vbt=" + securityCode.value;
	}
	else
	{
		secruityLock = '';
	}
	
	// Return data to page
	document.getElementById(ID).innerHTML= secruityLock;
}

function forceDownloadAlert()
{
	if(document.forms['form1'].elements['forceDownload'].checked == true)
	{
		alert('By checking this box you will have to upload the config.ask.php file manually.');
	}
}

function setImage(newValue, img, formName, fieldName)
{
	if(newValue != '')
	{ 
		document.getElementById(img).src= newValue; 
		document.forms[formName].elements[fieldName].value= newValue; 
	}
}

function setFile(newValue, formName, fieldName)
{
	if(newValue != '')
	{ 
		document.forms[formName].elements[fieldName].value= newValue; 
	}
}

function submitForm(f)
{
	if(window.XMLHttpRequest){
	var xmlReq = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
	var xmlReq = new ActiveXObject('Microsoft.XMLHTTP');
	}
	var formData = '', elem = '';
	for(var s=0; s<f.elements.length; s++){
	elem = f.elements[s];
	if(formData != ''){
	formData += '&';
	}
	formData += elem.name+"="+elem.value;
	}
	xmlReq.onreadystatechange = function(){
	if(xmlReq.readyState == 4){
	document.getElementById('taf').innerHTML = xmlReq.responseText;
	}
	}
	xmlReq.open(f.method, f.action, true);
	xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlReq.send(formData);
	return false;
}

function setFlashCode(formName, targetFieldName, filePath, swfWidth, swfHeight)
{
	// Look for existing content
	if(document.forms[formName].elements[targetFieldName].value != '')
	{
		overwrite = confirm("This Custom HTML field already contains data.\nAre you sure you want to overwrite the current data?\n\nIf you would like to overwrite the current data press `Ok`\n\nOR\n\nIf you want to keep the current data press `Cancel`.");
	}
	else{ overwrite = true; }
	
	if(overwrite == true)
	{
		output  = '<script type="text/javascript">\n';
		
		output += "AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0','width','"+ swfWidth +"','height','"+ swfHeight +"','src','" + filePath.substr(0, (filePath.length-4)) + "','quality','high','pluginspage','http://www.macromedia.com/go/getflashplayer','movie','" + filePath.substr(0, (filePath.length-4)) + "' ); //end AC code";
		output += '</script>\n';
		output += '<noscript>\n';
		output += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" height="' + swfHeight + '" width="' + swfWidth + '">\n';
		output += '<param name="movie" value="' + filePath + '">\n';
		output += '<param name="quality" value="high">\n';
		output += '<embed src="' + filePath + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" height="' + swfHeight + '" width="' + swfWidth + '">';
		output += '</object>\n';
		output += '</noscript>';
	
		// Set new HTML
		document.forms[formName].elements[targetFieldName].value=output;
	}
}

function setImageCode(formName, targetFieldName, filePath, imgWidth, imgHeight, imgBorder, imgTitle, imgAlign)
{
	// Look for existing content
	if(document.forms[formName].elements[targetFieldName].value != '')
	{
		overwrite = confirm("This Custom HTML field already contains data.\nAre you sure you want to overwrite the current data?\n\nIf you would like to overwrite the current data press `Ok`\n\nOR\n\nIf you want to keep the current data press `Cancel`.");
	}
	else{ overwrite = true; }
	
	if(overwrite == true)
	{
		// check for empty attributes and do not include them
		if(imgWidth == '')  { newWidth = "";   } else { newWidth   = ' width="' + imgWidth + '"'; }
		if(imgHeight == '') { newHeight = "";  } else { newHeight = ' height="' + imgHeight + '"'; }
		if(imgTitle == '')  { newTitle = "";   } else { newTitle   = ' title="' + imgTitle + '"'; }
		if(imgBorder == '') { newBorder = "";  } else { newBorder  = ' border="' + imgBorder + '"'; }
		if(imgAlign == '')  { newAlign = "";   } else { newAlign   = ' align="' + imgAlign + '"'; }		
		
		output = '<img src="' + filePath + '"' + newHeight + newWidth + newTitle + newBorder + newAlign + '>';

		// Set new HTML
		document.forms[formName].elements[targetFieldName].value=output;			
	}
}
