function statusSetter(str) {
   window.defaultStatus = str;
   window.status = str;
}

function submitForm(theform, theaction){
	theform.action = theaction;
	theform.submit();
}

function submitFinal(theform){
	confirm_final = theform.elements["data[entry_applications][0][update_locked]"][0].checked;
	
	if(confirm_final == true){
		if(confirm("Are you sure you'd like to proceed? By selecting 'YES' you understand that the system will not allow you to change your credits under any circumstances. Only proceed if your credits have been approved and are confirmed for publication.")){
			theform.submit();
		}
	}else{
		theform.submit();
	}
}

function submitUpload(theform){
	confirm_final = theform.elements["upload_locked"][0].checked;
	
	if(confirm_final == true){
		if(confirm("Are you sure you'd like to proceed? By selecting 'YES' you understand that the system will not allow you to upload or replace additional files under any circumstances. Only proceed if your files have been confirmed and meet the publication standards outlined in the Preparation section.")){
			theform.submit();
		}
	}else{
		theform.submit();
	}
}

function submitOffline(theform, theaction){
	if(confirm("PLEASE NOTE:\nYou will NOT be able to add/delete entries to this entry group.\nYou will be able change credit information in the Entry history under My Account.")){
		theform.action = theaction;
		theform.submit();
	}
}

function deleteForm(theform, theaction, allShow){
	var item_selected = 0;

	if(theform.elements[allShow + "_id[]"].checked == true){
		item_selected = 1;
	}
	
	for(x = 0;x < theform.elements[allShow + "_id[]"].length; x++){
		if(theform.elements[allShow + "_id[]"][x].checked == true){
			item_selected = 1;
			break;	
		};
	}
	
	if(item_selected == 1){
		if(confirm("Do you really want to delete selected entries?")){
			theform.action = theaction;
			theform.submit();
		}
	}else{
		alert("Please select entries you want to delete.");	
	}
}

function printLabels(theform, theaction, allShow){
	var print_selected = 0;
	var qry = "";
	
	if(theform.elements[allShow + "_id[]"].checked == true){
		if(theform.elements[allShow + "_id[]"].value != undefined){
			qry = theform.elements[allShow + "_id[]"].value
			print_selected = 1;
		}
	}
	
	
	for(x = 0;x < theform.elements[allShow + "_id[]"].length; x++){
		if(theform.elements[allShow + "_id[]"][x].checked == true){
			if(print_selected == 0){
				qry = theform.elements[allShow + "_id[]"][x].value
			}else{
				qry += "," + theform.elements[allShow + "_id[]"][x].value
			}
			print_selected = 1;
			
		};
	}
	
	
	if(print_selected == 1){
		window.open(theaction + qry);
	}else{
		alert("Please select entries you want to print.");	
	}
	
	

}


function editEntry(allShow, fType){
	edit_count = 0;
	theform = eval('document.entries_' + allShow);
	
	if(theform.elements[allShow + "_id[]"].checked == true){
		entry_id = theform.elements[allShow + "_id[]"].value;
		edit_count = 1;
	}
	
	for(x = 0;x < theform.elements[allShow + "_id[]"].length; x++){
		if(theform.elements[allShow + "_id[]"][x].checked == true){
			entry_id = theform.elements[allShow + "_id[]"][x].value;
			edit_count ++;
			if(edit_count > 1){
				break;
			}
		};
	}
	
	if(edit_count == 0){
		alert("Please select an entry you want to "+fType+".");
	}else if(edit_count > 1){
		alert("Please only select a single entry you want to "+fType+".");
	}else{
		if(fType == "edit"){
			window.location = "/work/entry/edit/?id=" + entry_id;
		}else if(fType == "duplicate"){
			window.location = "/work/entry/clone/?id=" + entry_id;
		}else if(fType == "edit_credits"){
			window.location = "/account/history/edit/?id=" + entry_id;
		}else if(fType == "upload files"){
			window.location = "/finalists/upload/?id=" + entry_id;
		}else if(fType == "upload digital files"){
			window.location = "/work/entry/upload/?id=" + entry_id;	
		}else if(fType == "update"){
			window.location = "/finalists/edit/?id=" + entry_id;
		}
		
	}
	
	
}


function prefill_entry(show_name, show_id, category_name, category_id, group_id){
	fillCategoryDropDown(show_name, category_name);
	fillSubCategoryDropDown(category_id, group_id);
}



function fillCategoryDropDown(val, select){
	show_name = val;
	
	document.entries.CategoryType.focus();
	
	document.entries.CategoryType.length = 1;
	document.entries.CategoryType.options[0] = new Option("-- Choose Category --");
   	document.entries.CategoryType.options[0].value = '0';
	document.entries.CategoryType.options[0].selected = true;
	
	document.entries.SubCategoryType.length = 1;
	document.entries.SubCategoryType.options[0].selected = true;
	
	newOptions = eval(val+'Category');
	
	for(var i= 0; i < newOptions.length; i++) {
		document.entries.CategoryType.length=i+1;
		thisEntry = newOptions[i][1];
		newOption = new Option( thisEntry );
		newOption.value = newOptions[i][0];
		document.entries.CategoryType.options[i+1] = newOption;
		
		if (thisEntry == select && select != '' ){
			document.entries.CategoryType.options[i+1].selected = true;
		}
	}
}


function fillSubCategoryDropDown(val, select){
	
	document.entries.SubCategoryType.focus();
	
	document.entries.SubCategoryType.length = 1;
	document.entries.SubCategoryType.options[0] = new Option("-- Choose Sub-Category --");
   	document.entries.SubCategoryType.options[0].value = '0';
	document.entries.SubCategoryType.options[0].selected = true;
	
	
	OptionShow = eval(show_name+'Category');
	for(var i= 0; i < OptionShow.length; i++) {
		if(OptionShow[i][0] == val){
			category = i;
			break;
		}
	}
	
	newOptions = eval(show_name+'Category[' + category + '][2]');
	
	for(var i= 0; i < newOptions.length; i++) {
		document.entries.SubCategoryType.length=i+1;
		thisEntry = newOptions[i][1];
		newOption = new Option( thisEntry );
		newOption.value = newOptions[i][0];
		document.entries.SubCategoryType.options[i+1] = newOption;
		
		if (newOptions[i][0] == select && select != '' ){
			document.entries.SubCategoryType.options[i+1].selected = true;
		}
		
	}
}

function reset_contacts(){
	document.entries.elements["data[entry_applications][0][adhoc_name]"].disabled = false;
	document.entries.elements["data[entry_applications][0][adhoc_lastname]"].disabled = false;
	document.entries.elements["data[entry_applications][0][adhoc_phone]"].disabled = false;
	document.entries.elements["data[entry_applications][0][adhoc_email]"].disabled = false;	
}


function populate_contacts(selectItem){
	if(selectItem != 0){
		id = Contacts[selectItem][0];
		firstname = Contacts[selectItem][1];
		lastname = Contacts[selectItem][2];
		phone = Contacts[selectItem][3];
		email = Contacts[selectItem][4];
		
		dis = true;
	}else{
		id = "";
		firstname = "";
		lastname = "";
		phone = "";
		email = "";
		
		dis = false;
	}
	
	document.entries.elements["data[entry_applications][0][agency_contacts_id]"].value = id;
	document.entries.elements["data[entry_applications][0][adhoc_name]"].value = firstname;
	document.entries.elements["data[entry_applications][0][adhoc_lastname]"].value = lastname;
	document.entries.elements["data[entry_applications][0][adhoc_phone]"].value = phone;
	document.entries.elements["data[entry_applications][0][adhoc_email]"].value = email;
	
	document.entries.elements["data[entry_applications][0][adhoc_name]"].disabled = dis;
	document.entries.elements["data[entry_applications][0][adhoc_lastname]"].disabled = dis;
	document.entries.elements["data[entry_applications][0][adhoc_phone]"].disabled = dis;
	document.entries.elements["data[entry_applications][0][adhoc_email]"].disabled = dis;
}

function setAllRow(theRow){
	if (navigator.appName.indexOf('Netscape') != -1){
		tableMain = theRow.parentNode; //mozilla
	}else{
		tableMain = theRow.parentElement; //ie & safari
	}	
	
	theRows = tableMain.getElementsByTagName('tr');
	
	//alert(navigator.appName)
	///alert(theRows[4].getAttribute('bgcolor'))
}

function checkAllSet(allItem, allShow){
	entry_form = eval("document.entries_" + allShow);
	
	
	if(entry_form.elements[allItem].checked == false){
		entry_form.elements[allItem].checked = true;
	}else{
		entry_form.elements[allItem].checked = false;
	}
	
	checkAll(allItem, allShow);
}

function checkAll(allItem, allShow){
	entry_form = eval("document.entries_" + allShow);
	
	allItem = entry_form.elements[allItem];
	
	if(allItem.checked == false){
		setFlag = false;
		setOver = "out";	
	}else{
		setFlag = true;
		setOver = "over";
	}
	
	
	
	entry_form.elements[allShow + "_id[]"].checked = setFlag;
	
	for(x = 0;x < entry_form.elements[allShow + "_id[]"].length; x++){
		entry_form.elements[allShow + "_id[]"][x].checked = setFlag;
	}
	
	for(x = 0;x < theRows.length; x++){
		setPointer(theRows[x], 0, setOver, '#D6E2F0', '#B1C5CB', '#C9E2FF');
	}
	
}

function setRow(theRow){
	theRowOver = theRow;
}

function CH(checkItem){
	if(checkItem.checked == true){
		setPointer(theRowOver, 0, 'over', '#D6E2F0', '#B1C5CB', '#C9E2FF');
	}else{
		setPointer(theRowOver, 0, 'out', '#D6E2F0', '#B1C5CB', '#C9E2FF');
	}
}

function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
	
    var theCells = null;
	var marked_row = "";
    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == '' || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function
