/***************************************************************************\
| filename		: wfunc.js
| description	: generic javascript functions
| created  	: 	22/09/06 By Walter
| modified 	:
|
| (c) Walter - www.waltertje.com
|
\***************************************************************************/

//change display style of a span-element
function openCloseSpan(strSpan)
{
	if (this.document.getElementById(strSpan).style.display == "none")
	{
		this.document.getElementById(strSpan).style.display = "block";
	}
	else
	{
   		this.document.getElementById(strSpan).style.display = "none";
	}
}//openCloseSpan()

//opens a new window to search for certain fields (eg artist(s) )
//passes the id-value to a field frmForm[strField + '_id'] and the title-value to frmForm[strField]
function openSelectWindow(strMode, strField, strFieldType, strEFC)
{
	if ( strEFC == null )
	{
		strEFC = '';
	}

	window.open(sBase + 'select.php?mode=' + strMode + '&field=' + strField + '&fieldtype=' + strFieldType + '&efc=' + strEFC,'wiSelect','width=400,height=350,status=no,toolbar=no,menubar=no,resizable=yes');
}

//opens the select-a-lot-page
//passes id- and title value to a select field
function openSelectALotWindow(strMode, strField, strFieldType)
{
	window.open(sBase + 'selectalot.php?mode=' + strMode + '&field=' + strField + '&fieldtype=' + strFieldType,'wiSelect','width=600,height=450,status=no,toolbar=no,menubar=no,resizable=yes');
} //openSelectALotWindow()

//opens a new window to create a new contact
//passes the id-value to a field frmForm[strField + '_id'] and the name-value to frmForm[strField]
function openContactWindow(strField)
{
	window.open('contact_add.php?mini=TRUE&field=' + strField,'wiSelect','width=500,height=575,status=no,toolbar=no,menubar=no,resizable=yes')
}

//clear a field and the ID-field related to it
function emptyField(objField, objField_id)
{
	objField.value = '';
	objField_id.value = 0;
}

//add an option to a select list
function addOption(objField, strValue, strText)
{
	iCount = objField.options.length;
	objField.options[iCount] = new Option(strText, strValue);
}

//remove an option from a select list
function delOption(objField, iOption)
{
	count = objField.options.length;
	for ( i = 0; i < count; i++ )
	{
		if ( objField.options[i].selected )
		{
			objField.options[i] = null;
			//the select-box is recalculated. So the counters decrease too
			i--;
			count--;
		}
	}
}

//returns a string with all selected options in a select list
//seperated by a comma
function get_selected_values(objSelect)
{
	str = '';
	count = objSelect.options.length;
	for ( i = 0; i < count; i++ )
	{
		if ( objSelect.options[i].selected )
		{
			if ( str != '' )
			{
				str += ',';
			}
			str += objSelect.options[i].value;
		}
	}
	
	return str;
}

//dynamic fields have dynamic fieldnames ending in an index (eg myField7)
//this function cuts the index from the fieldname
//eg myField1 -> 1, my12thfield99 -> 99
function getIndexFromFieldname(strFieldname)
{
	var str;

	str = strFieldname.match(/[0-9]+$/); //$ means last found pattern

	if ( str == null )
	{
		str = -1;
	}

	return str;
}

function GetXmlHttpObject()
{
	var objXMLHttp=null;
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}

	return objXMLHttp;
}

function trim(value) 
{
	value = value.replace(/^\s+/,'');
	value = value.replace(/\s+$/,'');
	return value;
}

function get_select_values(objSelect)
{
	str = '';
	count = objSelect.options.length;
	for ( i = 0; i < count; i++ )
	{
		str += objSelect.options[i].value;
		if ( ( i + 1 ) < count )
		{
			str += ',';
		}
	}

	return str;
} //get_select_values()

//call the rating-script via AJAX
//Note: variables must be set in the html-page
function rateIt(strSpaRatingID, strSpaRateFormID, strSelRatingID, intParentID, intRateType)
{
	var intRating = document.getElementById(strSelRatingID).value;
	var strBaseScriptUrl = './ajax/rate.php?rate_type=' + intRateType;
	
	if ( intRating > 0 )
	{
		xmlHttp = GetXmlHttpObject();
		if (xmlHttp == null)
		{
			alert ('Your browser is too old for this application. You can download a newer version for free at www.getfirefox.com or www.microsoft.com\n');
			return;
		}
		
		strScriptUrl = strBaseScriptUrl + '&parent_id=' + intParentID + '&rating=' + intRating;
		xmlHttp.onreadystatechange = updateRating;
		xmlHttp.onreadystatechange = function(){ updateRating(strSpaRatingID, strSpaRateFormID); };
		xmlHttp.open("GET", strScriptUrl, true);
		xmlHttp.send(null);
	}

	return;
} //-->rateIt()

//update the span spaRating with the new values passed via AJAX.
//in the html-page the used variables must be declared!
function updateRating(strSpaRatingID, strSpaRateFormID)
{
	//the rating has been processed.
	//update the image, the count, the message and hide the form
	if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" )
	{
		var strResult = xmlHttp.responseText;
		//document.getElementById("spaDebug").innerHTML = strResult;
		var spaRating = document.getElementById(strSpaRatingID);
		var spaRateForm = document.getElementById(strSpaRateFormID);
		
		//strResult = rating|rates|your_vote
		arResult = strResult.split('|');
		intNewRating = arResult[0];
		intNewRatings = arResult[1];
		intYourVote = arResult[2];
		
		spaRating.innerHTML = strImgRating.replace('%d', intNewRating) + ' ' + strMsgVotes.replace('%d', intNewRatings) + ' ' + strMsgYourVote.replace('%d', intYourVote);
		//hide the form
		spaRateForm.style.display = 'none';
	}
	
	return;
} //-->updateRating()

//call the vote-script via AJAX
//Note: variables must be set in the html-page
function vote(intPollID, intVote)
{
	var strBaseScriptUrl = './ajax/vote.php?poll_id=' + intPollID + '&vote=' + intVote;
	
	if ( ( intVote > 0 ) && ( intVote < 6 ) )
	{
		xmlHttp = GetXmlHttpObject();
		if (xmlHttp == null)
		{
			alert ('Your browser is too old for this application. You can download a newer version for free at www.getfirefox.com or www.microsoft.com\n');
			return;
		}
		strScriptUrl = strBaseScriptUrl;
		xmlHttp.onreadystatechange = updatePoll;
		xmlHttp.onreadystatechange = function(){ updatePoll(); };
		xmlHttp.open("GET", strScriptUrl, true);
		xmlHttp.send(null);
	}

	return;
} //-->vote()

//update the span spaRating with the new values passed via AJAX.
//in the html-page the used variables must be declared!
function updatePoll()
{
	//the rating has been processed.
	//update the image, the count, the message and hide the form
	if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" )
	{
		var strResult = xmlHttp.responseText;		
		var spaPollResults = document.getElementById("spaPollResults");
		var spaDebug = document.getElementById("spaDebug");		
		//spaDebug.innerHTML = strResult
		
		//strResult = question|total_votes|answer1|votes1|percent1|...votes5|percent5
		arResult = strResult.split('|');
		
		strOutput = '';
		for ( i = 2; i < 16; i = i + 3)
		{
			if  ( arResult[i] != '' )
			{
				strImgTxt =  arResult[(i+1)] + ' votes (' +  arResult[(i+2)] + '%)';
				strImgUrl = 'ajax/createimg.php?votes=' + arResult[(i+1)] + '&perc=' + arResult[(i+2)];
				strOutput = strOutput + arResult[i] + '<br /><img src="' + strImgUrl + '" alt="' + strImgTxt + '"><br />';
			}
		}
		
		//spaDebug.innerHTML = strOutput;
		spaPollResults.innerHTML = strOutput;
	}
	
	return;
} //-->updatePoll()

function maxLength(objField, maxlength)
{
   	length = objField.value.length;
   	r = length - maxlength;
	if ( length > maxlength )
    {
    	objField.value = objField.value.substring(0, objField.value.length - r);
    }
} //maxLength()