$(document).ready(function()
{
    
    $("input[type='text']").css('width', '180px')
                            .css('height', '16px')
                            .css('padding-top', '5px')
                            .css('border', 'solid 1px #ccc');
                            
    $("input[type='password']").css('width', '180px')
                                .css('height', '16px')
                                .css('padding-top', '5px')
                                .css('border', 'solid 1px #ccc');
                                
    if($.browser.msie && $.browser.version=="6.0")
    {
         $("input[type='text']").css('width', '180px')
                            .css('height', '24px')
                            .css('padding-top', '5px');
                            
        $("input[type='password']").css('width', '180px')
        .css('height', '24px')
        .css('padding-top', '5px');
    }
    
    
});

function browser(){
	this.dom = (document.getElementById) ? true : false;
	this.ns4 = (document.layers) ? true : false;
	this.ie = (document.all) ? true : false;
	this.ns6 = this.dom && !this.ie
	this.ie4 = this.ie && !this.dom;
	this.opera = (navigator.userAgent.indexOf("Opera") != -1);
	this.mac = (navigator.appVersion.indexOf("Mac") != -1);
	this.macie4 = this.ie4 || this.mac;
	this.dhtml = (this.dom || this.ns4 || this.ie || this.ie4 || this.opera || !this.mac) ? true : false;
	this.dhtml = (this.mac)?false:true;
	return this;
}

var bw = new browser();

function toggleTable(el,st) {
	var myelement = document.getElementById(el);

	if(st == 1){
		if(bw.ie){
			myelement.style.display = "inline"
		}else{
			myelement.style.display = "table-cell"
		}
	}else{
		myelement.style.display = "none"
	}
}

function agency_cmb_type(id){
	theform = document.register;

	if(id == "Other"){
		toggleTable('type_1',1);
		theform.elements["data[agency][0][type]"].value = "";
	}else{
		toggleTable('type_1',0);
		theform.elements["data[agency][0][type]"].value = id;
	}
}


function country_states(id){
	theform = document.register;

	if(id == 840){
		toggleTable('el_1',1);
		toggleTable('el_2',0);
		toggleTable('el_3',0);
		theform.elements["state_province"][0].selected = true;
	}else if(id == 124){
		toggleTable('el_1',0);
		toggleTable('el_2',1);
		toggleTable('el_3',0);
		theform.elements["canada_province"][0].selected = true;
	}else{
		toggleTable('el_1',0);
		toggleTable('el_2',0);
		toggleTable('el_3',1);
		theform.elements["data[agency][0][state_province]"].value = "";
	}
}

function country_states_store(id){
	theform = document.winners_store;

	if(id == 840){
		toggleTable('el_1',1);
		toggleTable('el_2',0);
		theform.elements["state_province"][0].selected = true;
	}else{
		toggleTable('el_1',0);
		toggleTable('el_2',1);
		theform.elements["store[delivery][state_province]"].value = "";
	}
}

function set_state_store(str){
	theform = document.winners_store;
	theform.elements["store[delivery][state_province]"].value = str;
}


function empty_basket(){
	if(confirm("Do you really want to empty your basket?")){
		window.location = "/store/?action=empty";
	}
}

function remove_from_basket(item,item_id){
	if(confirm("Do you really want to remove "+item+" from your basket?")){
		window.location = "/store/?b="+item_id+"&action=remove";
	}
}

function delete_basket_Form(basketform, theaction){
	if(confirm("Do you really want to remove selected items?")){
		basketform.action = theaction;
		basketform.submit();
	}
}

function country_states_checkout(id, theform, the_element, prefix,clear){

	if(id == 840){
		toggleTable(prefix+'_el_1',1);
		toggleTable(prefix+'_el_2',0);
		toggleTable(prefix+'_el_3',0);

		toggleTable(prefix+'_el_not_man',0);
		toggleTable(prefix+'_el_man',1);

		theform.elements[prefix+"_state_province"][0].selected = true;
	}else if(id == 124){
		toggleTable(prefix+'_el_1',0);
		toggleTable(prefix+'_el_2',1);
		toggleTable(prefix+'_el_3',0);

		toggleTable(prefix+'_el_not_man',0);
		toggleTable(prefix+'_el_man',1);

		theform.elements[prefix+"_canada_province"][0].selected = true;
	}else{
		toggleTable(prefix+'_el_1',0);
		toggleTable(prefix+'_el_2',0);
		toggleTable(prefix+'_el_3',1);

		toggleTable(prefix+'_el_not_man',1);
		toggleTable(prefix+'_el_man',0);

		if(clear)
		theform.elements[the_element].value = "";
	}
}

function set_state_checkout(theform,str,the_element){
	theform.elements[the_element].value = str;
}

function submitOffStore(theform, theaction){
	var checked_type = 0;

	for(x = 0;x < theform.payment_type.length; x++){
		if (theform.payment_type[x].checked == 1) {
			checked_type = 1;
		}
	}

	if(checked_type == 1){
		theform.action = theaction;
		theform.submit();
	}else{
		alert("Please select one of the offline payment choices.");
	}
}

function set_state(str){
	theform = document.register;
	theform.elements["data[agency][0][state_province]"].value = str;
}

function statusSetter(str) {
   window.defaultStatus = str;
   window.status = str;
}

function submitForm(theform, theaction){
	theform.action = theaction;
	theform.submit();
}

function submitCredit(theform, theaction, thecount, thefield){
	store_name = theform.elements["store["+thefield+"]["+thecount+"][name]"];
	store_quantity = theform.elements["store["+thefield+"]["+thecount+"][quantity]"];
	store_entry_id= theform.elements["store["+thefield+"]["+thecount+"][entry_id]"];


	if(store_name.value == "" || store_quantity.value=="" || store_entry_id.options[store_entry_id.options.selectedIndex].value == ""){
		alert("Please make sure you have filled in the following: 'WINNING ENTRY' or 'COMPANY/INDIVIDUAL NAME' or 'QUANTITY' before adding additional " + thefield);
	}else{
		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,theaction){
	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.action = theaction;
			theform.submit();
		}
	}else{
		if(theaction != '')
			theform.action = theform.action+theaction;
		else
			theform.action = theaction;

		theform.submit();
	}
}

function addDonation(theform, donation_amount, total_amount){

	if(theform.elements["donation"][0].checked != true){
		amount_value = donation_amount + total_amount;
		theform.elements["amount"].value = "$" + amount_value;
		alert('Thank You\nAn addition of $' + donation_amount + ' will be added to your Invoice.');
	}else{
		theform.elements["amount"].value = "$" + total_amount;
	}

}


function submitOffline(theform, theaction){
	var checked_type = 0;

	for(x = 0;x < theform.payment_type.length; x++){
		if (theform.payment_type[x].checked == 1) {
			checked_type = 1;
		}
	}

	if(checked_type == 1){

//		if(theform.elements["userdata[entry_code]"].value == ""){
//			if(confirm("Please remember to fill in your Entry Code if you received a Call for Entry through the mail.  This is found below your address in red.\n\nPress OK to continue with processing your payment without the entry code.")){
//				theform.action = theaction;
//				theform.submit();
//			}
//		}else{
//		if(confirm("PLEASE NOTE:\nYou will NOT be able to add/delete entries to this entry group.\nYou will be able to change credit information in your entries by clicking the Closed Entries link under the My Entries tab.")){
			theform.action = theaction;
			theform.submit();
//		}
//		}
	}else{
		alert("Please select one of the offline payment choices.");
	}
}

function submitOnline(theform, theaction){

//	if(theform.elements["userdata[entry_code]"].value == ""){
//		if(confirm("Please remember to fill in your Entry Code if you received a Call for Entry through the mail.  This is found below your address in red.\n\nPress OK to continue with processing your payment without the entry code.")){
//			theform.action = theaction;
//			theform.submit();
//		}
//	}else{

//	if(confirm("PLEASE NOTE:\nYou will NOT be able to add/delete entries to this entry group.\nYou will be able to change credit information in your entries by clicking the Closed Entries link under the My Entries tab.")){
		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 toggleOff(elv){
	for(var x= 0; x < 4; x++) {
		if(x > elv){
			toggleTable('el_' + x,0);
		}
	}
}

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, '#EAE7E5', '#FFCBA4', '#C9E2FF');
	}

}

function deleteCredits(theform, 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 credits?")){
			theform.submit();
		}
	}else{
		alert("Please select credits you want to delete.");
	}
}

function editCredits(theform, allShow){
	var item_selected = 0;
	edit_count = 0;

	if(theform.elements[allShow + "_id[]"].checked == true){
		item_selected = 1;
		edit_count = 1;
	}

	for(x = 0;x < theform.elements[allShow + "_id[]"].length; x++){
		if(theform.elements[allShow + "_id[]"][x].checked == true){
			item_selected = 1;
			edit_count ++;
			if(edit_count > 1){
				break;
			}
		};
	}

	if(edit_count == 0){
		alert("Please select an entry you want to edit.");
	}else if(edit_count > 1){
		alert("Please only select a single entry you want to edit.");
	}else{
		theform.elements["edit_entry"].value = 1;
		theform.submit();
	}
}

function checkGeneric(allItem, entry_form){
	allItem = entry_form.elements[allItem];

	if(allItem.checked == false){
		setFlag = false;
	}else{
		setFlag = true;
	}

	entry_form.elements[allItem.name + "_id[]"].checked = setFlag;

	for(x = 0;x < entry_form.elements[allItem.name + "_id[]"].length; x++){
		entry_form.elements[allItem.name + "_id[]"][x].checked = setFlag;
	}

}

function checkSetGeneric(allItem, entry_form){

	if(entry_form.elements[allItem].checked == false){
		entry_form.elements[allItem].checked = true;
	}else{
		entry_form.elements[allItem].checked = false;
	}

	checkGeneric(allItem, entry_form);
}


function setRow(theRow){
	theRowOver = theRow;
}

function CH(checkItem){
	if(checkItem.checked == true){
		setPointer(theRowOver, 0, 'over', '#FFFFFF', '#E3E3E3', '#C9E2FF');
	}else{
		setPointer(theRowOver, 0, 'out', '#FFFFFF', '#E3E3E3', '#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
