// 2009-06-29 18:40 for page id 48 Lightbox Upload


// Javascript based on http://www.petefreitag.com/item/587.cfm
// (attachmentX discontinued, toProcess[n] implemented)
// *** Note that PHP is utilized in this Javascript. ***
// *** If moving to external JS then save as jscript.php and include after $cfg['file']['limit'] is defined ***
var upload_number = 1;
function addFileInput() {
	// if(upload_number > <?php echo $cfg['file']['limit']-1; ?>){
	if(upload_number > 1){
		alert('Sorry, you can only upload 1 file');
		exit(0);
	}
	var d = document.createElement("div");
	var l = document.createElement("a"); //***
	var file = document.createElement("input");
	file.setAttribute("type", "file");
	file.setAttribute("size", "35");
	file.setAttribute("style", "margin:5px 10px 5px 0px");
	file.setAttribute("name", "toProcess["+upload_number+"]");
	l.setAttribute("href", "javascript:removeFileInput('f"+upload_number+"');");  //***
	l.appendChild(document.createTextNode("Remove"));  //***
	d.setAttribute("id", "f"+upload_number); //***
	d.appendChild(file);
	d.appendChild(l); //***
	document.getElementById("moreUploads").appendChild(d);
	upload_number++;
}

function removeFileInput(i){
	var elm = document.getElementById(i);
	document.getElementById("moreUploads").removeChild(elm);
	upload_number = upload_number - 1; // decrement the max file upload counter if the file is removed
}

// validation, only works with first field
// To Do: determine how to validate input boxes 2-5

function phpMyEdit_trim(str)
{
    while (str.substring(0, 1) == " " || str.substring(0, 1) == "n" || str.substring(0, 1) == "r") {
        str = str.substring(1, str.length);
    }
    while (str.substring(str.length - 1, str.length) == " " || str.substring(str.length - 1, str.length) == "n" || str.substring(str.length - 1, str.length) == "r") {
        str = str.substring(0, str.length - 1);
    }
    return str;
};

function phpMyEdit_form_control(theForm)
{
    if(phpMyEdit_trim(theForm.toProcess0.value) === ""){
        alert("Click the Browse button in order to select a file for upload.");
        theForm.toProcess0.focus();
        return false;
    }
    if(phpMyEdit_trim(theForm.photo_text.value) === ""){
        alert("Please enter text to be displayed with your photo");
        theForm.photo_text.focus();
        return false;
    }
    return true;
};

function TrackCount(fieldObj,countFieldName,maxChars) {
	// Usage: <textarea name="c" rows="6" cols="48" onkeyup="TrackCount(this,'textcount',255)" onkeypress="LimitText(this,255)" onChange="javascript:this.value=this.value.toUpperCase();"></textarea>
	// Characters remaining: <input type="text" name="textcount" size="3" value="255">
	// <br><input type="submit" name="submit1" value="Proceed To The Next Page...">
	var countField = eval("fieldObj.form."+countFieldName);
	var diff = maxChars - fieldObj.value.length;
	// Need to check & enforce limit here also in case user pastes data
	if (diff < 0) {
		fieldObj.value = fieldObj.value.substring(0,maxChars);
		diff = maxChars - fieldObj.value.length;
	}
	countField.value = diff;
}

function LimitText(fieldObj,maxChars) {
	var result = true;
	if (fieldObj.value.length >= maxChars)
		result = false;
	if (window.event)
		window.event.returnValue = result;
	return result;
}

